|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# @Time : 2025/1/1 |
| 3 | +# @Author : wenshao |
| 4 | + |
| 5 | +# @Project : browser-use-webui |
| 6 | +# @FileName: context.py |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import base64 |
| 10 | +import json |
| 11 | +import logging |
| 12 | +import os |
| 13 | + |
| 14 | +from playwright.async_api import Browser as PlaywrightBrowser |
| 15 | +from browser_use.browser.context import BrowserContext, BrowserContextConfig |
| 16 | +from browser_use.browser.browser import Browser |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class CustomBrowserContext(BrowserContext): |
| 22 | + """ |
| 23 | + 定制BrowserContext |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, |
| 27 | + browser: 'Browser', |
| 28 | + config: BrowserContextConfig = BrowserContextConfig(), |
| 29 | + ): |
| 30 | + super(CustomBrowserContext, self).__init__(browser, config) |
| 31 | + |
| 32 | + async def _create_context(self, browser: PlaywrightBrowser): |
| 33 | + """Creates a new browser context with anti-detection measures and loads cookies if available.""" |
| 34 | + if self.browser.config.chrome_instance_path and len(browser.contexts) > 0: |
| 35 | + # Connect to existing Chrome instance instead of creating new one |
| 36 | + context = browser.contexts[0] |
| 37 | + else: |
| 38 | + # Original code for creating new context |
| 39 | + context = await browser.new_context( |
| 40 | + viewport=self.config.browser_window_size, |
| 41 | + no_viewport=False, |
| 42 | + user_agent=( |
| 43 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' |
| 44 | + '(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36' |
| 45 | + ), |
| 46 | + java_script_enabled=True, |
| 47 | + bypass_csp=self.config.disable_security, |
| 48 | + ignore_https_errors=self.config.disable_security, |
| 49 | + record_video_dir=self.config.save_recording_path, |
| 50 | + record_video_size=self.config.browser_window_size # set record video size |
| 51 | + ) |
| 52 | + |
| 53 | + if self.config.trace_path: |
| 54 | + await context.tracing.start(screenshots=True, snapshots=True, sources=True) |
| 55 | + |
| 56 | + # Load cookies if they exist |
| 57 | + if self.config.cookies_file and os.path.exists(self.config.cookies_file): |
| 58 | + with open(self.config.cookies_file, 'r') as f: |
| 59 | + cookies = json.load(f) |
| 60 | + logger.info(f'Loaded {len(cookies)} cookies from {self.config.cookies_file}') |
| 61 | + await context.add_cookies(cookies) |
| 62 | + |
| 63 | + # Expose anti-detection scripts |
| 64 | + await context.add_init_script( |
| 65 | + """ |
| 66 | + // Webdriver property |
| 67 | + Object.defineProperty(navigator, 'webdriver', { |
| 68 | + get: () => undefined |
| 69 | + }); |
| 70 | +
|
| 71 | + // Languages |
| 72 | + Object.defineProperty(navigator, 'languages', { |
| 73 | + get: () => ['en-US', 'en'] |
| 74 | + }); |
| 75 | +
|
| 76 | + // Plugins |
| 77 | + Object.defineProperty(navigator, 'plugins', { |
| 78 | + get: () => [1, 2, 3, 4, 5] |
| 79 | + }); |
| 80 | +
|
| 81 | + // Chrome runtime |
| 82 | + window.chrome = { runtime: {} }; |
| 83 | +
|
| 84 | + // Permissions |
| 85 | + const originalQuery = window.navigator.permissions.query; |
| 86 | + window.navigator.permissions.query = (parameters) => ( |
| 87 | + parameters.name === 'notifications' ? |
| 88 | + Promise.resolve({ state: Notification.permission }) : |
| 89 | + originalQuery(parameters) |
| 90 | + ); |
| 91 | + """ |
| 92 | + ) |
| 93 | + |
| 94 | + return context |
0 commit comments