-
-
Save odensc/accbd80cc5c3b796c5ea484a385b742f to your computer and use it in GitHub Desktop.
Some ad detection code adapted from @simple-hacker
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 | |
const origFetch = window.fetch; | |
window.fetch = (url, init, ...args) => { | |
if (typeof url === "string") { | |
if (url.includes("/access_token")) { | |
url = url.replace("player_type=site", "player_type=thunderdome"); | |
} else if ( | |
url.includes("/gql") && | |
init && | |
typeof init.body === "string" && | |
init.body.includes("PlaybackAccessToken") | |
) { | |
const newBody = JSON.parse(init.body); | |
newBody.variables.playerType = "thunderdome"; | |
init.body = JSON.stringify(newBody); | |
} | |
} | |
return origFetch(url, init, ...args); | |
}; | |
const attachMutationObserver = () => { | |
const videoPlayer = document.querySelector(".video-player"); | |
if (!videoPlayer) return; | |
const adObserver = new MutationObserver(() => { | |
const ad = videoPlayer.querySelector( | |
'[data-test-selector="ad-banner-default-text"]' | |
); | |
if (ad) { | |
console.log("Ad detected, swapping to picture-by-picture player."); | |
const mainVideo = document.querySelector( | |
".video-player__container video" | |
); | |
const pbypContainer = document.querySelector( | |
".picture-by-picture-player" | |
); | |
const pbypVideo = pbypContainer.querySelector("video"); | |
if (pbypVideo.muted) pbypVideo.volume = mainVideo.volume; | |
pbypVideo.muted = mainVideo.muted; | |
pbypVideo.style.width = `${mainVideo.offsetWidth}px`; | |
pbypVideo.style.height = `${mainVideo.offsetHeight}px`; | |
pbypContainer.style.opacity = "1"; | |
pbypContainer.style.pointerEvents = "none"; | |
pbypContainer.style.position = "absolute"; | |
pbypContainer.style.left = `-${mainVideo.offsetWidth}px`; | |
pbypContainer.style.zIndex = "9999"; | |
pbypContainer.style.overflow = "visible"; | |
pbypContainer.querySelector(".tw-aspect").style.overflow = "visible"; | |
} else { | |
const pbypContainer = document.querySelector( | |
".picture-by-picture-player" | |
); | |
const pbypVideo = pbypContainer && pbypContainer.querySelector("video"); | |
if (pbypVideo) { | |
pbypVideo.muted = true; | |
pbypContainer.style.opacity = "0"; | |
} | |
} | |
}); | |
adObserver.observe(videoPlayer, { | |
childList: true, | |
subtree: true, | |
}); | |
}; | |
window.onload = () => { | |
attachMutationObserver(); | |
const pushState = history.pushState; | |
history.pushState = (...args) => { | |
pushState(...args); | |
attachMutationObserver(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment