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.1

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.1
File Size Uploaded Provenance Actions
playwright_blocker-1.0.1.tar.gz 37.7 kB With provenance Details for playwright_blocker-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 68.7 kB

Release files / playwright_blocker-1.0.1.tar.gz

Download URL playwright_blocker-1.0.1.tar.gz
Size 37.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4f71ee95abe3e097aa8732b2fc4daa1be70c4fc0dc13801eb504bb080148cbab
BLAKE2b-256 checksum
How to use checksums
7320235a48f7de0c47b12872914c0fc3c073afbc4f7fb8477fe46b0e8d70ffc7
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 4, 2026.

Transparency log

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

Download URL playwright_blocker-1.0.1-py3-none-any.whl
Size 31.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9fcb18a773fb48343ac26be24b032a48194f01eed48a58b48da72f4fe815a8f1
BLAKE2b-256 checksum
How to use checksums
9bd55e8dc9f45360e5ff90f31451cf209bcde5025a69c7c4a0a386ec0f1ae5d4
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 4, 2026.

Transparency log

Security for playwright-blocker 1.0.1

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.1.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 4, 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