Skip to main content

playwright-blocker

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.

Install

Requires Python 3.10 or newer.

python -m pip install playwright-blocker

Sync

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()

Async

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.

Options

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.

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.

Development

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.

Domain list and license

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.


Designed & crafted with ❤️ by Karim Shoair.

Metadata

Release files for playwright-blocker 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for playwright-blocker 1.0.0
File Size Uploaded Provenance Actions
playwright_blocker-1.0.0.tar.gz 37.3 kB With provenance Details for playwright_blocker-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for playwright-blocker 1.0.0
File Interpreter ABI Platform Actions
playwright_blocker-1.0.0-py3-none-any.whl Python 3 none any Details for playwright_blocker-1.0.0-py3-none-any.whl

Total release size: 68.2 kB

Release files / playwright_blocker-1.0.0.tar.gz

Download URL playwright_blocker-1.0.0.tar.gz
Size 37.3 kB
Tags Source
SHA-256 checksum
How to use checksums
69f8491072a4467021361368228a6f4ace7657bd5169b91edcb474c1ff2645cb
BLAKE2b-256 checksum
How to use checksums
19e0af8abf25196b90188742837dc2518e43c67e0834b137ac2bff9c62f8c004
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

Release files / playwright_blocker-1.0.0-py3-none-any.whl

Download URL playwright_blocker-1.0.0-py3-none-any.whl
Size 30.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3eed1b707f3c95377a264c0cafceab03ceff13900195f490cb73871fac6761b
BLAKE2b-256 checksum
How to use checksums
6dfa3c41a9c3a54b4a515d04856a3dcfe99420e566869ea95477b6c9ad25d2e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

Security for playwright-blocker 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for playwright_blocker-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page