-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytb-ads-blocker.js
59 lines (45 loc) · 1.92 KB
/
ytb-ads-blocker.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
class AdBlocker {
constructor() {
this.vid = null;
this.ad = null;
this.pbRate = 1;
}
hideOrRemoveElement(element, action = 'hide') {
if (element) {
if (action === 'hide') {
element.style.display = 'none';
} else if (action === 'remove') {
element.remove();
}
}
}
clickElement(element) {
if (element) {
element.click();
}
}
removeAds() {
this.vid = document.getElementsByClassName('video-stream html5-main-video')[0];
this.ad = document.getElementsByClassName('video-ads ytp-ad-module')[0];
if (!this.vid) return;
const closeableAds = document.getElementsByClassName('ytp-ad-overlay-close-button');
Array.from(closeableAds).forEach(ad => this.clickElement(ad));
this.hideOrRemoveElement(document.getElementsByClassName('style-scope ytd-watch-next-secondary-results-renderer sparkles-light-cta GoogleActiveViewElement')[0]);
this.hideOrRemoveElement(document.getElementsByClassName('style-scope ytd-item-section-renderer sparkles-light-cta')[0]);
this.clickElement(document.getElementsByClassName('ytp-ad-text ytp-ad-skip-button-text')[0]);
this.hideOrRemoveElement(document.getElementsByClassName('ytp-ad-message-container')[0]);
this.hideOrRemoveElement(document.getElementsByClassName('style-scope ytd-companion-slot-renderer')[0], 'remove');
if (this.ad && this.ad.children.length && document.getElementsByClassName('ytp-ad-text ytp-ad-preview-text')[0]) {
this.vid.playbackRate = 16;
this.vid.muted = true;
}
this.hideOrRemoveElement(document.getElementById('masthead-ad'), 'remove');
this.hideOrRemoveElement(document.getElementsByTagName('ytd-ad-slot-renderer')[0], 'remove');
this.hideOrRemoveElement(document.getElementsByTagName('ytd-reel-shelf-renderer')[0], 'remove');
}
init() {
setInterval(() => this.removeAds(), 100);
}
}
const adBlocker = new AdBlocker();
adBlocker.init();