-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.user.js
More file actions
163 lines (135 loc) · 5.88 KB
/
script.user.js
File metadata and controls
163 lines (135 loc) · 5.88 KB
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
// ==UserScript==
// @name YouTube Ad-blocker
// @icon https://www.gstatic.com/youtube/img/branding/favicon/favicon_192x192.png
// @version 1.4.5
// @description Removes ads from YouTube videos and pages using Enhancer for YouTube's 'Remove Ads' button.
// @author AlejandroLHC
// @updateURL https://github.com/AlejandroLuisHC/yt-adblocker-script/raw/main/script.user.js
// @downloadURL https://github.com/AlejandroLuisHC/yt-adblocker-script/raw/main/script.user.js
// @match https://www.youtube.com/*
// ==/UserScript==
(function () {
'use strict';
const searchInterval = 50;
let failCounter = 0;
let blockCounter = 0;
let masterSwitch = true;
let allowUpdate = true;
function removeAds() {
const currentURL = window.location.href;
if (!masterSwitch || !currentURL.includes("youtube.com")) {
return;
}
if (/https:\/\/www\.youtube\.com\/watch\?.*/.test(currentURL)) {
const adShowing = document.querySelector('.ad-showing');
const ad2Showing = document.querySelector('#video-ads');
const bannerShowing = document.querySelector('.player-ads');
const adMiniBanner = document.querySelector('.ytd-ad-slot-renderer');
const skipButtonShowing = document.querySelector('#ytp-ad-skip-button-modern');
const adContainerShowing = document.querySelector('.ytp-cultural-moment-player-content')
const blockMessageShown = document.querySelector('ytd-enforcement-message-view-model');
const premiumDialogShowing = document.querySelector('.mealbar-promo-renderer');
const button = document.querySelector('efyt-not-interested');
removeElement(bannerShowing);
removeElement(ad2Showing);
removeElement(adMiniBanner);
removeElement(adContainerShowing);
removeElement(premiumDialogShowing.parentNode);
clickElement(skipButtonShowing);
if (blockMessageShown) {
navigateHistory();
blockCounter = blockMessageShown ? blockCounter + 1 : 0;
}
if (blockCounter > 10) {
handleAdBlockerRetry();
}
if (button) {
button.click();
failCounter = 0;
} else {
handleButtonNotFound();
}
if (failCounter > 10) {
handleFailedToFindButton();
}
}
if (/https:\/\/www\.youtube\.com\/$/.test(currentURL)) {
const headAdShowing = document.querySelector('#masthead-ad');
const adCardShowing = document.querySelector('ytd-ad-slot-renderer');
const ytAdBanner = document.querySelector('ytd-statement-banner-renderer');
removeElement(headAdShowing);
if (adCardShowing || ytAdBanner) {
const adParent = (adCardShowing || ytAdBanner).parentNode.parentNode;
removeElement(adParent);
}
}
}
function removeElement(element) {
element?.remove();
}
function clickElement(element) {
element?.click();
}
function navigateHistory() {
History.back();
History.forward();
}
function handleAdBlockerRetry() {
const blocker = window.confirm("Make sure there are no other ad-blockers working on this page.\n\nPress 'OK' to retry.\nPress 'Cancel' to disable the YouTube ad-blocker script for this session.");
if (blocker) {
blockCounter = 0;
} else {
masterSwitch = false;
}
}
function handleButtonNotFound() {
console.error(`Failed to find button. Retrying in ${searchInterval} ms`);
failCounter++;
}
function handleFailedToFindButton() {
const buttonNotFound = window.confirm("Failed to find the 'Remove Ads' button. Please make sure that Enhancer for YouTube is installed.\n\nPress 'OK' to redirect to the installation page.\nPress 'Cancel' to disable the YouTube ad-blocker script for this session.");
if (buttonNotFound) {
window.open("https://chrome.google.com/webstore/detail/enhancer-for-youtube/ponfpcnoihfmfllpaingbgckeeldkhle");
failCounter = 0;
} else {
masterSwitch = false;
}
}
function checkUpdate() {
if (!allowUpdate || !window.location.href.includes("youtube.com")) {
return;
}
const scriptUrl = 'https://raw.githubusercontent.com/AlejandroLuisHC/yt-adblocker-script/main/script.user.js';
fetch(scriptUrl)
.then(response => response.text())
.then(data => {
const match = data.match(/@version\s+(\d+\.\d+)/)[1];
if (match) {
const majorVersion = match.split('.')[0];
handleVersionCheck(majorVersion, scriptUrl);
} else {
console.error('YouTube ad-blocker script: Unable to extract version from the GitHub script.');
}
})
.catch(error => {
console.error('YouTube ad-blocker script: Error checking for updates:', error);
});
}
function handleVersionCheck(version, script) {
const currentVersion = GM_info.script.version.split('.')[0];
if (version > currentVersion) {
handleUpdateAvailable(version, script);
}
}
function handleUpdateAvailable(version, script) {
if (window.confirm(`A major update is available for the YouTube ad-blocker script. Please update to version ${version}.\n\nPress 'OK' to redirect and upgrade the script.\nPress 'Cancel' to not update for this session.`)) {
window.open(script);
} else {
allowUpdate = false;
}
}
removeAds();
checkUpdate();
setInterval(removeAds, searchInterval);
setInterval(checkUpdate, 30 * 60 * 1000);
})();