Created
August 23, 2019 06:46
-
-
Save codesorter2015/162b07263d106b2cc7440072f7f9d53f to your computer and use it in GitHub Desktop.
How to automatically skip youtube Ads using puppeteer?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
(async () => { | |
const browser = await puppeteer.launch({headless:false}); | |
const page = await browser.newPage(); | |
let url = 'youtube url'; | |
await page.goto(url); | |
await page.evaluate(() => { | |
self.moHandler = { | |
changesObserver: function (mutation) { | |
if (mutation.type === 'attributes'){ | |
if(mutation.target.className == 'ytp-ad-skip-button-container' || mutation.target.className == 'style-scope ytd-button-renderer style-text size-default'){ | |
mutation.target.click(); | |
} | |
} | |
}, | |
subscriber: function (mutations) { | |
mutations.forEach((mutation) => { | |
self.moHandler.changesObserver(mutation); | |
}); | |
}, | |
init: function () { | |
const target = self.document.documentElement; | |
const config = { | |
attributes: true | |
}; | |
self.mObserver = new MutationObserver(self.moHandler.subscriber); | |
self.mObserver.observe(target, config); | |
} | |
} | |
self.moHandler.init(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment