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.

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.

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

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.2
File Size Uploaded Provenance Actions
playwright_blocker-1.0.2.tar.gz 38.7 kB With provenance Details for playwright_blocker-1.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 69.8 kB

Release files / playwright_blocker-1.0.2.tar.gz

Download URL playwright_blocker-1.0.2.tar.gz
Size 38.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6d22663e67cc21801145f06bf4e0b5ccc40dc7987c44e8ddf8279ec53d3f2aa7
BLAKE2b-256 checksum
How to use checksums
fa456fa752adb2d9f814c2de2c2ba0722f36f43984e698462fa1982dd98388c0
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 5, 2026.

Transparency log

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

Download URL playwright_blocker-1.0.2-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
befafdc85fdeb13ae578d9c9b1795b167da53324becd41a520098e878b853101
BLAKE2b-256 checksum
How to use checksums
df496e07569f8118411c5f23ab636ba30de48fa840465f6bca7ddb4acda5d905
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 5, 2026.

Transparency log

Security for playwright-blocker 1.0.2

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.2.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 5, 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