
A plugin library for any Playwright-based library that allows blocking ads, domains, and resource types through injectors.

Open-source web application security scanner for automated vulnerability detection, manual penetration testing, and API security testing with a proxy-based interception engine.

Fast YAML-based vulnerability scanner with template-driven detection engine for automated security testing across web apps, APIs, networks, DNS, and cloud infrastructure.

Automatic SQL injection and database takeover tool
A plugin library for any Playwright-based library that allows blocking ads, domains, and resource types through injectors. Supports sync and async pages.
This library was ported from the blocking code inside Scrapling.
The package uses only the Python standard library.
Requires Python 3.10 or newer.
python -m pip install playwright-blocker
from playwright.sync_api import sync_playwright
from playwright_blocker import inject
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
try:
context = browser.new_context(service_workers="block")
page = context.new_page()
inject(page)
page.goto("https://example.com")
print(page.title())
finally:
browser.close()
import asyncio
from playwright.async_api import async_playwright
from playwright_blocker import inject_async
async def main():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch()
try:
context = await browser.new_context(service_workers="block")
page = await context.new_page()
await inject_async(page)
await page.goto("https://example.com")
print(await page.title())
finally:
await browser.close()
asyncio.run(main())
These examples use Playwright. The same inject and inject_async calls work with pages from any Playwright-based library.
Both functions take a page as their first argument. Set the other arguments by name.
| Argument | Default | Effect |
|---|---|---|
block_ads | True | Block the bundled ad and tracking domains. |
blocked_domains | None | Add domain names to block. Accepts a set or another iterable of strings. |
disable_resources | False | Block the resource types listed below. |
Logging is automatic. Blocked requests are logged at DEBUG level.
handler = inject(
page,
blocked_domains={"ads.example.com", "tracker.example.net"},
disable_resources=True,
)
Use lowercase hostnames without a scheme, path or port. A domain also blocks its subdomains: example.com matches cdn.example.com, but does not match notexample.com. Input sets are copied and are not changed.
Bundled ad domains are allowed when navigating the main page. Ad scripts, images, and iframes are still blocked. Domains passed in blocked_domains also block main-page navigation.
disable_resources=True blocks these resource types: font, image, media, beacon, object, imageset, texttrack, websocket, csp_report, and stylesheet. Only resource types reported by the driver's HTTP route API can be blocked. This option does not intercept WebSocket connections.
To use only your own domains, pass block_ads=False. If all options are off, no route is added.
From this directory:
python -m pip install -e ".[test]" mypy pyright ruff build twine tox
python -m playwright install chromium
python -m patchright install chromium
python -m pytest
mypy src/
pyright
ruff check .
ruff format --check .
python -m build
python -m twine check dist/*
The browser tests run in sequence and use a local HTTP server. They check sync and async page routing and reject browser contexts.
The bundled domains were extracted from Peter Lowe's ad and tracking server list, using its plain hostname format. Source credit is also kept in the data module.
The code is under the BSD 3-Clause license.