Skip to content

Instantly share code, notes, and snippets.

Forked from kaytwo/puppeteer-with-adblock.js
Created February 12, 2019 15:32
  • Star 21 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
'use strict';
const puppeteer = require('puppeteer');
(async () => {
0. download ublock, I used
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('');
// 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('');
// 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment