Skip to content

Instantly share code, notes, and snippets.

@eldorplus
Forked from codesorter2015/skipAds.js
Created March 27, 2021 11:23
Show Gist options
  • Save eldorplus/e78b7a975aec445b0c9efbde666f6bbb to your computer and use it in GitHub Desktop.
Save eldorplus/e78b7a975aec445b0c9efbde666f6bbb 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