-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt-hide-ads.js
60 lines (54 loc) · 1.82 KB
/
yt-hide-ads.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// ==UserScript==
// @name hide youtube ads
// @description hide youtube google ad, auto click "skip ad" and reload if video load failed because of ad block
// @match *://www.youtube.com/*
// @run-at document-start
// ==/UserScript==
const isWatch = () => { return window.location.href.includes('watch') }
const fixVideo = () => {
if (!isWatch())
return
const el = document.querySelector('.video-stream.html5-main-video')
if (el && el.src.length === 0) {
const url = new URL(window.location.href)
if (url.searchParams.has('t')) {
url.searchParams.set('t', 0)
window.location.href = url.toString()
} else {
window.location.reload(false)
}
}
}
const closeAd = function (){
const css = '.video-ads .ad-container .adDisplay, #player-ads, .ytp-ad-module, .ytp-ad-image-overlay{ display: none !important; }',
style = document.createElement('style');
style.type = 'text/css'
style.id = 'no-ads'
if (style.styleSheet){
style.styleSheet.cssText = css
} else {
style.appendChild(document.createTextNode(css))
}
document.head.appendChild(style)
};
const skipAd = function(){
const observer = new MutationObserver(function(list, mutationObserver){
for (const mutation of list) {
const button = document.querySelector(".ytp-ad-skip-button.ytp-button")||document.querySelector(".videoAdUiSkipButton")
if (button && button.nodeName === "BUTTON" ) {
button.click()
}
}
mutationObserver.disconnect()
})
const i = setInterval(() => {
const videoEl = document.querySelector('.video-ads.ytp-ad-module')
if (videoEl !== null) {
observer.observe(videoEl, {childList: true, subtree: true })
clearInterval(i)
}
}, 100)
};
closeAd()
skipAd()
setInterval(fixVideo, 500)