-
Notifications
You must be signed in to change notification settings - Fork 2
/
yesblocktheads.user.js
166 lines (145 loc) · 5.88 KB
/
yesblocktheads.user.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ==UserScript==
// @name Yes BlockTheAds
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description Kiss the annoying youtube ads goodbye!
// @author elliottophellia
// @license GPL-3.0
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @homepageURL https://github.com/elliottophellia/yesblocktheads
// @supportURL https://github.com/elliottophellia/yesblocktheads/issues
// @downloadURL https://cdn.rei.my.id/yesblocktheads/yesblocktheads.user.js
// @updateURL https://cdn.rei.my.id/yesblocktheads/yesblocktheads.meta.js
// @match *://*.youtube.com/*
// @exclude *://accounts.youtube.com/*
// @exclude *://www.youtube.com/live_chat_replay*
// @exclude *://www.youtube.com/persist_identity*
// @run-at document-idle
// @compatible chrome
// @compatible firefox
// @compatible opera
// @compatible edge
// ==/UserScript==
(async () => {
'use strict';
const tag = '[Yes BlockTheAds]';
const adSelectors = [
'#masthead-ad', '.video-ads.ytp-ad-module', 'ytd-ad-slot-renderer',
'ytd-rich-item-renderer.style-scope.ytd-rich-grid-row #content:has(.ytd-display-ad-renderer)',
'tp-yt-paper-dialog:has(yt-mealbar-promo-renderer)', 'yt-mealbar-promo-renderer',
'ytd-engagement-panel-section-list-renderer[target-id="engagement-panel-ads"]',
'#related #player-ads', '#related ytd-ad-slot-renderer',
'ytd-popup-container:has(a[href="/premium"])', 'ad-slot-renderer',
'ytm-companion-ad-renderer'
];
const log = (message) => console.log(`${tag}[${getTimestamp()}] ${message}`);
const debug = (message) => console.debug(`${tag}[${getTimestamp()}] ${message}`);
const asDoubleDigit = value => value < 10 ? `0${value}` : value;
const getTimestamp = () => {
const dt = new Date();
return `${asDoubleDigit(dt.getHours())}:${asDoubleDigit(dt.getMinutes())}:${asDoubleDigit(dt.getSeconds())}`;
};
const setFlag = (flagName) => {
if (!document.getElementById(flagName)) {
const flag = document.createElement('style');
flag.id = flagName;
document.head.appendChild(flag);
return false;
}
return true;
};
const injectAdBlockStyles = () => {
if (setFlag('yesBlockTheAdsStyles')) {
debug('Ad-blocking styles already present');
return;
}
const style = document.createElement('style');
style.textContent = adSelectors.map(selector => `${selector}{display:none!important}`).join('');
document.head.appendChild(style);
log('Ad-blocking styles successfully injected');
};
const simulateInteraction = (element) => {
const events = ['mousedown', 'mouseup', 'click'];
events.forEach(eventType => {
const event = new MouseEvent(eventType, {
view: window,
bubbles: true,
cancelable: true
});
element.dispatchEvent(event);
});
};
const getVideoPlayer = () => document.querySelector('.ad-showing video, video');
const ensureVideoPlayback = (video) => {
if (video.paused && video.currentTime < 0.5) {
video.play().catch(() => debug('Auto-play prevented'));
debug('Attempting to auto-play video');
}
};
const clearOverlays = () => {
const premiumPrompts = document.querySelectorAll('ytd-popup-container');
premiumPrompts.forEach(prompt => {
if (prompt.querySelector('a[href="/premium"]')) {
prompt.remove();
debug('Removed a YouTube Premium prompt');
}
});
const overlay = document.querySelector('tp-yt-iron-overlay-backdrop[style*="z-index: 2201"]');
if (overlay) {
overlay.style.display = 'none';
debug('Hidden an overlay backdrop');
}
};
const bypassAd = (video) => {
const skipBtn = document.querySelector('.ytp-ad-skip-button, .ytp-skip-ad-button, .ytp-ad-skip-button-modern');
const adIndicator = document.querySelector('.video-ads.ytp-ad-module .ytp-ad-player-overlay, .ytp-ad-button-icon');
if ((skipBtn || adIndicator) && !window.location.href.includes('https://m.youtube.com/')) {
video.muted = true;
}
if (skipBtn) {
setTimeout(() => {
if (video.currentTime > 0.5) {
video.currentTime = video.duration;
debug('Skipped ad for special case');
return;
}
simulateInteraction(skipBtn);
debug('Bypassed ad using skip button');
}, 250);
} else if (adIndicator) {
video.currentTime = video.duration;
debug('Forcefully ended the current ad');
}
};
const enhanceYouTubeExperience = () => {
if (setFlag('yesBlockTheAdsEnhanced')) {
debug('Yes BlockTheAds enhancement already active');
return;
}
new MutationObserver(() => {
const video = getVideoPlayer();
if (video) {
clearOverlays();
bypassAd(video);
ensureVideoPlayback(video);
}
}).observe(document.body, { childList: true, subtree: true });
log('Yes BlockTheAds enhancement activated');
};
const waitForYouTubeApp = () => {
return new Promise(resolve => {
const checkForApp = () => {
if (document.querySelector('ytd-app')) {
resolve();
} else {
setTimeout(checkForApp, 100);
}
};
checkForApp();
});
};
await waitForYouTubeApp();
injectAdBlockStyles();
enhanceYouTubeExperience();
log('Monitoring YouTube for ads...');
})();