Skip to content

Instantly share code, notes, and snippets.

@kaytwo
Created November 15, 2017 14:10
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kaytwo/54909ad753aecfcba0cf8038ebfa9322 to your computer and use it in GitHub Desktop.
Save kaytwo/54909ad753aecfcba0cf8038ebfa9322 to your computer and use it in GitHub Desktop.
'use strict';
const puppeteer = require('puppeteer');
(async () => {
/* PRECONDITION:
0. download ublock, I used https://github.com/gorhill/uBlock/releases/download/1.14.19b5/uBlock0.chromium.zip
1. run $PATH_TO_CHROME --user-data-dir=/some/empty/directory --load-extension=/location/of/ublock
2. enable block lists you want to use
*/
const ext = '/Users/ckanich/Downloads/uBlock0.chromium';
const datadir = '/Users/ckanich/Downloads/profile/';
// Doesn't work with headless: false, my guess is that ublock
// (or every extension?) breaks if it can't show its menu button
const browser = await puppeteer.launch(
{
headless: false,
userDataDir: datadir,
args: [`--disable-extensions-except=${ext}`, `--load-extension=${ext}`]
});
const page = await browser.newPage();
// First load will show ads and I have no idea why:
await page.goto('https://www.bing.com/search?q=web+hosting');
// Give ublock time to load its settings or something?
// Without this wait next page ends up with ads too
await page.waitFor(2500);
// This page will not show ads:
await page.goto('https://www.nytimes.com');
// Wait for the page to actually render everything
await page.waitFor(5000);
await page.screenshot({ path: 'nyt-without-ads.png', fullPage: true });
await browser.close();
})();
@KaKi87
Copy link

KaKi87 commented May 1, 2020

Hello, thanks for your tutorial, it works very nicely !... In non-headless mode. Did you find any way to make perform ad-blocking in headless mode ? Thanks

@kaytwo
Copy link
Author

kaytwo commented May 1, 2020

Nope, but judging by this the capability might have been added? Just not on Windows :(.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment