-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove ads from youtube.txt
More file actions
351 lines (332 loc) · 11 KB
/
remove ads from youtube.txt
File metadata and controls
351 lines (332 loc) · 11 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// ==UserScript==
// @name Auto Close YouTube Ads
// @namespace http://fuzetsu.acypa.com
// @version 1.4.1
// @description Close and/or Mute YouTube ads automatically!
// @author fuzetsu
// @match *://*.youtube.com/*
// @exclude *://*.youtube.com/subscribe_embed?*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_registerMenuCommand
// @require https://gitcdn.xyz/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/wait-for-elements/wait-for-elements.js
// @require https://gitcdn.xyz/repo/kufii/My-UserScripts/fa4555701cf5a22eae44f06d9848df6966788fa8/libs/gm_config.js
// ==/UserScript==
/* globals GM_getValue GM_setValue GM_deleteValue GM_registerMenuCommand GM_config waitForElems waitForUrl */
/**
* This section of the code holds the css selectors that point different parts of YouTube's
* user interface. If the script ever breaks and you don't want to wait for me to fix it
* chances are that it can be fixed by just updating these selectors here.
*/
const CSS = {
// the button used to skip an ad
skipButton: '.videoAdUiSkipButton,.ytp-ad-skip-button',
// the area showing the countdown to the skip button showing
preSkipButton: '.videoAdUiPreSkipButton,.ytp-ad-preview-container',
// little x that closes banner ads
closeBannerAd: '.close-padding.contains-svg,a.close-button,.ytp-ad-overlay-close-button',
// button that toggle mute on the video
muteButton: '.ytp-mute-button',
// the slider bar handle that represents the current volume
muteIndicator: '.ytp-volume-slider-handle',
// container for ad on video
adArea: '.videoAdUi,.ytp-ad-player-overlay',
// container that shows ad length eg 3:23
adLength: '.videoAdUiAttribution,.ytp-ad-duration-remaining',
// container for header ad on the home page
homeAdContainer: '#masthead-ad'
}
const util = {
log: (...args) => console.log(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: purple;', ...args),
clearTicks: ticks => {
ticks.forEach(tick =>
!tick ? null : typeof tick === 'number' ? clearInterval(tick) : tick.stop()
)
ticks.length = 0
},
keepTrying: (wait, action) => {
const tick = setInterval(() => action() && clearInterval(tick), wait)
return tick
},
storeGet: key => {
if (typeof GM_getValue === 'undefined') {
const value = localStorage.getItem(key)
return value === 'true' ? true : value === 'false' ? false : value
}
return GM_getValue(key)
},
storeSet: (key, value) =>
typeof GM_setValue === 'undefined' ? localStorage.setItem(key, value) : GM_setValue(key, value),
storeDel: key =>
typeof GM_deleteValue === 'undefined' ? localStorage.removeItem(key) : GM_deleteValue(key),
q: (query, context) => (context || document).querySelector(query),
qq: (query, context) => Array.from((context || document).querySelectorAll(query)),
get: (obj, str) => util.getPath(obj, str.split('.').reverse()),
getPath: (obj, path) =>
obj == null ? null : path.length > 0 ? util.getPath(obj[path.pop()], path) : obj
}
const SCRIPT_NAME = 'Auto Close YouTube Ads'
const SHORT_AD_MSG_LENGTH = 12000
const TICKS = []
let DONT_SKIP = false
const config = GM_config([
{
key: 'muteAd',
label: 'Mute ads?',
type: 'bool',
default: true
},
{
key: 'hideAd',
label: 'Hide video ads?',
type: 'bool',
default: false
},
{
key: 'secWaitBanner',
label: 'Banner ad close delay (seconds)',
type: 'number',
default: 3,
min: 0
},
{
key: 'secWaitVideo',
label: 'Video ad skip delay (seconds)',
type: 'number',
default: 3,
min: 0
},
{
key: 'minAdLengthForSkip',
label: 'Dont skip video shorter than this (seconds)',
type: 'number',
default: 0,
min: 0
},
{
key: 'muteEvenIfNotSkipping',
label: 'Mute video even if not skipping',
type: 'bool',
default: true
},
{
key: 'debug',
label: 'Show extra debug information.',
type: 'bool',
default: false
},
{
key: 'version',
type: 'hidden',
default: 1
}
])
const configVersion = 2
let conf = config.load()
config.onsave = cfg => (conf = cfg)
// config upgrade procedure
function upgradeConfig() {
let lastVersion
while (conf.version < configVersion && lastVersion !== conf.version) {
util.log('upgrading config version, current = ', conf.version, ', target = ', configVersion)
lastVersion = conf.version
switch (conf.version) {
case 1: {
const oldConf = {
muteAd: util.storeGet('MUTE_AD'),
hideAd: util.storeGet('HIDE_AD'),
secWait: util.storeGet('SEC_WAIT')
}
if (oldConf.muteAd != null) conf.muteAd = !!oldConf.muteAd
if (oldConf.hideAd != null) conf.hideAd = !!oldConf.hideAd
if (oldConf.secWait != null && !isNaN(oldConf.secWait))
conf.secWaitBanner = conf.secWaitVideo = parseInt(oldConf.secWait)
conf.version = 2
config.save(conf)
;['SEC_WAIT', 'HIDE_AD', 'MUTE_AD'].forEach(util.storeDel)
break
}
}
}
}
upgradeConfig()
function createMessageElement() {
const elem = document.createElement('div')
elem.setAttribute(
'style',
'border: 1px solid white;border-right: none;background: rgb(0,0,0,0.75);color:white;position: absolute;right: 0;z-index: 1000;top: 10px;padding: 10px;padding-right: 20px;cursor: pointer;pointer-events: all;'
)
return elem
}
function showMessage(container, text, ms) {
const message = createMessageElement()
message.textContent = text
container.appendChild(message)
util.log(`showing message [${ms}ms]: ${text}`)
setTimeout(() => message.remove(), ms)
}
function setupCancelDiv(ad) {
const skipArea = util.q(CSS.preSkipButton, ad)
const skipText = skipArea && skipArea.textContent.trim().replace(/\s+/g, ' ')
if (skipText && !['will begin', 'will play'].some(snip => skipText.includes(snip))) {
const cancelClass = 'acya-cancel-skip'
let cancelDiv = util.q('.' + cancelClass)
if (cancelDiv) cancelDiv.remove()
cancelDiv = createMessageElement()
cancelDiv.className = cancelClass
cancelDiv.textContent = (conf.muteAd ? 'Un-mute & ' : '') + 'Cancel Auto Skip'
cancelDiv.onclick = () => {
util.log('cancel clicked')
DONT_SKIP = true
cancelDiv.remove()
const muteButton = getMuteButton()
const muteIndicator = getMuteIndicator()
if (conf.muteAd && muteButton && muteIndicator && isMuted(muteIndicator)) muteButton.click()
}
ad.appendChild(cancelDiv)
} else {
util.log("skip button area wasn't there for some reason.. couldn't place cancel button.")
}
}
function parseTime(str) {
const [minutes, seconds] = str
.split(' ')
.pop()
.split(':')
.map(num => parseInt(num))
util.log(str, minutes, seconds)
return minutes * 60 + seconds || 0
}
const getMuteButton = () => util.q(CSS.muteButton)
const getMuteIndicator = () => util.q(CSS.muteIndicator)
const isMuted = m => m.style.left === '0px'
function getAdLength(ad) {
if (!ad) return 0
const time = ad.querySelector(CSS.adLength)
return time ? parseTime(time.textContent) : 0
}
function waitForAds() {
DONT_SKIP = false
TICKS.push(
waitForElems({
sel: CSS.skipButton,
onmatch: btn => {
util.log('found skip button')
util.keepTrying(500, () => {
if (!btn) return true
// if not visible
if (btn.offsetParent === null) return
setTimeout(() => {
if (DONT_SKIP) {
util.log('not skipping...')
DONT_SKIP = false
return
}
util.log('clicking skip button')
btn.click()
}, conf.secWaitVideo * 1000)
return true
})
}
}),
waitAndClick(CSS.closeBannerAd, conf.secWaitBanner * 1000),
waitForElems({
sel: CSS.adArea,
onmatch: ad => {
// reset don't skip
DONT_SKIP = false
const adLength = getAdLength(ad)
const isShort = adLength < conf.minAdLengthForSkip
const debug = () =>
conf.debug
? `[DEBUG adLength = ${adLength}, minAdLengthForSkip = ${conf.minAdLengthForSkip}]`
: ''
if (isShort && !conf.muteEvenIfNotSkipping) {
DONT_SKIP = true
return showMessage(
ad,
`Shot AD detected, will not skip or mute. ${debug()}`,
SHORT_AD_MSG_LENGTH
)
}
if (conf.hideAd) {
ad.style.zIndex = 10
ad.style.background = 'black'
}
// show option to cancel automatic skip
if (!isShort) setupCancelDiv(ad)
if (!conf.muteAd) return
const muteButton = getMuteButton()
const muteIndicator = getMuteIndicator()
if (!muteIndicator) return util.log('unable to determine mute state, skipping mute')
muteButton.click()
util.log('Video ad detected, muting audio')
// wait for the ad to disappear before unmuting
util.keepTrying(250, () => {
if (!util.q(CSS.adArea)) {
if (isMuted(muteIndicator)) {
muteButton.click()
util.log('Video ad ended, unmuting audio')
} else {
util.log('Video ad ended, audio already unmuted')
}
return true
}
})
if (isShort) {
DONT_SKIP = true
return showMessage(
ad,
`Short AD detected, will not skip but will mute. ${debug()}`,
SHORT_AD_MSG_LENGTH
)
}
}
})
)
}
const waitAndClick = (sel, ms, cb) =>
waitForElems({
sel: sel,
onmatch: btn => {
util.log('Found ad, closing in', ms, 'ms')
setTimeout(() => {
btn.click()
if (cb) cb(btn)
}, ms)
}
})
util.log('Started')
if (window.self === window.top) {
let videoUrl
// close home ad whenever encountered
waitForElems({ sel: CSS.homeAdContainer, onmatch: ad => ad.remove() })
// wait for video page
waitForUrl(/^https:\/\/www\.youtube\.com\/watch\?.*v=.+/, () => {
if (videoUrl && location.href !== videoUrl) {
util.log('Changed video, removing old wait')
util.clearTicks(TICKS)
}
videoUrl = location.href
util.log('Entered video, waiting for ads')
waitForAds()
TICKS.push(
waitForUrl(
url => url !== videoUrl,
() => {
videoUrl = null
util.clearTicks(TICKS)
util.log('Left video, stopped waiting for ads')
},
true
)
)
})
} else {
if (/^https:\/\/www\.youtube\.com\/embed\//.test(location.href)) {
util.log('Found embedded video, waiting for ads')
waitForAds()
}
}
GM_registerMenuCommand('Auto Close Youtube Ads - Manage Settings', config.setup)