Skip to content

Fix async page.route #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## 1.0.3
- Fix when custom headers are not properly applied for async version

## 1.0.2
- Fix compatibility issues with Python 3.8 and above

Expand Down
12 changes: 8 additions & 4 deletions playwright_stealth/stealth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from typing import Union
from playwright.async_api import Page as AsyncPage
from playwright.sync_api import Page as SyncPage
from playwright_stealth.core import StealthConfig
Expand All @@ -17,16 +16,21 @@ def combine_scripts(properties: Properties, config: StealthConfig):
return "\n".join(scripts)


def generate_stealth_headers(properties: Properties, page: Union[AsyncPage, SyncPage]):
def generate_stealth_headers_sync(properties: Properties, page: SyncPage):
"""Generates the stealth headers for the page by replacing the original headers with the spoofed ones for every request."""
page.route("**/*", lambda route: route.continue_(headers=properties.as_dict()["header"]))


async def generate_stealth_headers_async(properties: Properties, page: AsyncPage):
"""Generates the stealth headers for the page by replacing the original headers with the spoofed ones for every request."""
await page.route("**/*", lambda route: route.continue_(headers=properties.as_dict()["header"]))


def stealth_sync(page: SyncPage, config: StealthConfig = None):
"""teaches synchronous playwright Page to be stealthy like a ninja!"""
properties = Properties()
combined_script = combine_scripts(properties, config)
generate_stealth_headers(properties, page)
generate_stealth_headers_sync(properties, page)

page.add_init_script(combined_script)

Expand All @@ -35,6 +39,6 @@ async def stealth_async(page: AsyncPage, config: StealthConfig = None):
"""teaches asynchronous playwright Page to be stealthy like a ninja!"""
properties = Properties()
combined_script = combine_scripts(properties, config)
generate_stealth_headers(properties, page)
await generate_stealth_headers_async(properties, page)

await page.add_init_script(combined_script)
30 changes: 15 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
[tool.poetry]
name = "tf-playwright-stealth"
packages = [
{ include = "playwright_stealth" },
]
version = "1.0.2"
packages = [{ include = "playwright_stealth" }]
version = "1.0.3"
description = "Makes playwright stealthy like a ninja!"
authors = []
homepage = "https://www.agentql.com/"
Expand Down Expand Up @@ -36,4 +34,4 @@ profile = "black"
line_length = 100
wrap_length = 100
multi_line_output = 3
include_trailing_comma = true
include_trailing_comma = true
Loading