-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
62 lines (50 loc) · 1.51 KB
/
contentScript.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
const observer = new MutationObserver(handleMainParentElementChange);
const observerConfig = { attributes: true, childList: true, subtree: true }
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.type === 'changeUrl') {
main()
}
return true
}
)
async function waitPageReady () {
while (true) {
const parentElement = document.querySelector('#container .video-ads.ytp-ad-module')
console.log({parentElement})
if (parentElement) {
return parentElement
}
await sleep(500)
}
}
function handleMainParentElementChange (mutationList, observer) {
clickSkipButton()
}
async function main () {
observer.disconnect()
if (!window.location.pathname.includes('watch')) {
return
}
const mainParentElement = await waitPageReady() // espera a que el elemento padre exista
clickSkipButton() // para la primera carga
observer.observe(mainParentElement, observerConfig) // detecta cambios posteriores
}
function clickSkipButton () {
let buttonElement = null
buttonElement = document.querySelector('#container div.video-ads button.ytp-ad-skip-button-modern')
if (!buttonElement) {
// si no encuentra el boton modern
buttonElement = document.querySelector('#container div.video-ads button.ytp-ad-skip-button')
}
if (buttonElement) {
buttonElement.click()
return true
}
return false
}
// espera una cantidad de milisegundos
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
main()