I’m trying to use an ad blocker extension with Puppeteer but can’t get it to work. Here’s what I’ve done:
- Cloned the ad blocker repo into my project folder
- Tried to load the extension when launching the browser
- Visited a site known to have ads, but they still show up
My code looks like this:
const extDir = './adblock/chrome'
const browser = await puppeteer.launch({
headless: false,
args: [
'--window-size=1920,1080',
`--load-extension=${extDir}`,
]
})
The path to manifest.json seems correct but the extension isn’t loading. I also tried adding it manually when starting Chrome but no luck. What am I missing? Any ideas on how to properly load and use an ad blocker with Puppeteer?
I’ve dealt with a similar issue before, and here’s what worked for me:
Instead of using the ‘–load-extension’ argument, try utilizing the ‘puppeteer-extra’ plugin along with ‘puppeteer-extra-plugin-adblocker’. This combo has been more reliable in my experience.
First, install these packages:
npm install puppeteer-extra puppeteer-extra-plugin-adblocker
Then, modify your code to something like this:
const puppeteer = require(‘puppeteer-extra’)
const AdblockerPlugin = require(‘puppeteer-extra-plugin-adblocker’)
puppeteer.use(AdblockerPlugin())
const browser = await puppeteer.launch({ headless: false })
This approach has consistently worked for me across different projects. It’s more straightforward and doesn’t require manual extension loading. Give it a shot and let me know if it resolves your issue!
While the previous suggestions are valid, I’d like to offer an alternative approach that has worked well for me. Instead of using third-party plugins, you can leverage Chrome’s built-in ad-blocking capabilities by utilizing the ‘–enable-features=BlockingAllAds’ flag. Here’s how you can modify your code:
const browser = await puppeteer.launch({
headless: false,
args: [
‘–window-size=1920,1080’,
‘–enable-features=BlockingAllAds’
]
});
This method is lightweight and doesn’t require additional dependencies. It’s worth noting that this flag might not block all ads, but it should significantly reduce them. If you need more robust ad-blocking, you may want to consider the plugin-based solutions mentioned earlier. Let me know if this helps or if you need further clarification.
hey pete, i had the same problem. try using puppeteer-extra with the adblocker plugin instead. it’s way easier:
npm i puppeteer-extra puppeteer-extra-plugin-adblocker
then just:
const puppeteer = require('puppeteer-extra')
puppeteer.use(require('puppeteer-extra-plugin-adblocker')())
works like a charm for me. lemme know if u need more help!