Last active
October 17, 2022 14:26
-
-
Save pundoo/cca02e68d078794ff0601fd9fd0cb50a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twitch-videoad.js application/javascript | |
(function() { | |
if ( /(^|\.)twitch\.tv$/.test(document.location.hostname) === false ) { return; } | |
function hookFetch() { | |
var OPT_ACCESS_TOKEN_PLAYER_TYPE = 'embed';//480p | |
//var OPT_ACCESS_TOKEN_PLAYER_TYPE = 'picture-by-picture';//360p | |
var realFetch = window.fetch; | |
window.fetch = function(url, init, ...args) { | |
if (typeof url === 'string') { | |
if (OPT_ACCESS_TOKEN_PLAYER_TYPE) { | |
if (url.includes('/access_token')) { | |
var modifiedUrl = new URL(url); | |
modifiedUrl.searchParams.set('player_type', OPT_ACCESS_TOKEN_PLAYER_TYPE); | |
arguments[0] = modifiedUrl.href; | |
} | |
else if (url.includes('gql') && init && typeof init.body === 'string' && init.body.includes('PlaybackAccessToken')) { | |
const newBody = JSON.parse(init.body); | |
newBody.variables.playerType = OPT_ACCESS_TOKEN_PLAYER_TYPE; | |
init.body = JSON.stringify(newBody); | |
} | |
} | |
} | |
return realFetch.apply(this, arguments); | |
} | |
} | |
hookFetch(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment