Playwright - How to combine the adblocker with other, simpler blocking rules? #2333
Unanswered
paul-norman
asked this question in
Q&A
Replies: 1 comment 4 replies
-
Hi @paul-norman, In general it is a bit tricky to combine the const { chromium } = require('playwright-chromium');
const { PlaywrightBlocker } = require('@cliqz/adblocker-playwright');
const fetch = require('cross-fetch');
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
const page = await context.newPage();
// Comment this block and images can be blocked...
PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => {
blocker
.blockFonts();
.blockImages();
.enableBlockingInPage(page);
});
await page.goto('https://duckduckgo.com/?q=butterflies', { waitUntil: 'load' }); The above should work (I did not test it). There are a few more methods like |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to use your own
page.route
rule to intercept requests while also using the adblocker?For example, I would like to block all fonts and images because they are of no use to me (they simply consume bandwidth):
Unless the adblocker lines are commented out, the images all load (and in fact never pass through the
page.route
function).I've only been using Playwright for a few days and am new to Node too, so it is quite possibly that I am doing something fundamentally wrong, but there doesn't seem to be another way to abort requests in Playwright (whereas
request.abort()
in thepage.on('request')
could be used in Puppeteer).Can anyone shed some light on this use case?
Thanks in advance, and thanks for making a great tool that can be plugged in so easily!
Beta Was this translation helpful? Give feedback.
All reactions