-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathYoutubeAntiAntiAdblock.user.js
258 lines (226 loc) · 9.94 KB
/
YoutubeAntiAntiAdblock.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ==UserScript==
// @name Youtube Anti-anti-adblock
// @namespace http://github.com/planetarian/TamperMonkey-Scripts
// @version 0.13
// @description Replaces the youtube video player with a youtube embed iframe to subvert the anti-adblock measures.
// @author Chami
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @updateURL https://github.com/planetarian/TamperMonkey-Scripts/raw/main/YoutubeAntiAntiAdblock.user.js
// @downloadURL https://github.com/planetarian/TamperMonkey-Scripts/raw/main/YoutubeAntiAntiAdblock.user.js
// @grant GM_addStyle
// ==/UserScript==
/*
YT player locations:
body
> #player > #player-wrap > #player-api
> ytd-app > #content > #page-manager > ytd-watch-flexy
> #full-bleed-container
> #columns > #primary > #primary-inner > #player > #player-container-outer > #player-container-inner > #container
> #player-container > #ytd-player > #container
> #movie_player > .html5-video-container > video //before
> #movie_player > #aab-embed //after
*/
(function() {
'use strict';
function log(message) {
console.log("AAB: " + message);
}
function callPlayer(func, args) {
if (!embed) return;
embed.contentWindow.postMessage(JSON.stringify({
'event': 'command',
'func': func,
'args': args || []
}), '*');
}
// get the video ID of the video currently being watched
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
}
// replace the youtube player with the embed
function replacePlayer(videoId, timestamp) {
// movie_player contains all the player controls, get rid of them and replace with the embed
const player = document.getElementById('movie_player');
if (!player && !embed) {
// We're still loading the initial page; do nothing
log("page loading; skipping.");
return false;
}
log("checking for video player.");
const videoEls = player.getElementsByClassName('html5-main-video');
if (videoEls.length > 1) {
log("there seem to be multiple video players?");
}
// if we have an html5 player (no anti-adblock)
if (videoEls.length > 0 && !!videoEls[0].src) {
log("html5 player present. using that instead.");
if (embed) {
log("removing embed.");
embed.remove();
embed = undefined;
}
}
// if there's no html5 player (anti-adblock present)
else {
log("no html5 player present. adding embed.");
// remove the notice
const errorOverlay = document.getElementById('error-screen');
if (errorOverlay) {
errorOverlay.remove();
log("error overlay removed.");
}
var src = `https://www.youtube.com/embed/${videoId}?enablejsapi=1&autoplay=1`;
if (!!timestamp && timestamp.length) {
let time = timestamp[0]
time = time.replace(/s$/, '');
src += `&start=${time}`
}
if (embed) {
embed.src = src;
player.innerHtml = '';
player.appendChild(embed);
}
else {
// replace the player
player.innerHTML = `<iframe id="aab-embed" width="100%" height="100%" src="${src}" allow="autoplay"></iframe>`;
embed = document.getElementById('aab-embed');
}
callPlayer('playVideo');
log("YouTube player replaced with embed iframe.");
return true;
}
}
// replace player again after the user navigates to a new video
document.addEventListener("yt-navigate-finish", function(event) {
stage = 0;
// clear the embed if we've added it already
const embed = document.getElementById('aab-embed');
if (embed) embed.remove();
// replace the video on the new page
const videoId = youtube_parser(document.location.href);
if (!videoId) return;
const url = new URL(document.location.href);
replacePlayer(videoId, url.searchParams.getAll('t'));
});
// unhide the player container stuff in case anti-adblock hid it
GM_addStyle('ytd-watch-flexy[player-unavailable] #player-container-outer.ytd-watch-flexy { visibility: visible !important; }');
// hold onto the embed for later reference
var embed = null;
const descObserver = new MutationObserver(textContainersMutated);
const commentsObserver = new MutationObserver(commentsContainerMutated);
const textObserver = new MutationObserver(textContainersMutated);
// set up the mutation observer to monitor for when the player is added
const pageManager = document.getElementById('page-manager');
const pageObserver = new MutationObserver(pageMutated);
try {
log("observing page manager.");
pageObserver.observe(pageManager, { childList: true });
}
catch (error) {
log("couldn't observe page manager.");
}
var stage = 0;
const observingTag = 'aab-observing';
function pageMutated(mutationList, observer) {
if (stage >= 2) return;
for (const mutation of mutationList) {
if (mutation.type === "childList") {
log(`anti-anti-adblock reacting to element mutation. Stage ${stage}`);
if (stage === 0) {
// looking for the main player container
const playerContainer = document.getElementById('player');
if (!playerContainer) {
log("no player container found.");
continue;
}
addTextObservers();
// the actual player itself hasn't been added yet
// so we need to monitor the element it'll be added to
const ytdPlayer = document.getElementById('ytd-player');
const ytdContainer = ytdPlayer.getElementsByClassName('ytd-player');
if (ytdContainer.length == 1) {
pageObserver.disconnect();
stage = 1;
log("observing containers.");
pageObserver.observe(ytdContainer[0], { childList: true });
}
}
else if (stage === 1) {
// should have the player ready to replace now
const videoId = youtube_parser(document.location.href);
if (!videoId) {
log("not a video page.");
continue;
}
const url = new URL(document.location.href);
if (!replacePlayer(videoId, url.searchParams.getAll('t'))) continue;
pageObserver.disconnect();
stage = 2;
}
}
}
}
function addTextObservers() {
const desc = document.querySelectorAll('#description-inline-expander yt-attributed-string')[0];
if (desc && !desc.classList.contains(observingTag)) {
try {
log("observing video description.");
descObserver.observe(desc, { childList: true });
desc.classList.add(observingTag);
}
catch (error) {
log("couldn't observe video description.");
console.error(error);
}
}
const comments = document.querySelectorAll('#comments')[0];
if (comments && !comments.classList.contains(observingTag)) {
try {
log("observing comments section.");
commentsObserver.observe(comments, { childList: true });
comments.classList.add(observingTag);
}
catch (error) {
log("couldn't observe comments section.");
console.error(error);
}
}
}
function commentsContainerMutated(mutationList, observer) {
log("comments section mutated.");
for (const mutation of mutationList) {
const comments = document.querySelectorAll('#comments > #sections > #contents')[0];
textObserver.observe(comments, { childList: true });
}
}
function textContainersMutated(mutationList, observer) {
for (const mutation of mutationList) {
for (let i = 0; i < Array.from(mutation.addedNodes).length; i++) {
const node = mutation.addedNodes[i];
const links = node.querySelectorAll('#comment #body #comment-content #content-text a');
if (!links.length) continue;
for (let l = 0; l < links.length; l++) {
const link = links[l];
const taggedClass = 'aab-link';
if (link.classList.contains(taggedClass)) continue;
link.classList.add(taggedClass);
const matches = /(?:(?<hh>\d\d?):)?(?<mm>\d\d?):(?<ss>\d\d?)/.exec(link.innerText);
if (!matches) continue;
let timestamp = (Number(matches.groups.mm)*60)+Number(matches.groups.ss);
if (matches.groups.hh > 0) {
timestamp += Number(matches.groups.hh) * 60 * 60;
}
log(`found timestamp ${timestamp}`);
link.addEventListener('click', (ev) => {
log(timestamp);
callPlayer('playVideo');
callPlayer('seekTo', [timestamp]);
});
}
}
}
}
})();