daijro / camoufox

🦊 Anti-detect browser
https://camoufox.com
Mozilla Public License 2.0
640 stars 49 forks source link

Network Request Interception Documentation or Example? #59

Closed collynash closed 2 weeks ago

collynash commented 2 weeks ago

In playwright one can intercept network requests with something like this as follows:

from playwright.async_api import Response, async_playwright

async def handle_response(response: Response):
    """Processing code here"""
    if response.request.method == 'GET' and 'sample/request/path' in response.url:
        ....

async def run():
    async with async_playwright() as playwright:
        chromium = playwright.chromium
        browser = <playwright-browser-code>
        page = browser.pages
        # Attach the event listener
        page.on('response', handle_response)
    ...

How does one achieve something similar with camoufox to intercept target network request responses?

daijro commented 2 weeks ago

Sure, here's a reproducible example:

from camoufox.async_api import AsyncCamoufox
from playwright.async_api import Response

async def handle_response(response: Response):
    """Processing code here"""
    if response.request.method == 'GET':
        print('GET request:', response.url)

async def run():
    async with AsyncCamoufox() as browser:
        page = await browser.new_page()
        # Attach the event listener
        page.on('response', handle_response)
        await page.goto('https://example.com')

if __name__ == '__main__':
    import asyncio
    asyncio.run(run())

The Camoufox browser is a drop in replacement for Playwright's browser. All new pages are Playwright pages, so your original Playwright code will work for it as well.