common扩展

This commit is contained in:
2026-06-11 14:38:23 +08:00
parent 919840a893
commit 02a4484211
14 changed files with 299 additions and 127 deletions
+5 -21
View File
@@ -15,6 +15,7 @@ from typing import Any
from app.core.config import Settings
from app.core.errors import BrowserUnavailableError, ResourceBusyError
from surugaya_common.browser_profile import DEFAULT_PAGE_HEADERS, stealth_launch_args
logger = logging.getLogger(__name__)
@@ -80,28 +81,17 @@ class BrowserPool:
# 用 playwright-stealth 钩子包装 playwright:后续 new_context/new_page
# 会自动应用 stealth(含 HTTP sec-ch-ua 客户端提示),并令 navigator.languages
# 与日语环境保持一致,避免指纹自相矛盾。
from playwright_stealth import Stealth
from surugaya_common.stealth import create_stealth
stealth = Stealth(
navigator_languages_override=("ja-JP", "ja", "en-US", "en"),
)
self._playwright_cm = stealth.use_async(async_playwright())
self._playwright_cm = create_stealth().use_async(async_playwright())
self._playwright = await self._playwright_cm.__aenter__()
else:
self._playwright_cm = None
self._playwright = await async_playwright().start()
chromium = self._playwright.chromium
launch_args = [
"--disable-blink-features=AutomationControlled",
"--disable-dev-shm-usage",
launch_args = stealth_launch_args() + [
"--disable-web-security",
"--no-first-run",
"--disable-infobars",
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-gpu",
"--use-mock-keychain",
"--password-store=basic",
]
# 本地可视化调试时强制窗口化,避免浏览器启动后不可见
if not headless:
@@ -305,13 +295,7 @@ class BrowserPool:
async def _configure_page(context: Any) -> Any:
"""在上下文中创建页面并设置反检测请求头。"""
page = await context.new_page()
await page.set_extra_http_headers(
{
"Accept-Language": "ja-JP,ja;q=0.9,en-US;q=0.8,en;q=0.7",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
}
)
await page.set_extra_http_headers(DEFAULT_PAGE_HEADERS)
return page
async def create_persistent_context(self, storage_state: dict[str, Any] | None = None) -> Any: