2022-03-31 17:33:10 +02:00
|
|
|
// ==UserScript==
|
|
|
|
// @name TwitchAdSolutions (vaft)
|
|
|
|
// @namespace https://github.com/pixeltris/TwitchAdSolutions
|
2024-06-16 07:53:51 +02:00
|
|
|
// @version 9.0.0
|
2022-03-31 17:33:10 +02:00
|
|
|
// @description Multiple solutions for blocking Twitch ads (vaft)
|
|
|
|
// @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js
|
|
|
|
// @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js
|
|
|
|
// @author https://github.com/cleanlock/VideoAdBlockForTwitch#credits
|
|
|
|
// @match *://*.twitch.tv/*
|
|
|
|
// @run-at document-start
|
2023-10-19 17:45:09 +02:00
|
|
|
// @inject-into page
|
2022-03-31 17:33:10 +02:00
|
|
|
// @grant none
|
|
|
|
// ==/UserScript==
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
//This stops Twitch from pausing the player when in another tab and an ad shows.
|
|
|
|
try {
|
|
|
|
Object.defineProperty(document, 'visibilityState', {
|
|
|
|
get() {
|
|
|
|
return 'visible';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Object.defineProperty(document, 'hidden', {
|
|
|
|
get() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const block = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
};
|
|
|
|
const process = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
//This corrects the background tab buffer bug when switching to the background tab for the first time after an extended period.
|
|
|
|
doTwitchPlayerTask(false, false, true, false, false);
|
|
|
|
};
|
2022-09-01 20:12:51 +02:00
|
|
|
document.addEventListener('visibilitychange', block, true);
|
2022-03-31 17:33:10 +02:00
|
|
|
document.addEventListener('webkitvisibilitychange', block, true);
|
|
|
|
document.addEventListener('mozvisibilitychange', block, true);
|
|
|
|
document.addEventListener('hasFocus', block, true);
|
|
|
|
if (/Firefox/.test(navigator.userAgent)) {
|
|
|
|
Object.defineProperty(document, 'mozHidden', {
|
|
|
|
get() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Object.defineProperty(document, 'webkitHidden', {
|
|
|
|
get() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
function declareOptions(scope) {
|
|
|
|
scope.AdSignifier = 'stitched';
|
|
|
|
scope.ClientID = 'kimne78kx3ncx6brgo4mv6wki5h1ko';
|
|
|
|
scope.ClientVersion = 'null';
|
|
|
|
scope.ClientSession = 'null';
|
2023-08-09 02:07:38 +02:00
|
|
|
scope.PlayerType2 = 'embed'; //Source
|
2024-04-12 13:46:45 +02:00
|
|
|
scope.PlayerType3 = 'site'; //Source
|
|
|
|
scope.PlayerType4 = 'autoplay'; //360p
|
2022-03-31 17:33:10 +02:00
|
|
|
scope.CurrentChannelName = null;
|
|
|
|
scope.UsherParams = null;
|
|
|
|
scope.WasShowingAd = false;
|
|
|
|
scope.GQLDeviceID = null;
|
|
|
|
scope.IsSquadStream = false;
|
2022-08-27 16:32:21 +02:00
|
|
|
scope.StreamInfos = [];
|
|
|
|
scope.StreamInfosByUrl = [];
|
|
|
|
scope.MainUrlByUrl = [];
|
|
|
|
scope.EncodingCacheTimeout = 60000;
|
2023-06-01 11:33:30 +02:00
|
|
|
scope.ClientIntegrityHeader = null;
|
|
|
|
scope.AuthorizationHeader = null;
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
declareOptions(window);
|
2024-04-12 13:46:45 +02:00
|
|
|
var twitchWorkers = [];
|
2022-03-31 17:33:10 +02:00
|
|
|
var adBlockDiv = null;
|
|
|
|
var OriginalVideoPlayerQuality = null;
|
|
|
|
var IsPlayerAutoQuality = null;
|
|
|
|
const oldWorker = window.Worker;
|
|
|
|
window.Worker = class Worker extends oldWorker {
|
|
|
|
constructor(twitchBlobUrl) {
|
|
|
|
var newBlobStr = `
|
2022-08-27 16:32:21 +02:00
|
|
|
${getStreamUrlForResolution.toString()}
|
|
|
|
${getStreamForResolution.toString()}
|
|
|
|
${stripUnusedParams.toString()}
|
2022-03-31 17:33:10 +02:00
|
|
|
${processM3U8.toString()}
|
|
|
|
${hookWorkerFetch.toString()}
|
|
|
|
${declareOptions.toString()}
|
|
|
|
${getAccessToken.toString()}
|
|
|
|
${gqlRequest.toString()}
|
|
|
|
${adRecordgqlPacket.toString()}
|
|
|
|
${tryNotifyTwitch.toString()}
|
|
|
|
${parseAttributes.toString()}
|
2024-06-16 07:53:51 +02:00
|
|
|
${getWasmWorkerUrl.toString()}
|
|
|
|
var workerUrl = getWasmWorkerUrl('${twitchBlobUrl}');
|
|
|
|
if (workerUrl && workerUrl.includes('assets.twitch.tv/assets/amazon-ivs-wasmworker')) {
|
|
|
|
declareOptions(self);
|
|
|
|
self.addEventListener('message', function(e) {
|
|
|
|
if (e.data.key == 'UpdateIsSquadStream') {
|
|
|
|
IsSquadStream = e.data.value;
|
|
|
|
} else if (e.data.key == 'UpdateClientVersion') {
|
|
|
|
ClientVersion = e.data.value;
|
|
|
|
} else if (e.data.key == 'UpdateClientSession') {
|
|
|
|
ClientSession = e.data.value;
|
|
|
|
} else if (e.data.key == 'UpdateClientId') {
|
|
|
|
ClientID = e.data.value;
|
|
|
|
} else if (e.data.key == 'UpdateDeviceId') {
|
|
|
|
GQLDeviceID = e.data.value;
|
|
|
|
} else if (e.data.key == 'UpdateClientIntegrityHeader') {
|
|
|
|
ClientIntegrityHeader = e.data.value;
|
|
|
|
} else if (e.data.key == 'UpdateAuthorizationHeader') {
|
|
|
|
AuthorizationHeader = e.data.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
hookWorkerFetch();
|
|
|
|
importScripts(workerUrl);
|
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
`;
|
|
|
|
super(URL.createObjectURL(new Blob([newBlobStr])));
|
2024-04-12 13:46:45 +02:00
|
|
|
twitchWorkers.push(this);
|
2022-03-31 17:33:10 +02:00
|
|
|
this.onmessage = function(e) {
|
|
|
|
if (e.data.key == 'ShowAdBlockBanner') {
|
|
|
|
if (adBlockDiv == null) {
|
|
|
|
adBlockDiv = getAdBlockDiv();
|
|
|
|
}
|
2023-06-01 11:33:30 +02:00
|
|
|
adBlockDiv.P.textContent = 'Blocking ads';
|
2022-03-31 17:33:10 +02:00
|
|
|
adBlockDiv.style.display = 'block';
|
|
|
|
} else if (e.data.key == 'HideAdBlockBanner') {
|
|
|
|
if (adBlockDiv == null) {
|
|
|
|
adBlockDiv = getAdBlockDiv();
|
|
|
|
}
|
|
|
|
adBlockDiv.style.display = 'none';
|
|
|
|
} else if (e.data.key == 'PauseResumePlayer') {
|
|
|
|
doTwitchPlayerTask(true, false, false, false, false);
|
|
|
|
} else if (e.data.key == 'ForceChangeQuality') {
|
|
|
|
//This is used to fix the bug where the video would freeze.
|
|
|
|
try {
|
2023-06-01 09:03:45 +02:00
|
|
|
//if (navigator.userAgent.toLowerCase().indexOf('firefox') == -1) {
|
2022-08-27 16:32:21 +02:00
|
|
|
return;
|
2023-06-01 09:03:45 +02:00
|
|
|
//}
|
2022-03-31 17:33:10 +02:00
|
|
|
var autoQuality = doTwitchPlayerTask(false, false, false, true, false);
|
|
|
|
var currentQuality = doTwitchPlayerTask(false, true, false, false, false);
|
|
|
|
if (IsPlayerAutoQuality == null) {
|
|
|
|
IsPlayerAutoQuality = autoQuality;
|
|
|
|
}
|
|
|
|
if (OriginalVideoPlayerQuality == null) {
|
|
|
|
OriginalVideoPlayerQuality = currentQuality;
|
|
|
|
}
|
2023-06-01 10:21:49 +02:00
|
|
|
if (!currentQuality.includes('360') || e.data.value != null) {
|
|
|
|
if (!OriginalVideoPlayerQuality.includes('360')) {
|
2022-03-31 17:33:10 +02:00
|
|
|
var settingsMenu = document.querySelector('div[data-a-target="player-settings-menu"]');
|
|
|
|
if (settingsMenu == null) {
|
|
|
|
var settingsCog = document.querySelector('button[data-a-target="player-settings-button"]');
|
|
|
|
if (settingsCog) {
|
|
|
|
settingsCog.click();
|
|
|
|
var qualityMenu = document.querySelector('button[data-a-target="player-settings-menu-item-quality"]');
|
|
|
|
if (qualityMenu) {
|
|
|
|
qualityMenu.click();
|
|
|
|
}
|
|
|
|
var lowQuality = document.querySelectorAll('input[data-a-target="tw-radio"');
|
|
|
|
if (lowQuality) {
|
2023-06-01 10:21:49 +02:00
|
|
|
var qualityToSelect = lowQuality.length - 2;
|
2022-03-31 17:33:10 +02:00
|
|
|
if (e.data.value != null) {
|
|
|
|
if (e.data.value.includes('original')) {
|
|
|
|
e.data.value = OriginalVideoPlayerQuality;
|
|
|
|
if (IsPlayerAutoQuality) {
|
|
|
|
e.data.value = 'auto';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('160p')) {
|
|
|
|
qualityToSelect = 5;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('360p')) {
|
|
|
|
qualityToSelect = 4;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('480p')) {
|
|
|
|
qualityToSelect = 3;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('720p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('822p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('864p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('900p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('936p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('960p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('1080p')) {
|
|
|
|
qualityToSelect = 2;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('source')) {
|
|
|
|
qualityToSelect = 1;
|
|
|
|
}
|
|
|
|
if (e.data.value.includes('auto')) {
|
|
|
|
qualityToSelect = 0;
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 13:01:50 +02:00
|
|
|
var currentQualityLS = window.localStorage.getItem('video-quality');
|
2022-03-31 17:33:10 +02:00
|
|
|
lowQuality[qualityToSelect].click();
|
2023-06-01 10:21:49 +02:00
|
|
|
settingsCog.click();
|
2022-04-04 13:01:50 +02:00
|
|
|
window.localStorage.setItem('video-quality', currentQualityLS);
|
2022-03-31 17:33:10 +02:00
|
|
|
if (e.data.value != null) {
|
|
|
|
OriginalVideoPlayerQuality = null;
|
|
|
|
IsPlayerAutoQuality = null;
|
|
|
|
doTwitchPlayerTask(false, false, false, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
OriginalVideoPlayerQuality = null;
|
|
|
|
IsPlayerAutoQuality = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function getAdBlockDiv() {
|
|
|
|
//To display a notification to the user, that an ad is being blocked.
|
|
|
|
var playerRootDiv = document.querySelector('.video-player');
|
|
|
|
var adBlockDiv = null;
|
|
|
|
if (playerRootDiv != null) {
|
|
|
|
adBlockDiv = playerRootDiv.querySelector('.adblock-overlay');
|
|
|
|
if (adBlockDiv == null) {
|
|
|
|
adBlockDiv = document.createElement('div');
|
|
|
|
adBlockDiv.className = 'adblock-overlay';
|
|
|
|
adBlockDiv.innerHTML = '<div class="player-adblock-notice" style="color: white; background-color: rgba(0, 0, 0, 0.8); position: absolute; top: 0px; left: 0px; padding: 5px;"><p></p></div>';
|
|
|
|
adBlockDiv.style.display = 'none';
|
|
|
|
adBlockDiv.P = adBlockDiv.querySelector('p');
|
|
|
|
playerRootDiv.appendChild(adBlockDiv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adBlockDiv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function getWasmWorkerUrl(twitchBlobUrl) {
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open('GET', twitchBlobUrl, false);
|
|
|
|
req.send();
|
|
|
|
return req.responseText.split("'")[1];
|
|
|
|
}
|
|
|
|
function hookWorkerFetch() {
|
2024-04-12 13:46:45 +02:00
|
|
|
console.log('hookWorkerFetch');
|
2022-03-31 17:33:10 +02:00
|
|
|
var realFetch = fetch;
|
|
|
|
fetch = async function(url, options) {
|
|
|
|
if (typeof url === 'string') {
|
2024-04-25 22:51:37 +02:00
|
|
|
if (url.endsWith('m3u8')) {
|
2022-03-31 17:33:10 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var processAfter = async function(response) {
|
|
|
|
//Here we check the m3u8 for any ads and also try fallback player types if needed.
|
|
|
|
var responseText = await response.text();
|
|
|
|
var weaverText = null;
|
|
|
|
weaverText = await processM3U8(url, responseText, realFetch, PlayerType2);
|
|
|
|
if (weaverText.includes(AdSignifier)) {
|
|
|
|
weaverText = await processM3U8(url, responseText, realFetch, PlayerType3);
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
if (weaverText.includes(AdSignifier)) {
|
|
|
|
weaverText = await processM3U8(url, responseText, realFetch, PlayerType4);
|
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
resolve(new Response(weaverText));
|
|
|
|
};
|
|
|
|
var send = function() {
|
|
|
|
return realFetch(url, options).then(function(response) {
|
|
|
|
processAfter(response);
|
|
|
|
})['catch'](function(err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
send();
|
|
|
|
});
|
|
|
|
} else if (url.includes('/api/channel/hls/')) {
|
|
|
|
var channelName = (new URL(url)).pathname.match(/([^\/]+)(?=\.\w+$)/)[0];
|
|
|
|
UsherParams = (new URL(url)).search;
|
|
|
|
CurrentChannelName = channelName;
|
|
|
|
//To prevent pause/resume loop for mid-rolls.
|
|
|
|
var isPBYPRequest = url.includes('picture-by-picture');
|
|
|
|
if (isPBYPRequest) {
|
|
|
|
url = '';
|
|
|
|
}
|
2022-08-27 16:32:21 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var processAfter = async function(response) {
|
2022-08-28 20:10:29 +02:00
|
|
|
if (response.status == 200) {
|
|
|
|
encodingsM3u8 = await response.text();
|
|
|
|
var streamInfo = StreamInfos[channelName];
|
|
|
|
if (streamInfo == null) {
|
|
|
|
StreamInfos[channelName] = streamInfo = {};
|
|
|
|
}
|
|
|
|
streamInfo.ChannelName = channelName;
|
2023-10-11 12:08:30 +02:00
|
|
|
streamInfo.RequestedAds = new Set();
|
2022-08-28 20:10:29 +02:00
|
|
|
streamInfo.Urls = [];// xxx.m3u8 -> { Resolution: "284x160", FrameRate: 30.0 }
|
|
|
|
streamInfo.EncodingsM3U8Cache = [];
|
|
|
|
streamInfo.EncodingsM3U8 = encodingsM3u8;
|
|
|
|
var lines = encodingsM3u8.replace('\r', '').split('\n');
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
if (!lines[i].startsWith('#') && lines[i].includes('.m3u8')) {
|
|
|
|
streamInfo.Urls[lines[i]] = -1;
|
|
|
|
if (i > 0 && lines[i - 1].startsWith('#EXT-X-STREAM-INF')) {
|
|
|
|
var attributes = parseAttributes(lines[i - 1]);
|
|
|
|
var resolution = attributes['RESOLUTION'];
|
|
|
|
var frameRate = attributes['FRAME-RATE'];
|
|
|
|
if (resolution) {
|
|
|
|
streamInfo.Urls[lines[i]] = {
|
|
|
|
Resolution: resolution,
|
|
|
|
FrameRate: frameRate
|
|
|
|
};
|
|
|
|
}
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
2022-08-28 20:10:29 +02:00
|
|
|
StreamInfosByUrl[lines[i]] = streamInfo;
|
|
|
|
MainUrlByUrl[lines[i]] = url;
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
2022-08-28 20:10:29 +02:00
|
|
|
resolve(new Response(encodingsM3u8));
|
|
|
|
} else {
|
|
|
|
resolve(response);
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
var send = function() {
|
|
|
|
return realFetch(url, options).then(function(response) {
|
|
|
|
processAfter(response);
|
|
|
|
})['catch'](function(err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
send();
|
|
|
|
});
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return realFetch.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
2022-08-28 20:10:29 +02:00
|
|
|
function getStreamUrlForResolution(encodingsM3u8, resolutionInfo, qualityOverrideStr) {
|
|
|
|
var qualityOverride = 0;
|
|
|
|
if (qualityOverrideStr && qualityOverrideStr.endsWith('p')) {
|
|
|
|
qualityOverride = qualityOverrideStr.substr(0, qualityOverrideStr.length - 1) | 0;
|
|
|
|
}
|
|
|
|
var qualityOverrideFoundQuality = 0;
|
|
|
|
var qualityOverrideFoundFrameRate = 0;
|
2022-08-27 16:32:21 +02:00
|
|
|
var encodingsLines = encodingsM3u8.replace('\r', '').split('\n');
|
|
|
|
var firstUrl = null;
|
2022-08-28 20:10:29 +02:00
|
|
|
var lastUrl = null;
|
|
|
|
var matchedResolutionUrl = null;
|
|
|
|
var matchedFrameRate = false;
|
2022-08-27 16:32:21 +02:00
|
|
|
for (var i = 0; i < encodingsLines.length; i++) {
|
|
|
|
if (!encodingsLines[i].startsWith('#') && encodingsLines[i].includes('.m3u8')) {
|
|
|
|
if (i > 0 && encodingsLines[i - 1].startsWith('#EXT-X-STREAM-INF')) {
|
2022-08-28 20:10:29 +02:00
|
|
|
var attributes = parseAttributes(encodingsLines[i - 1]);
|
|
|
|
var resolution = attributes['RESOLUTION'];
|
|
|
|
var frameRate = attributes['FRAME-RATE'];
|
|
|
|
if (resolution) {
|
|
|
|
if (qualityOverride) {
|
|
|
|
var quality = resolution.toLowerCase().split('x')[1];
|
|
|
|
if (quality == qualityOverride) {
|
|
|
|
qualityOverrideFoundQuality = quality;
|
|
|
|
qualityOverrideFoundFrameRate = frameRate;
|
|
|
|
matchedResolutionUrl = encodingsLines[i];
|
|
|
|
if (frameRate < 40) {
|
|
|
|
//console.log(`qualityOverride(A) quality:${quality} frameRate:${frameRate}`);
|
|
|
|
return matchedResolutionUrl;
|
|
|
|
}
|
|
|
|
} else if (quality < qualityOverride) {
|
|
|
|
//if (matchedResolutionUrl) {
|
|
|
|
// console.log(`qualityOverride(B) quality:${qualityOverrideFoundQuality} frameRate:${qualityOverrideFoundFrameRate}`);
|
|
|
|
//} else {
|
|
|
|
// console.log(`qualityOverride(C) quality:${quality} frameRate:${frameRate}`);
|
|
|
|
//}
|
|
|
|
return matchedResolutionUrl ? matchedResolutionUrl : encodingsLines[i];
|
|
|
|
}
|
|
|
|
} else if ((!resolutionInfo || resolution == resolutionInfo.Resolution) &&
|
|
|
|
(!matchedResolutionUrl || (!matchedFrameRate && frameRate == resolutionInfo.FrameRate))) {
|
|
|
|
matchedResolutionUrl = encodingsLines[i];
|
|
|
|
matchedFrameRate = frameRate == resolutionInfo.FrameRate;
|
|
|
|
if (matchedFrameRate) {
|
|
|
|
return matchedResolutionUrl;
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
|
|
|
if (firstUrl == null) {
|
|
|
|
firstUrl = encodingsLines[i];
|
|
|
|
}
|
2022-08-28 20:10:29 +02:00
|
|
|
lastUrl = encodingsLines[i];
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-28 20:10:29 +02:00
|
|
|
if (qualityOverride) {
|
|
|
|
return lastUrl;
|
|
|
|
}
|
|
|
|
return matchedResolutionUrl ? matchedResolutionUrl : firstUrl;
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
2022-08-28 20:10:29 +02:00
|
|
|
async function getStreamForResolution(streamInfo, resolutionInfo, encodingsM3u8, fallbackStreamStr, playerType, realFetch) {
|
|
|
|
var qualityOverride = null;
|
|
|
|
if (streamInfo.EncodingsM3U8Cache[playerType].Resolution != resolutionInfo.Resolution ||
|
2022-08-27 16:32:21 +02:00
|
|
|
streamInfo.EncodingsM3U8Cache[playerType].RequestTime < Date.now() - EncodingCacheTimeout) {
|
2022-08-28 20:10:29 +02:00
|
|
|
console.log(`Blocking ads (type:${playerType}, resolution:${resolutionInfo.Resolution}, frameRate:${resolutionInfo.FrameRate}, qualityOverride:${qualityOverride})`);
|
2022-08-27 16:32:21 +02:00
|
|
|
}
|
|
|
|
streamInfo.EncodingsM3U8Cache[playerType].RequestTime = Date.now();
|
|
|
|
streamInfo.EncodingsM3U8Cache[playerType].Value = encodingsM3u8;
|
2022-08-28 20:10:29 +02:00
|
|
|
streamInfo.EncodingsM3U8Cache[playerType].Resolution = resolutionInfo.Resolution;
|
|
|
|
var streamM3u8Url = getStreamUrlForResolution(encodingsM3u8, resolutionInfo, qualityOverride);
|
2022-08-27 16:32:21 +02:00
|
|
|
var streamM3u8Response = await realFetch(streamM3u8Url);
|
|
|
|
if (streamM3u8Response.status == 200) {
|
|
|
|
var m3u8Text = await streamM3u8Response.text();
|
|
|
|
WasShowingAd = true;
|
2022-08-28 20:10:29 +02:00
|
|
|
postMessage({
|
|
|
|
key: 'ShowAdBlockBanner'
|
|
|
|
});
|
2022-08-27 16:32:21 +02:00
|
|
|
postMessage({
|
|
|
|
key: 'ForceChangeQuality'
|
|
|
|
});
|
|
|
|
if (!m3u8Text || m3u8Text.includes(AdSignifier)) {
|
|
|
|
streamInfo.EncodingsM3U8Cache[playerType].Value = null;
|
|
|
|
}
|
|
|
|
return m3u8Text;
|
2022-03-31 17:33:10 +02:00
|
|
|
} else {
|
2022-08-27 16:32:21 +02:00
|
|
|
streamInfo.EncodingsM3U8Cache[playerType].Value = null;
|
|
|
|
return fallbackStreamStr;
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-27 16:32:21 +02:00
|
|
|
function stripUnusedParams(str, params) {
|
|
|
|
if (!params) {
|
|
|
|
params = [ 'token', 'sig' ];
|
|
|
|
}
|
|
|
|
var tempUrl = new URL('https://localhost/' + str);
|
|
|
|
for (var i = 0; i < params.length; i++) {
|
|
|
|
tempUrl.searchParams.delete(params[i]);
|
|
|
|
}
|
|
|
|
return tempUrl.pathname.substring(1) + tempUrl.search;
|
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
async function processM3U8(url, textStr, realFetch, playerType) {
|
|
|
|
//Checks the m3u8 for ads and if it finds one, instead returns an ad-free stream.
|
2022-08-27 16:32:21 +02:00
|
|
|
var streamInfo = StreamInfosByUrl[url];
|
2022-03-31 17:33:10 +02:00
|
|
|
//Ad blocking for squad streams is disabled due to the way multiple weaver urls are used. No workaround so far.
|
|
|
|
if (IsSquadStream == true) {
|
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
if (!textStr) {
|
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
//Some live streams use mp4.
|
2022-08-28 20:10:29 +02:00
|
|
|
if (!textStr.includes('.ts') && !textStr.includes('.mp4')) {
|
2022-03-31 17:33:10 +02:00
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
var haveAdTags = textStr.includes(AdSignifier);
|
|
|
|
if (haveAdTags) {
|
2022-08-27 16:32:21 +02:00
|
|
|
var isMidroll = textStr.includes('"MIDROLL"') || textStr.includes('"midroll"');
|
|
|
|
//Reduces ad frequency. TODO: Reduce the number of requests. This is really spamming Twitch with requests.
|
|
|
|
if (!isMidroll) {
|
2023-10-11 12:08:30 +02:00
|
|
|
if (playerType === PlayerType2) {
|
|
|
|
var lines = textStr.replace('\r', '').split('\n');
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
var line = lines[i];
|
|
|
|
if (line.startsWith('#EXTINF') && lines.length > i + 1) {
|
|
|
|
if (!line.includes(',live') && !streamInfo.RequestedAds.has(lines[i + 1])) {
|
|
|
|
// Only request one .ts file per .m3u8 request to avoid making too many requests
|
|
|
|
//console.log('Fetch ad .ts file');
|
|
|
|
streamInfo.RequestedAds.add(lines[i + 1]);
|
|
|
|
fetch(lines[i + 1]).then((response)=>{response.blob()});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 16:32:21 +02:00
|
|
|
try {
|
2022-08-28 20:10:29 +02:00
|
|
|
//tryNotifyTwitch(textStr);
|
2022-08-27 16:32:21 +02:00
|
|
|
} catch (err) {}
|
|
|
|
}
|
|
|
|
var currentResolution = null;
|
|
|
|
if (streamInfo && streamInfo.Urls) {
|
2022-08-28 20:10:29 +02:00
|
|
|
for (const [resUrl, resInfo] of Object.entries(streamInfo.Urls)) {
|
2022-08-27 16:32:21 +02:00
|
|
|
if (resUrl == url) {
|
2022-08-28 20:10:29 +02:00
|
|
|
currentResolution = resInfo;
|
|
|
|
//console.log(resInfo.Resolution);
|
2022-08-27 16:32:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Keep the m3u8 around for a little while (once per ad) before requesting a new one
|
|
|
|
var encodingsM3U8Cache = streamInfo.EncodingsM3U8Cache[playerType];
|
|
|
|
if (encodingsM3U8Cache) {
|
|
|
|
if (encodingsM3U8Cache.Value && encodingsM3U8Cache.RequestTime >= Date.now() - EncodingCacheTimeout) {
|
|
|
|
try {
|
|
|
|
var result = getStreamForResolution(streamInfo, currentResolution, encodingsM3U8Cache.Value, null, playerType, realFetch);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
encodingsM3U8Cache.Value = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
streamInfo.EncodingsM3U8Cache[playerType] = {
|
|
|
|
RequestTime: Date.now(),
|
|
|
|
Value: null,
|
|
|
|
Resolution: null
|
|
|
|
};
|
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
var accessTokenResponse = await getAccessToken(CurrentChannelName, playerType);
|
|
|
|
if (accessTokenResponse.status === 200) {
|
|
|
|
var accessToken = await accessTokenResponse.json();
|
|
|
|
try {
|
|
|
|
var urlInfo = new URL('https://usher.ttvnw.net/api/channel/hls/' + CurrentChannelName + '.m3u8' + UsherParams);
|
|
|
|
urlInfo.searchParams.set('sig', accessToken.data.streamPlaybackAccessToken.signature);
|
|
|
|
urlInfo.searchParams.set('token', accessToken.data.streamPlaybackAccessToken.value);
|
|
|
|
var encodingsM3u8Response = await realFetch(urlInfo.href);
|
|
|
|
if (encodingsM3u8Response.status === 200) {
|
2022-08-27 16:32:21 +02:00
|
|
|
return getStreamForResolution(streamInfo, currentResolution, await encodingsM3u8Response.text(), textStr, playerType, realFetch);
|
2022-03-31 17:33:10 +02:00
|
|
|
} else {
|
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
return textStr;
|
|
|
|
} else {
|
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (WasShowingAd) {
|
2022-08-28 20:10:29 +02:00
|
|
|
console.log('Finished blocking ads');
|
2022-03-31 17:33:10 +02:00
|
|
|
WasShowingAd = false;
|
|
|
|
//Here we put player back to original quality and remove the blocking message.
|
|
|
|
postMessage({
|
|
|
|
key: 'ForceChangeQuality',
|
|
|
|
value: 'original'
|
|
|
|
});
|
|
|
|
postMessage({
|
|
|
|
key: 'PauseResumePlayer'
|
|
|
|
});
|
|
|
|
postMessage({
|
|
|
|
key: 'HideAdBlockBanner'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
return textStr;
|
|
|
|
}
|
|
|
|
function parseAttributes(str) {
|
|
|
|
return Object.fromEntries(
|
|
|
|
str.split(/(?:^|,)((?:[^=]*)=(?:"[^"]*"|[^,]*))/)
|
|
|
|
.filter(Boolean)
|
|
|
|
.map(x => {
|
|
|
|
const idx = x.indexOf('=');
|
|
|
|
const key = x.substring(0, idx);
|
|
|
|
const value = x.substring(idx + 1);
|
|
|
|
const num = Number(value);
|
|
|
|
return [key, Number.isNaN(num) ? value.startsWith('"') ? JSON.parse(value) : value : num];
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
async function tryNotifyTwitch(streamM3u8) {
|
|
|
|
//We notify that an ad was requested but was not visible and was also muted.
|
|
|
|
var matches = streamM3u8.match(/#EXT-X-DATERANGE:(ID="stitched-ad-[^\n]+)\n/);
|
|
|
|
if (matches.length > 1) {
|
|
|
|
const attrString = matches[1];
|
|
|
|
const attr = parseAttributes(attrString);
|
|
|
|
var podLength = parseInt(attr['X-TV-TWITCH-AD-POD-LENGTH'] ? attr['X-TV-TWITCH-AD-POD-LENGTH'] : '1');
|
|
|
|
var podPosition = parseInt(attr['X-TV-TWITCH-AD-POD-POSITION'] ? attr['X-TV-TWITCH-AD-POD-POSITION'] : '0');
|
|
|
|
var radToken = attr['X-TV-TWITCH-AD-RADS-TOKEN'];
|
|
|
|
var lineItemId = attr['X-TV-TWITCH-AD-LINE-ITEM-ID'];
|
|
|
|
var orderId = attr['X-TV-TWITCH-AD-ORDER-ID'];
|
|
|
|
var creativeId = attr['X-TV-TWITCH-AD-CREATIVE-ID'];
|
|
|
|
var adId = attr['X-TV-TWITCH-AD-ADVERTISER-ID'];
|
|
|
|
var rollType = attr['X-TV-TWITCH-AD-ROLL-TYPE'].toLowerCase();
|
|
|
|
const baseData = {
|
|
|
|
stitched: true,
|
|
|
|
roll_type: rollType,
|
|
|
|
player_mute: true,
|
|
|
|
player_volume: 0.0,
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
for (let podPosition = 0; podPosition < podLength; podPosition++) {
|
|
|
|
const extendedData = {
|
|
|
|
...baseData,
|
|
|
|
ad_id: adId,
|
|
|
|
ad_position: podPosition,
|
|
|
|
duration: 0,
|
|
|
|
creative_id: creativeId,
|
|
|
|
total_ads: podLength,
|
|
|
|
order_id: orderId,
|
|
|
|
line_item_id: lineItemId,
|
|
|
|
};
|
|
|
|
await gqlRequest(adRecordgqlPacket('video_ad_impression', radToken, extendedData));
|
|
|
|
for (let quartile = 0; quartile < 4; quartile++) {
|
|
|
|
await gqlRequest(
|
|
|
|
adRecordgqlPacket('video_ad_quartile_complete', radToken, {
|
|
|
|
...extendedData,
|
|
|
|
quartile: quartile + 1,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await gqlRequest(adRecordgqlPacket('video_ad_pod_complete', radToken, baseData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function adRecordgqlPacket(event, radToken, payload) {
|
|
|
|
return [{
|
|
|
|
operationName: 'ClientSideAdEventHandling_RecordAdEvent',
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
eventName: event,
|
|
|
|
eventPayload: JSON.stringify(payload),
|
|
|
|
radToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
extensions: {
|
|
|
|
persistedQuery: {
|
|
|
|
version: 1,
|
|
|
|
sha256Hash: '7e6c69e6eb59f8ccb97ab73686f3d8b7d85a72a0298745ccd8bfc68e4054ca5b',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
function getAccessToken(channelName, playerType, realFetch) {
|
|
|
|
var body = null;
|
2023-06-01 10:21:49 +02:00
|
|
|
var templateQuery = 'query PlaybackAccessToken_Template($login: String!, $isLive: Boolean!, $vodID: ID!, $isVod: Boolean!, $playerType: String!) { streamPlaybackAccessToken(channelName: $login, params: {platform: "ios", playerBackend: "mediaplayer", playerType: $playerType}) @include(if: $isLive) { value signature __typename } videoPlaybackAccessToken(id: $vodID, params: {platform: "ios", playerBackend: "mediaplayer", playerType: $playerType}) @include(if: $isVod) { value signature __typename }}';
|
2022-03-31 17:33:10 +02:00
|
|
|
body = {
|
|
|
|
operationName: 'PlaybackAccessToken_Template',
|
|
|
|
query: templateQuery,
|
|
|
|
variables: {
|
|
|
|
'isLive': true,
|
|
|
|
'login': channelName,
|
|
|
|
'isVod': false,
|
|
|
|
'vodID': '',
|
|
|
|
'playerType': playerType
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return gqlRequest(body, realFetch);
|
|
|
|
}
|
|
|
|
function gqlRequest(body, realFetch) {
|
2023-06-01 11:33:30 +02:00
|
|
|
if (ClientIntegrityHeader == null) {
|
2023-10-11 12:08:30 +02:00
|
|
|
//console.warn('ClientIntegrityHeader is null');
|
2023-06-02 00:16:23 +02:00
|
|
|
//throw 'ClientIntegrityHeader is null';
|
2023-06-01 09:03:45 +02:00
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
var fetchFunc = realFetch ? realFetch : fetch;
|
|
|
|
if (!GQLDeviceID) {
|
|
|
|
var dcharacters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
var dcharactersLength = dcharacters.length;
|
|
|
|
for (var i = 0; i < 32; i++) {
|
|
|
|
GQLDeviceID += dcharacters.charAt(Math.floor(Math.random() * dcharactersLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fetchFunc('https://gql.twitch.tv/gql', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
headers: {
|
|
|
|
'Client-ID': ClientID,
|
2023-06-01 11:33:30 +02:00
|
|
|
'Client-Integrity': ClientIntegrityHeader,
|
2022-03-31 17:33:10 +02:00
|
|
|
'Device-ID': GQLDeviceID,
|
|
|
|
'X-Device-Id': GQLDeviceID,
|
|
|
|
'Client-Version': ClientVersion,
|
2023-06-01 11:33:30 +02:00
|
|
|
'Client-Session-Id': ClientSession,
|
|
|
|
'Authorization': AuthorizationHeader
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function doTwitchPlayerTask(isPausePlay, isCheckQuality, isCorrectBuffer, isAutoQuality, setAutoQuality) {
|
|
|
|
//This will do an instant pause/play to return to original quality once the ad is finished.
|
|
|
|
//We also use this function to get the current video player quality set by the user.
|
|
|
|
//We also use this function to quickly pause/play the player when switching tabs to stop delays.
|
|
|
|
try {
|
|
|
|
var videoController = null;
|
|
|
|
var videoPlayer = null;
|
|
|
|
function findReactNode(root, constraint) {
|
|
|
|
if (root.stateNode && constraint(root.stateNode)) {
|
|
|
|
return root.stateNode;
|
|
|
|
}
|
|
|
|
let node = root.child;
|
|
|
|
while (node) {
|
|
|
|
const result = findReactNode(node, constraint);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
node = node.sibling;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2024-02-23 12:23:33 +01:00
|
|
|
function findReactRootNode() {
|
|
|
|
var reactRootNode = null;
|
|
|
|
var rootNode = document.querySelector('#root');
|
|
|
|
if (rootNode && rootNode._reactRootContainer && rootNode._reactRootContainer._internalRoot && rootNode._reactRootContainer._internalRoot.current) {
|
|
|
|
reactRootNode = rootNode._reactRootContainer._internalRoot.current;
|
|
|
|
}
|
|
|
|
if (reactRootNode == null) {
|
|
|
|
var containerName = Object.keys(rootNode).find(x => x.startsWith('__reactContainer'));
|
|
|
|
if (containerName != null) {
|
|
|
|
reactRootNode = rootNode[containerName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reactRootNode;
|
|
|
|
}
|
|
|
|
var reactRootNode = findReactRootNode();
|
|
|
|
if (!reactRootNode) {
|
|
|
|
console.log('Could not find react root');
|
|
|
|
return;
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
videoPlayer = findReactNode(reactRootNode, node => node.setPlayerActive && node.props && node.props.mediaPlayerInstance);
|
|
|
|
videoPlayer = videoPlayer && videoPlayer.props && videoPlayer.props.mediaPlayerInstance ? videoPlayer.props.mediaPlayerInstance : null;
|
|
|
|
if (isPausePlay) {
|
|
|
|
videoPlayer.pause();
|
|
|
|
videoPlayer.play();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isCheckQuality) {
|
|
|
|
if (typeof videoPlayer.getQuality() == 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var playerQuality = JSON.stringify(videoPlayer.getQuality());
|
|
|
|
if (playerQuality) {
|
|
|
|
return playerQuality;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isAutoQuality) {
|
|
|
|
if (typeof videoPlayer.isAutoQualityMode() == 'undefined') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var autoQuality = videoPlayer.isAutoQualityMode();
|
|
|
|
if (autoQuality) {
|
|
|
|
videoPlayer.setAutoQualityMode(false);
|
|
|
|
return autoQuality;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (setAutoQuality) {
|
|
|
|
videoPlayer.setAutoQualityMode(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//This only happens when switching tabs and is to correct the high latency caused when opening background tabs and going to them at a later time.
|
|
|
|
//We check that this is a live stream by the page URL, to prevent vod/clip pause/plays.
|
|
|
|
try {
|
|
|
|
var currentPageURL = document.URL;
|
|
|
|
var isLive = true;
|
|
|
|
if (currentPageURL.includes('videos/') || currentPageURL.includes('clip/')) {
|
|
|
|
isLive = false;
|
|
|
|
}
|
|
|
|
if (isCorrectBuffer && isLive) {
|
|
|
|
//A timer is needed due to the player not resuming without it.
|
|
|
|
setTimeout(function() {
|
|
|
|
//If latency to broadcaster is above 5 or 15 seconds upon switching tabs, we pause and play the player to reset the latency.
|
|
|
|
//If latency is between 0-6, user can manually pause and resume to reset latency further.
|
|
|
|
if (videoPlayer.isLiveLowLatency() && videoPlayer.getLiveLatency() > 5) {
|
|
|
|
videoPlayer.pause();
|
|
|
|
videoPlayer.play();
|
|
|
|
} else if (videoPlayer.getLiveLatency() > 15) {
|
|
|
|
videoPlayer.pause();
|
|
|
|
videoPlayer.play();
|
|
|
|
}
|
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
} catch (err) {}
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
window.reloadTwitchPlayer = doTwitchPlayerTask;
|
2022-03-31 17:33:10 +02:00
|
|
|
var localDeviceID = null;
|
|
|
|
localDeviceID = window.localStorage.getItem('local_copy_unique_id');
|
2024-04-12 13:46:45 +02:00
|
|
|
function postTwitchWorkerMessage(key, value) {
|
|
|
|
twitchWorkers.forEach((worker) => {
|
|
|
|
worker.postMessage({key: key, value: value});
|
|
|
|
});
|
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
function hookFetch() {
|
|
|
|
var realFetch = window.fetch;
|
|
|
|
window.fetch = function(url, init, ...args) {
|
|
|
|
if (typeof url === 'string') {
|
|
|
|
//Check if squad stream.
|
|
|
|
if (window.location.pathname.includes('/squad')) {
|
2024-04-12 13:46:45 +02:00
|
|
|
postTwitchWorkerMessage('UpdateIsSquadStream', true);
|
2022-03-31 17:33:10 +02:00
|
|
|
} else {
|
2024-04-12 13:46:45 +02:00
|
|
|
postTwitchWorkerMessage('UpdateIsSquadStream', false);
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
if (url.includes('/access_token') || url.includes('gql')) {
|
|
|
|
//Device ID is used when notifying Twitch of ads.
|
|
|
|
var deviceId = init.headers['X-Device-Id'];
|
|
|
|
if (typeof deviceId !== 'string') {
|
|
|
|
deviceId = init.headers['Device-ID'];
|
|
|
|
}
|
|
|
|
//Added to prevent eventual UBlock conflicts.
|
|
|
|
if (typeof deviceId === 'string' && !deviceId.includes('twitch-web-wall-mason')) {
|
|
|
|
GQLDeviceID = deviceId;
|
|
|
|
} else if (localDeviceID) {
|
|
|
|
GQLDeviceID = localDeviceID.replace('"', '');
|
|
|
|
GQLDeviceID = GQLDeviceID.replace('"', '');
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
if (GQLDeviceID) {
|
2022-03-31 17:33:10 +02:00
|
|
|
if (typeof init.headers['X-Device-Id'] === 'string') {
|
|
|
|
init.headers['X-Device-Id'] = GQLDeviceID;
|
|
|
|
}
|
|
|
|
if (typeof init.headers['Device-ID'] === 'string') {
|
|
|
|
init.headers['Device-ID'] = GQLDeviceID;
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
postTwitchWorkerMessage('UpdateDeviceId', GQLDeviceID);
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
//Client version is used in GQL requests.
|
|
|
|
var clientVersion = init.headers['Client-Version'];
|
|
|
|
if (clientVersion && typeof clientVersion == 'string') {
|
|
|
|
ClientVersion = clientVersion;
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
if (ClientVersion) {
|
|
|
|
postTwitchWorkerMessage('UpdateClientVersion', ClientVersion);
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
//Client session is used in GQL requests.
|
|
|
|
var clientSession = init.headers['Client-Session-Id'];
|
|
|
|
if (clientSession && typeof clientSession == 'string') {
|
|
|
|
ClientSession = clientSession;
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
if (ClientSession) {
|
|
|
|
postTwitchWorkerMessage('UpdateClientSession', ClientSession);
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
|
|
|
//Client ID is used in GQL requests.
|
|
|
|
if (url.includes('gql') && init && typeof init.body === 'string' && init.body.includes('PlaybackAccessToken')) {
|
|
|
|
var clientId = init.headers['Client-ID'];
|
|
|
|
if (clientId && typeof clientId == 'string') {
|
|
|
|
ClientID = clientId;
|
|
|
|
} else {
|
|
|
|
clientId = init.headers['Client-Id'];
|
|
|
|
if (clientId && typeof clientId == 'string') {
|
|
|
|
ClientID = clientId;
|
|
|
|
}
|
|
|
|
}
|
2024-04-12 13:46:45 +02:00
|
|
|
if (ClientID) {
|
|
|
|
postTwitchWorkerMessage('UpdateClientId', ClientID);
|
2022-03-31 17:33:10 +02:00
|
|
|
}
|
2023-06-01 11:33:30 +02:00
|
|
|
//Client integrity header
|
|
|
|
ClientIntegrityHeader = init.headers['Client-Integrity'];
|
2024-04-12 13:46:45 +02:00
|
|
|
if (ClientIntegrityHeader) {
|
|
|
|
postTwitchWorkerMessage('UpdateClientIntegrityHeader', ClientIntegrityHeader);
|
2023-10-11 12:08:30 +02:00
|
|
|
}
|
2023-06-01 11:33:30 +02:00
|
|
|
//Authorization header
|
|
|
|
AuthorizationHeader = init.headers['Authorization'];
|
2024-04-12 13:46:45 +02:00
|
|
|
if (AuthorizationHeader) {
|
|
|
|
postTwitchWorkerMessage('UpdateAuthorizationHeader', AuthorizationHeader);
|
2023-10-11 12:08:30 +02:00
|
|
|
}
|
2023-06-01 09:03:45 +02:00
|
|
|
}
|
2022-03-31 17:33:10 +02:00
|
|
|
//To prevent pause/resume loop for mid-rolls.
|
|
|
|
if (url.includes('gql') && init && typeof init.body === 'string' && init.body.includes('PlaybackAccessToken') && init.body.includes('picture-by-picture')) {
|
|
|
|
init.body = '';
|
|
|
|
}
|
|
|
|
var isPBYPRequest = url.includes('picture-by-picture');
|
|
|
|
if (isPBYPRequest) {
|
|
|
|
url = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return realFetch.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
hookFetch();
|
2022-04-04 13:01:50 +02:00
|
|
|
})();
|