Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rewrite to Twitch Helix API Version, the original script is written by someone else
- var apiKey = "YourApiKey";
- var username = "YourChannelName";
- var twitch_widget = $("#livestream, #livestream-button");
- var recntStatChange = 0;
- var visitOffline = 0;
- var long = 60000;
- var short = 5000;
- function refresh() {
- handleTwitchStream();
- }
- function notRecnt() {
- if (recntStatChange == 0) {
- recntStatChange = 1;
- } else {
- if (recntStatChange == 1) {
- recntStatChange = 0;
- }
- }
- }
- function recntAndRefresh() {
- handleTwitchStream();
- notRecnt();
- }
- function handleTwitchStream() {
- twitch_widget.attr(`href`, `https://twitch.tv/${username}`);
- $.ajax({
- url: `https://api.twitch.tv/helix/streams?user_login=${username}`,
- type: `GET`,
- dataType: `json`,
- headers: {
- "Client-ID": apiKey
- },
- success: (res) => {
- if (res.data.length > 0) {
- if (recntStatChange == 0) {
- showStreamElements();
- visitOffline = 1;
- setTimeout(recntAndRefresh, long);
- }
- if (recntStatChange == 1) {
- setTimeout(refresh, short);
- //Refresh period set to short, streaming
- }
- }
- else {
- if (recntStatChange == 1) {
- hideStreamElements();
- setTimeout(recntAndRefresh, long);
- }
- if (recntStatChange == 0) {
- if (visitOffline == 0) {
- visitOffline = 1;
- setTimeout(refresh, long/2);
- }
- else {
- setTimeout(refresh, short);
- }
- }
- }
- },
- error: hideStreamElements
- });
- }
- function hideStreamElements() {
- $("#livestream, #livestream-button, #livestream iframe").addClass("hidetwitch");
- $iframe = $("#livestream, #livestream-button").find("iframe");
- $iframe.attr("src", $iframe.attr("src"));
- }
- function showStreamElements() {
- $("#livestream, #livestream-button, #livestream iframe").removeClass("hidetwitch");
- }
Add Comment
Please, Sign In to add comment