908 lines
47 KiB
JavaScript
908 lines
47 KiB
JavaScript
twitch-videoad.js text/javascript
|
|
(function() {
|
|
if ( /(^|\.)twitch\.tv$/.test(document.location.hostname) === false ) { return; }
|
|
'use strict';
|
|
var ourTwitchAdSolutionsVersion = 15;// Used to prevent conflicts with outdated versions of the scripts
|
|
if (typeof window.twitchAdSolutionsVersion !== 'undefined' && window.twitchAdSolutionsVersion >= ourTwitchAdSolutionsVersion) {
|
|
console.log("skipping vaft as there's another script active. ourVersion:" + ourTwitchAdSolutionsVersion + " activeVersion:" + window.twitchAdSolutionsVersion);
|
|
window.twitchAdSolutionsVersion = ourTwitchAdSolutionsVersion;
|
|
return;
|
|
}
|
|
window.twitchAdSolutionsVersion = ourTwitchAdSolutionsVersion;
|
|
function declareOptions(scope) {
|
|
scope.AdSignifier = 'stitched';
|
|
scope.ClientID = 'kimne78kx3ncx6brgo4mv6wki5h1ko';
|
|
scope.BackupPlayerTypes = [
|
|
'embed',//Source
|
|
'site',//Source
|
|
'autoplay'//360p
|
|
];
|
|
scope.FallbackPlayerType = 'embed';
|
|
scope.ForceAccessTokenPlayerType = 'site';
|
|
scope.SkipPlayerReloadOnHevc = false;// If true this will skip player reload on streams which have 2k/4k quality (if you enable this and you use the 2k/4k quality setting you'll get error #4000 / #3000 / spinning wheel on chrome based browsers)
|
|
scope.AlwaysReloadPlayerOnAd = false;
|
|
scope.PlayerReloadLowResTime = 1500;
|
|
scope.StreamInfos = [];
|
|
scope.StreamInfosByUrl = [];
|
|
scope.GQLDeviceID = null;
|
|
scope.ClientVersion = null;
|
|
scope.ClientSession = null;
|
|
scope.ClientIntegrityHeader = null;
|
|
scope.AuthorizationHeader = undefined;
|
|
scope.SimulatedAdsDepth = 0;
|
|
scope.IsPlayerBuffering = false;
|
|
scope.LastPausePlay = 0;
|
|
scope.FixPlayerBufferingInsideAds = true;
|
|
scope.FixPlayerBufferingOutsideAds = false;
|
|
scope.DelayBetweenEachPlayerFixBufferAttempt = 3000;
|
|
scope.ActiveStreamInfo = null;
|
|
scope.V2API = false;
|
|
}
|
|
var localStorageHookFailed = false;
|
|
var twitchWorkers = [];
|
|
var adBlockDiv = null;
|
|
var workerStringConflicts = [
|
|
'twitch',
|
|
'isVariantA'// TwitchNoSub
|
|
];
|
|
var workerStringAllow = [];
|
|
var workerStringReinsert = [
|
|
'isVariantA',// TwitchNoSub (prior to (0.9))
|
|
'besuper/',// TwitchNoSub (0.9)
|
|
'${patch_url}'// TwitchNoSub (0.9.1)
|
|
];
|
|
function getCleanWorker(worker) {
|
|
var root = null;
|
|
var parent = null;
|
|
var proto = worker;
|
|
while (proto) {
|
|
var workerString = proto.toString();
|
|
if (workerStringConflicts.some((x) => workerString.includes(x)) && !workerStringAllow.some((x) => workerString.includes(x))) {
|
|
if (parent !== null) {
|
|
Object.setPrototypeOf(parent, Object.getPrototypeOf(proto));
|
|
}
|
|
} else {
|
|
if (root === null) {
|
|
root = proto;
|
|
}
|
|
parent = proto;
|
|
}
|
|
proto = Object.getPrototypeOf(proto);
|
|
}
|
|
return root;
|
|
}
|
|
function getWorkersForReinsert(worker) {
|
|
var result = [];
|
|
var proto = worker;
|
|
while (proto) {
|
|
var workerString = proto.toString();
|
|
if (workerStringReinsert.some((x) => workerString.includes(x))) {
|
|
result.push(proto);
|
|
} else {
|
|
}
|
|
proto = Object.getPrototypeOf(proto);
|
|
}
|
|
return result;
|
|
}
|
|
function reinsertWorkers(worker, reinsert) {
|
|
var parent = worker;
|
|
for (var i = 0; i < reinsert.length; i++) {
|
|
Object.setPrototypeOf(reinsert[i], parent);
|
|
parent = reinsert[i];
|
|
}
|
|
return parent;
|
|
}
|
|
function isValidWorker(worker) {
|
|
var workerString = worker.toString();
|
|
return !workerStringConflicts.some((x) => workerString.includes(x))
|
|
|| workerStringAllow.some((x) => workerString.includes(x))
|
|
|| workerStringReinsert.some((x) => workerString.includes(x));
|
|
}
|
|
function hookWindowWorker() {
|
|
var reinsert = getWorkersForReinsert(window.Worker);
|
|
var newWorker = class Worker extends getCleanWorker(window.Worker) {
|
|
constructor(twitchBlobUrl, options) {
|
|
var isTwitchWorker = false;
|
|
try {
|
|
isTwitchWorker = new URL(twitchBlobUrl).origin.endsWith('.twitch.tv');
|
|
} catch {}
|
|
if (!isTwitchWorker) {
|
|
super(twitchBlobUrl, options);
|
|
return;
|
|
}
|
|
var newBlobStr = `
|
|
const pendingFetchRequests = new Map();
|
|
${getStreamUrlForResolution.toString()}
|
|
${processM3U8.toString()}
|
|
${hookWorkerFetch.toString()}
|
|
${declareOptions.toString()}
|
|
${getAccessToken.toString()}
|
|
${gqlRequest.toString()}
|
|
${parseAttributes.toString()}
|
|
${getWasmWorkerJs.toString()}
|
|
${getServerTimeFromM3u8.toString()}
|
|
${replaceServerTimeInM3u8.toString()}
|
|
${tryFixPlayerBuffering.toString()}
|
|
var workerString = getWasmWorkerJs('${twitchBlobUrl.replaceAll("'", "%27")}');
|
|
declareOptions(self);
|
|
GQLDeviceID = ${GQLDeviceID ? "'" + GQLDeviceID + "'" : null};
|
|
AuthorizationHeader = ${AuthorizationHeader ? "'" + AuthorizationHeader + "'" : undefined};
|
|
ClientIntegrityHeader = ${ClientIntegrityHeader ? "'" + ClientIntegrityHeader + "'" : null};
|
|
ClientVersion = ${ClientVersion ? "'" + ClientVersion + "'" : null};
|
|
ClientSession = ${ClientSession ? "'" + ClientSession + "'" : null};
|
|
self.addEventListener('message', function(e) {
|
|
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;
|
|
} else if (e.data.key == 'FetchResponse') {
|
|
const responseData = e.data.value;
|
|
if (pendingFetchRequests.has(responseData.id)) {
|
|
const { resolve, reject } = pendingFetchRequests.get(responseData.id);
|
|
pendingFetchRequests.delete(responseData.id);
|
|
if (responseData.error) {
|
|
reject(new Error(responseData.error));
|
|
} else {
|
|
// Create a Response object from the response data
|
|
const response = new Response(responseData.body, {
|
|
status: responseData.status,
|
|
statusText: responseData.statusText,
|
|
headers: responseData.headers
|
|
});
|
|
resolve(response);
|
|
}
|
|
}
|
|
} else if (e.data.key == 'SimulateAds') {
|
|
SimulatedAdsDepth = e.data.value;
|
|
console.log('SimulatedAdsDepth:' + SimulatedAdsDepth);
|
|
}
|
|
if (e.data.funcName) {
|
|
if (e.data.funcName == 'onClientSinkBuffering') {
|
|
IsPlayerBuffering = true;
|
|
} else if (e.data.funcName == 'onClientSinkPlaying') {
|
|
IsPlayerBuffering = false;
|
|
LastPausePlay = Date.now();
|
|
} else if (e.data.funcName == 'playIntent' || e.data.funcName == 'play') {
|
|
LastPausePlay = Date.now();
|
|
}
|
|
tryFixPlayerBuffering();
|
|
}
|
|
});
|
|
hookWorkerFetch();
|
|
eval(workerString);
|
|
`;
|
|
super(URL.createObjectURL(new Blob([newBlobStr])), options);
|
|
twitchWorkers.push(this);
|
|
this.addEventListener('message', (e) => {
|
|
if (e.data.key == 'ShowAdBlockBanner') {
|
|
if (adBlockDiv == null) {
|
|
adBlockDiv = getAdBlockDiv();
|
|
}
|
|
if (adBlockDiv != null) {
|
|
adBlockDiv.P.textContent = 'Blocking' + (e.data.isMidroll ? ' midroll' : '') + ' ads';
|
|
adBlockDiv.style.display = 'block';
|
|
}
|
|
} else if (e.data.key == 'HideAdBlockBanner') {
|
|
if (adBlockDiv == null) {
|
|
adBlockDiv = getAdBlockDiv();
|
|
}
|
|
if (adBlockDiv != null) {
|
|
adBlockDiv.style.display = 'none';
|
|
}
|
|
} else if (e.data.key == 'PauseResumePlayer') {
|
|
doTwitchPlayerTask(true, false);
|
|
} else if (e.data.key == 'ReloadPlayer') {
|
|
doTwitchPlayerTask(false, true);
|
|
}
|
|
});
|
|
this.addEventListener('message', async event => {
|
|
if (event.data.key == 'FetchRequest') {
|
|
const fetchRequest = event.data.value;
|
|
const responseData = await handleWorkerFetchRequest(fetchRequest);
|
|
this.postMessage({
|
|
key: 'FetchResponse',
|
|
value: responseData
|
|
});
|
|
}
|
|
});
|
|
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;
|
|
}
|
|
}
|
|
};
|
|
var workerInstance = reinsertWorkers(newWorker, reinsert);
|
|
Object.defineProperty(window, 'Worker', {
|
|
get: function() {
|
|
return workerInstance;
|
|
},
|
|
set: function(value) {
|
|
if (isValidWorker(value)) {
|
|
workerInstance = value;
|
|
} else {
|
|
console.log('Attempt to set twitch worker denied');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
function getWasmWorkerJs(twitchBlobUrl) {
|
|
var req = new XMLHttpRequest();
|
|
req.open('GET', twitchBlobUrl, false);
|
|
req.overrideMimeType("text/javascript");
|
|
req.send();
|
|
return req.responseText;
|
|
}
|
|
function hookWorkerFetch() {
|
|
console.log('hookWorkerFetch (vaft)');
|
|
var realFetch = fetch;
|
|
fetch = async function(url, options) {
|
|
if (typeof url === 'string') {
|
|
url = url.trimEnd();
|
|
if (url.endsWith('m3u8')) {
|
|
return new Promise(function(resolve, reject) {
|
|
var processAfter = async function(response) {
|
|
if (response.status === 200) {
|
|
resolve(new Response(await processM3U8(url, await response.text(), realFetch)));
|
|
} else {
|
|
resolve(response);
|
|
}
|
|
};
|
|
var send = function() {
|
|
return realFetch(url, options).then(function(response) {
|
|
processAfter(response);
|
|
})['catch'](function(err) {
|
|
reject(err);
|
|
});
|
|
};
|
|
send();
|
|
});
|
|
} else if (url.includes('/channel/hls/') && !url.includes('picture-by-picture')) {
|
|
V2API = url.includes('/api/v2/');
|
|
var channelName = (new URL(url)).pathname.match(/([^\/]+)(?=\.\w+$)/)[0];
|
|
if (ForceAccessTokenPlayerType) {
|
|
// parent_domains is used to determine if the player is embeded and stripping it gets rid of fake ads
|
|
var tempUrl = new URL(url);
|
|
tempUrl.searchParams.delete('parent_domains');
|
|
url = tempUrl.toString();
|
|
}
|
|
return new Promise(function(resolve, reject) {
|
|
var processAfter = async function(response) {
|
|
if (response.status == 200) {
|
|
var encodingsM3u8 = await response.text();
|
|
var serverTime = getServerTimeFromM3u8(encodingsM3u8);
|
|
var streamInfo = StreamInfos[channelName];
|
|
if (streamInfo != null && streamInfo.EncodingsM3U8 != null && (await realFetch(streamInfo.EncodingsM3U8.match(/^https:.*\.m3u8$/m)[0])).status !== 200) {
|
|
// The cached encodings are dead (the stream probably restarted)
|
|
streamInfo = null;
|
|
}
|
|
if (streamInfo == null || streamInfo.EncodingsM3U8 == null) {
|
|
StreamInfos[channelName] = streamInfo = {
|
|
ChannelName: channelName,
|
|
IsShowingAd: false,
|
|
AdStartTime: 0,
|
|
EncodingsM3U8: encodingsM3u8,
|
|
ModifiedM3U8: null,
|
|
IsUsingModifiedM3U8: false,
|
|
UsherParams: (new URL(url)).search,
|
|
RequestedAds: new Set(),
|
|
Urls: [],// xxx.m3u8 -> { Resolution: "284x160", FrameRate: 30.0 }
|
|
ResolutionList: [],
|
|
BackupEncodingsM3U8Cache: [],
|
|
ActiveBackupPlayerType: null,
|
|
IsMidroll: false
|
|
};
|
|
var lines = encodingsM3u8.replace('\r', '').split('\n');
|
|
for (var i = 0; i < lines.length - 1; i++) {
|
|
if (lines[i].startsWith('#EXT-X-STREAM-INF') && lines[i + 1].includes('.m3u8')) {
|
|
var attributes = parseAttributes(lines[i]);
|
|
var resolution = attributes['RESOLUTION'];
|
|
if (resolution) {
|
|
var resolutionInfo = {
|
|
Resolution: resolution,
|
|
FrameRate: attributes['FRAME-RATE'],
|
|
Codecs: attributes['CODECS'],
|
|
Url: lines[i + 1]
|
|
};
|
|
streamInfo.Urls[lines[i + 1]] = resolutionInfo;
|
|
streamInfo.ResolutionList.push(resolutionInfo);
|
|
}
|
|
StreamInfosByUrl[lines[i + 1]] = streamInfo;
|
|
}
|
|
}
|
|
var nonHevcResolutionList = streamInfo.ResolutionList.filter((element) => element.Codecs.startsWith('avc') || element.Codecs.startsWith('av0'));
|
|
if (AlwaysReloadPlayerOnAd || (nonHevcResolutionList.length > 0 && streamInfo.ResolutionList.some((element) => element.Codecs.startsWith('hev') || element.Codecs.startsWith('hvc')) && !SkipPlayerReloadOnHevc)) {
|
|
if (nonHevcResolutionList.length > 0) {
|
|
for (var i = 0; i < lines.length - 1; i++) {
|
|
if (lines[i].startsWith('#EXT-X-STREAM-INF')) {
|
|
var resSettings = parseAttributes(lines[i].substring(lines[i].indexOf(':') + 1));
|
|
const codecsKey = 'CODECS';
|
|
if (resSettings[codecsKey].startsWith('hev') || resSettings[codecsKey].startsWith('hvc')) {
|
|
var oldResolution = resSettings['RESOLUTION'];
|
|
const [targetWidth, targetHeight] = oldResolution.split('x').map(Number);
|
|
var newResolutionInfo = nonHevcResolutionList.sort((a, b) => {
|
|
// TODO: Take into account 'Frame-Rate' when sorting (i.e. 1080p60 vs 1080p30)
|
|
const [streamWidthA, streamHeightA] = a.Resolution.split('x').map(Number);
|
|
const [streamWidthB, streamHeightB] = b.Resolution.split('x').map(Number);
|
|
return Math.abs((streamWidthA * streamHeightA) - (targetWidth * targetHeight)) - Math.abs((streamWidthB * streamHeightB) - (targetWidth * targetHeight));
|
|
})[0];
|
|
console.log('ModifiedM3U8 swap ' + resSettings[codecsKey] + ' to ' + newResolutionInfo.Codecs + ' oldRes:' + oldResolution + ' newRes:' + newResolutionInfo.Resolution);
|
|
lines[i] = lines[i].replace(/CODECS="[^"]+"/, `CODECS="${newResolutionInfo.Codecs}"`);
|
|
lines[i + 1] = newResolutionInfo.Url + ' '.repeat(i + 1);// The stream doesn't load unless each url line is unique
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (nonHevcResolutionList.length > 0 || AlwaysReloadPlayerOnAd) {
|
|
streamInfo.ModifiedM3U8 = lines.join('\n');
|
|
var streamM3u8Url = streamInfo.EncodingsM3U8.match(/^https:.*\.m3u8$/m)[0];
|
|
var streamM3u8Response = await realFetch(streamM3u8Url);
|
|
if (streamM3u8Response.status == 200) {
|
|
var streamM3u8 = await streamM3u8Response.text();
|
|
if (streamM3u8.includes(AdSignifier) || SimulatedAdsDepth > 0) {
|
|
streamInfo.IsUsingModifiedM3U8 = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
streamInfo.IsUsingModifiedM3U8 = streamInfo.IsShowingAd && streamInfo.ModifiedM3U8;
|
|
}
|
|
resolve(new Response(replaceServerTimeInM3u8(streamInfo.IsUsingModifiedM3U8 ? streamInfo.ModifiedM3U8 : streamInfo.EncodingsM3U8, serverTime)));
|
|
} else {
|
|
resolve(response);
|
|
}
|
|
};
|
|
var send = function() {
|
|
return realFetch(url, options).then(function(response) {
|
|
processAfter(response);
|
|
})['catch'](function(err) {
|
|
reject(err);
|
|
});
|
|
};
|
|
send();
|
|
});
|
|
}
|
|
}
|
|
return realFetch.apply(this, arguments);
|
|
};
|
|
}
|
|
function getServerTimeFromM3u8(encodingsM3u8) {
|
|
if (V2API) {
|
|
var matches = encodingsM3u8.match(/#EXT-X-SESSION-DATA:DATA-ID="SERVER-TIME",VALUE="([^"]+)"/);
|
|
return matches.length > 1 ? matches[1] : null;
|
|
}
|
|
var matches = encodingsM3u8.match('SERVER-TIME="([0-9.]+)"');
|
|
return matches.length > 1 ? matches[1] : null;
|
|
}
|
|
function replaceServerTimeInM3u8(encodingsM3u8, newServerTime) {
|
|
if (V2API) {
|
|
return newServerTime ? encodingsM3u8.replace(/(#EXT-X-SESSION-DATA:DATA-ID="SERVER-TIME",VALUE=")[^"]+(")/, `$1${newServerTime}$2`) : encodingsM3u8;
|
|
}
|
|
return newServerTime ? encodingsM3u8.replace(new RegExp('(SERVER-TIME=")[0-9.]+"'), `SERVER-TIME="${newServerTime}"`) : encodingsM3u8;
|
|
}
|
|
function getStreamUrlForResolution(encodingsM3u8, resolutionInfo) {
|
|
var encodingsLines = encodingsM3u8.replace('\r', '').split('\n');
|
|
const [targetWidth, targetHeight] = resolutionInfo.Resolution.split('x').map(Number);
|
|
var matchedResolutionUrl = null;
|
|
var matchedFrameRate = false;
|
|
var closestResolutionUrl = null;
|
|
var closestResolutionDifference = Infinity;
|
|
for (var i = 0; i < encodingsLines.length - 1; i++) {
|
|
if (encodingsLines[i].startsWith('#EXT-X-STREAM-INF') && encodingsLines[i + 1].includes('.m3u8')) {
|
|
var attributes = parseAttributes(encodingsLines[i]);
|
|
var resolution = attributes['RESOLUTION'];
|
|
var frameRate = attributes['FRAME-RATE'];
|
|
if (resolution) {
|
|
if (resolution == resolutionInfo.Resolution && (!matchedResolutionUrl || (!matchedFrameRate && frameRate == resolutionInfo.FrameRate))) {
|
|
matchedResolutionUrl = encodingsLines[i + 1];
|
|
matchedFrameRate = frameRate == resolutionInfo.FrameRate;
|
|
if (matchedFrameRate) {
|
|
return matchedResolutionUrl;
|
|
}
|
|
}
|
|
const [width, height] = resolution.split('x').map(Number);
|
|
var difference = Math.abs((width * height) - (targetWidth * targetHeight));
|
|
if (difference < closestResolutionDifference) {
|
|
closestResolutionUrl = encodingsLines[i + 1];
|
|
closestResolutionDifference = difference;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return closestResolutionUrl;
|
|
}
|
|
function tryFixPlayerBuffering() {
|
|
// NOTE: This "ActiveStreamInfo" variable isn't ideal but now that squad streams are removed it should be correct
|
|
if (IsPlayerBuffering && LastPausePlay < Date.now() - DelayBetweenEachPlayerFixBufferAttempt && ActiveStreamInfo &&
|
|
((ActiveStreamInfo.IsShowingAd && FixPlayerBufferingInsideAds) || (!ActiveStreamInfo.IsShowingAd && FixPlayerBufferingOutsideAds))
|
|
) {
|
|
console.log("Attempting to fix player buffering by doing pause/play");
|
|
LastPausePlay = Date.now();
|
|
postMessage({
|
|
key: 'PauseResumePlayer'
|
|
});
|
|
}
|
|
}
|
|
async function processM3U8(url, textStr, realFetch) {
|
|
var streamInfo = StreamInfosByUrl[url];
|
|
if (!streamInfo) {
|
|
return textStr;
|
|
}
|
|
ActiveStreamInfo = streamInfo;
|
|
var haveAdTags = textStr.includes(AdSignifier) || SimulatedAdsDepth > 0;
|
|
if (haveAdTags) {
|
|
streamInfo.IsMidroll = textStr.includes('"MIDROLL"') || textStr.includes('"midroll"');
|
|
if (!streamInfo.IsMidroll) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
streamInfo.Resolution
|
|
var currentResolution = streamInfo.Urls[url];
|
|
if (!currentResolution) {
|
|
console.log('Ads will leak due to missing resolution info for ' + url);
|
|
return textStr;
|
|
}
|
|
if (streamInfo.ModifiedM3U8 && !streamInfo.IsUsingModifiedM3U8) {
|
|
streamInfo.AdStartTime = Date.now();
|
|
}
|
|
if (!streamInfo.IsShowingAd) {
|
|
streamInfo.AdStartTime = Date.now();
|
|
streamInfo.IsShowingAd = true;
|
|
if (streamInfo.ModifiedM3U8 && !streamInfo.IsUsingModifiedM3U8) {
|
|
postMessage({
|
|
key: 'ReloadPlayer'
|
|
});
|
|
}
|
|
postMessage({
|
|
key: 'ShowAdBlockBanner',
|
|
isMidroll: streamInfo.IsMidroll
|
|
});
|
|
}
|
|
var backupPlayerType = null;
|
|
var backupM3u8 = null;
|
|
var fallbackM3u8 = null;
|
|
var startIndex = 0;
|
|
if ((streamInfo.ModifiedM3U8 && !streamInfo.IsUsingModifiedM3U8) ||
|
|
(streamInfo.ModifiedM3U8 && streamInfo.AdStartTime > Date.now() - PlayerReloadLowResTime)
|
|
) {
|
|
// When doing player reload there are a lot of requests which causes the backup stream to load in slow. Briefly prefer using the low res version to prevent long delays
|
|
startIndex = BackupPlayerTypes.length - 1;
|
|
}
|
|
for (var playerTypeIndex = startIndex; !backupM3u8 && playerTypeIndex < BackupPlayerTypes.length; playerTypeIndex++) {
|
|
var playerType = BackupPlayerTypes[playerTypeIndex];
|
|
for (var i = 0; i < 2; i++) {
|
|
// This caches the m3u8 if it doesn't have ads. If the already existing cache has ads it fetches a new version (second loop)
|
|
var isFreshM3u8 = false;
|
|
var encodingsM3u8 = streamInfo.BackupEncodingsM3U8Cache[playerType];
|
|
if (!encodingsM3u8) {
|
|
isFreshM3u8 = true;
|
|
try {
|
|
var accessTokenResponse = await getAccessToken(streamInfo.ChannelName, playerType);
|
|
if (accessTokenResponse.status === 200) {
|
|
var accessToken = await accessTokenResponse.json();
|
|
var urlInfo = new URL('https://usher.ttvnw.net/api/' + (V2API ? 'v2/' : '') + 'channel/hls/' + streamInfo.ChannelName + '.m3u8' + streamInfo.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) {
|
|
encodingsM3u8 = streamInfo.BackupEncodingsM3U8Cache[playerType] = await encodingsM3u8Response.text();
|
|
}
|
|
}
|
|
} catch (err) {}
|
|
}
|
|
if (encodingsM3u8) {
|
|
try {
|
|
var streamM3u8Url = getStreamUrlForResolution(encodingsM3u8, currentResolution);
|
|
var streamM3u8Response = await realFetch(streamM3u8Url);
|
|
if (streamM3u8Response.status == 200) {
|
|
var m3u8Text = await streamM3u8Response.text();
|
|
if (m3u8Text) {
|
|
if (playerType == FallbackPlayerType) {
|
|
fallbackM3u8 = m3u8Text;
|
|
}
|
|
if (!m3u8Text.includes(AdSignifier) && (SimulatedAdsDepth == 0 || playerTypeIndex >= SimulatedAdsDepth - 1)) {
|
|
backupPlayerType = playerType;
|
|
backupM3u8 = m3u8Text;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {}
|
|
}
|
|
if (startIndex != 0) {
|
|
break;
|
|
}
|
|
streamInfo.BackupEncodingsM3U8Cache[playerType] = null;
|
|
if (isFreshM3u8) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (!backupM3u8 && fallbackM3u8) {
|
|
backupPlayerType = FallbackPlayerType;
|
|
backupM3u8 = fallbackM3u8;
|
|
}
|
|
if (backupM3u8) {
|
|
textStr = backupM3u8;
|
|
if (streamInfo.ActiveBackupPlayerType != backupPlayerType) {
|
|
streamInfo.ActiveBackupPlayerType = backupPlayerType;
|
|
console.log(`Blocking${(streamInfo.IsMidroll ? ' midroll ' : ' ')}ads (${backupPlayerType})`);
|
|
}
|
|
}
|
|
} else if (streamInfo.IsShowingAd) {
|
|
console.log('Finished blocking ads');
|
|
streamInfo.IsShowingAd = false;
|
|
streamInfo.ActiveBackupPlayerType = null;
|
|
postMessage({
|
|
key: streamInfo.IsUsingModifiedM3U8 ? 'ReloadPlayer' : 'PauseResumePlayer'
|
|
});
|
|
postMessage({
|
|
key: 'HideAdBlockBanner'
|
|
});
|
|
}
|
|
tryFixPlayerBuffering();
|
|
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];
|
|
}));
|
|
}
|
|
function getAccessToken(channelName, playerType) {
|
|
var body = null;
|
|
var templateQuery = 'query PlaybackAccessToken_Template($login: String!, $isLive: Boolean!, $vodID: ID!, $isVod: Boolean!, $playerType: String!) { streamPlaybackAccessToken(channelName: $login, params: {platform: "android", playerBackend: "mediaplayer", playerType: $playerType}) @include(if: $isLive) { value signature __typename } videoPlaybackAccessToken(id: $vodID, params: {platform: "android", playerBackend: "mediaplayer", playerType: $playerType}) @include(if: $isVod) { value signature __typename }}';
|
|
body = {
|
|
operationName: 'PlaybackAccessToken_Template',
|
|
query: templateQuery,
|
|
variables: {
|
|
'isLive': true,
|
|
'login': channelName,
|
|
'isVod': false,
|
|
'vodID': '',
|
|
'playerType': playerType
|
|
}
|
|
};
|
|
return gqlRequest(body);
|
|
}
|
|
function gqlRequest(body) {
|
|
if (!GQLDeviceID) {
|
|
var dcharacters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
var dcharactersLength = dcharacters.length;
|
|
for (var i = 0; i < 32; i++) {
|
|
GQLDeviceID += dcharacters.charAt(Math.floor(Math.random() * dcharactersLength));
|
|
}
|
|
}
|
|
var headers = {
|
|
'Client-ID': ClientID,
|
|
'Device-ID': GQLDeviceID,
|
|
'X-Device-Id': GQLDeviceID,
|
|
'Authorization': AuthorizationHeader,
|
|
...(ClientIntegrityHeader && {'Client-Integrity': ClientIntegrityHeader}),
|
|
...(ClientVersion && {'Client-Version': ClientVersion}),
|
|
...(ClientSession && {'Client-Session-Id': ClientSession})
|
|
};
|
|
return new Promise((resolve, reject) => {
|
|
const requestId = Math.random().toString(36).substring(2, 15);
|
|
const fetchRequest = {
|
|
id: requestId,
|
|
url: 'https://gql.twitch.tv/gql',
|
|
options: {
|
|
method: 'POST',
|
|
body: JSON.stringify(body),
|
|
headers
|
|
}
|
|
};
|
|
pendingFetchRequests.set(requestId, {
|
|
resolve,
|
|
reject
|
|
});
|
|
postMessage({
|
|
key: 'FetchRequest',
|
|
value: fetchRequest
|
|
});
|
|
});
|
|
}
|
|
function doTwitchPlayerTask(isPausePlay, isReload) {
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
var player = findReactNode(reactRootNode, node => node.setPlayerActive && node.props && node.props.mediaPlayerInstance);
|
|
player = player && player.props && player.props.mediaPlayerInstance ? player.props.mediaPlayerInstance : null;
|
|
var playerState = findReactNode(reactRootNode, node => node.setSrc && node.setInitialPlaybackSettings);
|
|
if (!player) {
|
|
console.log('Could not find player');
|
|
return;
|
|
}
|
|
if (!playerState) {
|
|
console.log('Could not find player state');
|
|
return;
|
|
}
|
|
if (player.paused || player.core?.paused) {
|
|
return;
|
|
}
|
|
if (isPausePlay) {
|
|
player.pause();
|
|
player.play();
|
|
return;
|
|
}
|
|
if (isReload) {
|
|
const lsKeyQuality = 'video-quality';
|
|
const lsKeyMuted = 'video-muted';
|
|
const lsKeyVolume = 'volume';
|
|
var currentQualityLS = localStorage.getItem(lsKeyQuality);
|
|
var currentMutedLS = localStorage.getItem(lsKeyMuted);
|
|
var currentVolumeLS = localStorage.getItem(lsKeyVolume);
|
|
if (localStorageHookFailed && player?.core?.state) {
|
|
localStorage.setItem(lsKeyMuted, JSON.stringify({default:player.core.state.muted}));
|
|
localStorage.setItem(lsKeyVolume, player.core.state.volume);
|
|
}
|
|
if (localStorageHookFailed && player?.core?.state?.quality?.group) {
|
|
localStorage.setItem(lsKeyQuality, JSON.stringify({default:player.core.state.quality.group}));
|
|
}
|
|
playerState.setSrc({ isNewMediaPlayerInstance: true, refreshAccessToken: true });
|
|
player.play();
|
|
if (localStorageHookFailed) {
|
|
setTimeout(() => {
|
|
localStorage.setItem(lsKeyQuality, currentQualityLS);
|
|
localStorage.setItem(lsKeyMuted, currentMutedLS);
|
|
localStorage.setItem(lsKeyVolume, currentVolumeLS);
|
|
}, 3000);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
window.reloadTwitchPlayer = doTwitchPlayerTask;
|
|
function postTwitchWorkerMessage(key, value) {
|
|
twitchWorkers.forEach((worker) => {
|
|
worker.postMessage({key: key, value: value});
|
|
});
|
|
}
|
|
async function handleWorkerFetchRequest(fetchRequest) {
|
|
try {
|
|
const response = await window.realFetch(fetchRequest.url, fetchRequest.options);
|
|
const responseBody = await response.text();
|
|
const responseObject = {
|
|
id: fetchRequest.id,
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers: Object.fromEntries(response.headers.entries()),
|
|
body: responseBody
|
|
};
|
|
return responseObject;
|
|
} catch (error) {
|
|
return {
|
|
id: fetchRequest.id,
|
|
error: error.message
|
|
};
|
|
}
|
|
}
|
|
function hookFetch() {
|
|
var realFetch = window.fetch;
|
|
window.realFetch = realFetch;
|
|
window.fetch = function(url, init, ...args) {
|
|
if (typeof url === 'string') {
|
|
if (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'];
|
|
}
|
|
if (typeof deviceId === 'string' && GQLDeviceID != deviceId) {
|
|
GQLDeviceID = deviceId;
|
|
postTwitchWorkerMessage('UpdateDeviceId', GQLDeviceID);
|
|
}
|
|
if (typeof init.headers['Client-Version'] === 'string' && init.headers['Client-Version'] !== ClientVersion) {
|
|
postTwitchWorkerMessage('UpdateClientVersion', ClientVersion = init.headers['Client-Version']);
|
|
}
|
|
if (typeof init.headers['Client-Session-Id'] === 'string' && init.headers['Client-Session-Id'] !== ClientSession) {
|
|
postTwitchWorkerMessage('UpdateClientSession', ClientSession = init.headers['Client-Session-Id']);
|
|
}
|
|
if (typeof init.headers['Client-Integrity'] === 'string' && init.headers['Client-Integrity'] !== ClientIntegrityHeader) {
|
|
postTwitchWorkerMessage('UpdateClientIntegrityHeader', ClientIntegrityHeader = init.headers['Client-Integrity']);
|
|
}
|
|
if (typeof init.headers['Authorization'] === 'string' && init.headers['Authorization'] !== AuthorizationHeader) {
|
|
postTwitchWorkerMessage('UpdateAuthorizationHeader', AuthorizationHeader = init.headers['Authorization']);
|
|
}
|
|
if (ForceAccessTokenPlayerType && typeof init.body === 'string' && init.body.includes('PlaybackAccessToken') && !init.body.includes('picture-by-picture')) {
|
|
let replacedPlayerType = '';
|
|
const newBody = JSON.parse(init.body);
|
|
if (Array.isArray(newBody)) {
|
|
for (let i = 0; i < newBody.length; i++) {
|
|
if (newBody[i]?.variables?.playerType && newBody[i]?.variables?.playerType !== ForceAccessTokenPlayerType) {
|
|
replacedPlayerType = newBody[i].variables.playerType;
|
|
newBody[i].variables.playerType = ForceAccessTokenPlayerType;
|
|
}
|
|
}
|
|
} else {
|
|
if (newBody?.variables?.playerType && newBody?.variables?.playerType !== ForceAccessTokenPlayerType) {
|
|
replacedPlayerType = newBody.variables.playerType;
|
|
newBody.variables.playerType = ForceAccessTokenPlayerType;
|
|
}
|
|
}
|
|
if (replacedPlayerType) {
|
|
console.log(`Replaced '${replacedPlayerType}' player type with '${ForceAccessTokenPlayerType}' player type`);
|
|
init.body = JSON.stringify(newBody);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return realFetch.apply(this, arguments);
|
|
};
|
|
}
|
|
function onContentLoaded() {
|
|
// This stops Twitch from pausing the player when in another tab and an ad shows.
|
|
// Taken from https://github.com/saucettv/VideoAdBlockForTwitch/blob/cefce9d2b565769c77e3666ac8234c3acfe20d83/chrome/content.js#L30
|
|
try {
|
|
Object.defineProperty(document, 'visibilityState', {
|
|
get() {
|
|
return 'visible';
|
|
}
|
|
});
|
|
}catch{}
|
|
let hidden = document.__lookupGetter__('hidden');
|
|
let webkitHidden = document.__lookupGetter__('webkitHidden');
|
|
try {
|
|
Object.defineProperty(document, 'hidden', {
|
|
get() {
|
|
return false;
|
|
}
|
|
});
|
|
}catch{}
|
|
var block = e => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
e.stopImmediatePropagation();
|
|
};
|
|
let wasVideoPlaying = true;
|
|
var visibilityChange = e => {
|
|
if (typeof chrome !== 'undefined') {
|
|
const videos = document.getElementsByTagName('video');
|
|
if (videos.length > 0) {
|
|
if (hidden.apply(document) === true || (webkitHidden && webkitHidden.apply(document) === true)) {
|
|
wasVideoPlaying = !videos[0].paused && !videos[0].ended;
|
|
} else if (wasVideoPlaying && !videos[0].ended && videos[0].paused && videos[0].muted) {
|
|
videos[0].play();
|
|
}
|
|
}
|
|
}
|
|
block(e);
|
|
};
|
|
document.addEventListener('visibilitychange', visibilityChange, true);
|
|
document.addEventListener('webkitvisibilitychange', visibilityChange, true);
|
|
document.addEventListener('mozvisibilitychange', visibilityChange, true);
|
|
document.addEventListener('hasFocus', block, true);
|
|
try {
|
|
if (/Firefox/.test(navigator.userAgent)) {
|
|
Object.defineProperty(document, 'mozHidden', {
|
|
get() {
|
|
return false;
|
|
}
|
|
});
|
|
} else {
|
|
Object.defineProperty(document, 'webkitHidden', {
|
|
get() {
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
}catch{}
|
|
// Hooks for preserving volume / resolution
|
|
var keysToCache = [
|
|
'video-quality',
|
|
'video-muted',
|
|
'volume',
|
|
'lowLatencyModeEnabled',// Low Latency
|
|
'persistenceEnabled',// Mini Player
|
|
];
|
|
var cachedValues = new Map();
|
|
for (var i = 0; i < keysToCache.length; i++) {
|
|
cachedValues.set(keysToCache[i], localStorage.getItem(keysToCache[i]));
|
|
}
|
|
var realSetItem = localStorage.setItem;
|
|
localStorage.setItem = function(key, value) {
|
|
if (cachedValues.has(key)) {
|
|
cachedValues.set(key, value);
|
|
}
|
|
realSetItem.apply(this, arguments);
|
|
};
|
|
var realGetItem = localStorage.getItem;
|
|
localStorage.getItem = function(key) {
|
|
if (cachedValues.has(key)) {
|
|
return cachedValues.get(key);
|
|
}
|
|
return realGetItem.apply(this, arguments);
|
|
};
|
|
if (!localStorage.getItem.toString().includes(Object.keys({cachedValues})[0])) {
|
|
// These hooks are useful to preserve player state on player reload
|
|
// Firefox doesn't allow hooking of localStorage functions but chrome does
|
|
localStorageHookFailed = true;
|
|
}
|
|
}
|
|
declareOptions(window);
|
|
hookWindowWorker();
|
|
hookFetch();
|
|
if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") {
|
|
onContentLoaded();
|
|
} else {
|
|
window.addEventListener("DOMContentLoaded", function() {
|
|
onContentLoaded();
|
|
});
|
|
}
|
|
window.simulateAds = (depth) => {
|
|
if (depth === undefined || depth < 0) {
|
|
console.log('Ad depth paramter required (0 = no simulated ad, 1+ = use backup player for given depth)');
|
|
return;
|
|
}
|
|
postTwitchWorkerMessage('SimulateAds', depth);
|
|
};
|
|
})();
|