Skip to content

Instantly share code, notes, and snippets.

@codesorter2015
Created August 23, 2019 06:46
Show Gist options
  • Save codesorter2015/162b07263d106b2cc7440072f7f9d53f to your computer and use it in GitHub Desktop.
Save codesorter2015/162b07263d106b2cc7440072f7f9d53f to your computer and use it in GitHub Desktop.
How to automatically skip youtube Ads using puppeteer?
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