forked from TheRealJoelmatic/RemoveAdblockThing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutube-Ad-blocker-Reminder-Remover
120 lines (104 loc) · 3.53 KB
/
Youtube-Ad-blocker-Reminder-Remover
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
// ==UserScript==
// @name Remove Adblock Thing
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Removes Adblock Thing
// @author JoelMatic
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function()
{
// Specify domains and JSON paths to remove
const domainsToRemove = [
'*.youtube-nocookie.com/*'
];
const jsonPathsToRemove = [
'playerResponse.adPlacements',
'playerResponse.playerAds',
'adPlacements',
'playerAds',
'playerConfig',
'auxiliaryUi.messageRenderers.enforcementMessageViewModel'
];
const debug = true; // Set to false to disable debug messages
let unpausedAfterSkip = 0; //This is used to check if the video has been unpaused already
if (debug) console.log("Remove Adblock Thing: Script started");
// Old variable but could work in some cases
window.__ytplayer_adblockDetected = false;
removeJsonPaths(domainsToRemove, jsonPathsToRemove);
setInterval(() =>
{
const popup = document.querySelector("body > ytd-app > ytd-popup-container > tp-yt-paper-dialog");
const video1 = document.querySelector("#movie_player > video.html5-main-video");
const video2 = document.querySelector("#movie_player > .html5-video-container > video");
if (popup)
{
if (debug) console.log("Remove Adblock Thing: Popup detected, removing...");
popup.remove();
unpausedAfterSkip = 2;
if (debug) console.log("Remove Adblock Thing: Popup removed");
}
// Check if the video is paused after removing the popup
if (!unpausedAfterSkip > 0) return;
if (video1)
{
// UnPause The Video
if (video1.paused) UnPauseVideo();
else if (unpausedAfterSkip > 0) unpausedAfterSkip--;
}
if (video2)
{
if (video2.paused) UnPauseVideo();
else if (unpausedAfterSkip > 0) unpausedAfterSkip--;
}
}, 1000);
// Observe and remove ads when new content is loaded dynamically
const observer = new MutationObserver(() =>
{
removeJsonPaths(domainsToRemove, jsonPathsToRemove);
});
const observerConfig = {
childList: true,
subtree: true
};
observer.observe(document.body, observerConfig);
function UnPauseVideo()
{
// Simulate pressing the "k" key to unpause the video
const keyEvent = new KeyboardEvent("keydown",{
key: "k",
code: "KeyK",
keyCode: 75,
which: 75,
bubbles: true,
cancelable: true,
view: window
});
document.dispatchEvent(keyEvent);
unpausedAfterSkip = 0;
if (debug) console.log("Remove Adblock Thing: Unpaused video using 'k' key");
}
function removeJsonPaths(domains, jsonPaths)
{
const currentDomain = window.location.hostname;
if (!domains.includes(currentDomain)) return;
jsonPaths.forEach(jsonPath =>{
const pathParts = jsonPath.split('.');
let obj = window;
for (const part of pathParts)
{
if (obj.hasOwnProperty(part))
{
obj = obj[part];
}
else
{
break;
}
}
obj = undefined;
});
}
})();