Skip to content

Instantly share code, notes, and snippets.

@bliaxiong
Created January 10, 2021 16:33
Show Gist options
  • Save bliaxiong/94541bc1f5fff361bf5e7da94deaf6d3 to your computer and use it in GitHub Desktop.
Save bliaxiong/94541bc1f5fff361bf5e7da94deaf6d3 to your computer and use it in GitHub Desktop.
Youtube userscript to block ads in videos
const observer = new MutationObserver(mutations => {
for(const mutation of mutations) {
for(const addedNode of mutation.addedNodes) {
if(addedNode.tagName === "VIDEO") {
const video = addedNode
video.addEventListener("timeupdate", () => {
const adSkipButton = document.querySelector(".ytp-ad-skip-button-slot button,.ytp-ad-overlay-close-button")
if(adSkipButton) {
adSkipButton.click()
}
if(document.querySelector(".ad-showing")) {
video.currentTime = video.duration
}
})
}
}
}
})
observer.observe(document.documentElement, { subtree: true, childList: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment