optexity-browser-use 0.9.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- browser_use/__init__.py +157 -0
- browser_use/actor/__init__.py +11 -0
- browser_use/actor/element.py +1175 -0
- browser_use/actor/mouse.py +134 -0
- browser_use/actor/page.py +561 -0
- browser_use/actor/playground/flights.py +41 -0
- browser_use/actor/playground/mixed_automation.py +54 -0
- browser_use/actor/playground/playground.py +236 -0
- browser_use/actor/utils.py +176 -0
- browser_use/agent/cloud_events.py +282 -0
- browser_use/agent/gif.py +424 -0
- browser_use/agent/judge.py +170 -0
- browser_use/agent/message_manager/service.py +473 -0
- browser_use/agent/message_manager/utils.py +52 -0
- browser_use/agent/message_manager/views.py +98 -0
- browser_use/agent/prompts.py +413 -0
- browser_use/agent/service.py +2316 -0
- browser_use/agent/system_prompt.md +185 -0
- browser_use/agent/system_prompt_flash.md +10 -0
- browser_use/agent/system_prompt_no_thinking.md +183 -0
- browser_use/agent/views.py +743 -0
- browser_use/browser/__init__.py +41 -0
- browser_use/browser/cloud/cloud.py +203 -0
- browser_use/browser/cloud/views.py +89 -0
- browser_use/browser/events.py +578 -0
- browser_use/browser/profile.py +1158 -0
- browser_use/browser/python_highlights.py +548 -0
- browser_use/browser/session.py +3225 -0
- browser_use/browser/session_manager.py +399 -0
- browser_use/browser/video_recorder.py +162 -0
- browser_use/browser/views.py +200 -0
- browser_use/browser/watchdog_base.py +260 -0
- browser_use/browser/watchdogs/__init__.py +0 -0
- browser_use/browser/watchdogs/aboutblank_watchdog.py +253 -0
- browser_use/browser/watchdogs/crash_watchdog.py +335 -0
- browser_use/browser/watchdogs/default_action_watchdog.py +2729 -0
- browser_use/browser/watchdogs/dom_watchdog.py +817 -0
- browser_use/browser/watchdogs/downloads_watchdog.py +1277 -0
- browser_use/browser/watchdogs/local_browser_watchdog.py +461 -0
- browser_use/browser/watchdogs/permissions_watchdog.py +43 -0
- browser_use/browser/watchdogs/popups_watchdog.py +143 -0
- browser_use/browser/watchdogs/recording_watchdog.py +126 -0
- browser_use/browser/watchdogs/screenshot_watchdog.py +62 -0
- browser_use/browser/watchdogs/security_watchdog.py +280 -0
- browser_use/browser/watchdogs/storage_state_watchdog.py +335 -0
- browser_use/cli.py +2359 -0
- browser_use/code_use/__init__.py +16 -0
- browser_use/code_use/formatting.py +192 -0
- browser_use/code_use/namespace.py +665 -0
- browser_use/code_use/notebook_export.py +276 -0
- browser_use/code_use/service.py +1340 -0
- browser_use/code_use/system_prompt.md +574 -0
- browser_use/code_use/utils.py +150 -0
- browser_use/code_use/views.py +171 -0
- browser_use/config.py +505 -0
- browser_use/controller/__init__.py +3 -0
- browser_use/dom/enhanced_snapshot.py +161 -0
- browser_use/dom/markdown_extractor.py +169 -0
- browser_use/dom/playground/extraction.py +312 -0
- browser_use/dom/playground/multi_act.py +32 -0
- browser_use/dom/serializer/clickable_elements.py +200 -0
- browser_use/dom/serializer/code_use_serializer.py +287 -0
- browser_use/dom/serializer/eval_serializer.py +478 -0
- browser_use/dom/serializer/html_serializer.py +212 -0
- browser_use/dom/serializer/paint_order.py +197 -0
- browser_use/dom/serializer/serializer.py +1170 -0
- browser_use/dom/service.py +825 -0
- browser_use/dom/utils.py +129 -0
- browser_use/dom/views.py +906 -0
- browser_use/exceptions.py +5 -0
- browser_use/filesystem/__init__.py +0 -0
- browser_use/filesystem/file_system.py +619 -0
- browser_use/init_cmd.py +376 -0
- browser_use/integrations/gmail/__init__.py +24 -0
- browser_use/integrations/gmail/actions.py +115 -0
- browser_use/integrations/gmail/service.py +225 -0
- browser_use/llm/__init__.py +155 -0
- browser_use/llm/anthropic/chat.py +242 -0
- browser_use/llm/anthropic/serializer.py +312 -0
- browser_use/llm/aws/__init__.py +36 -0
- browser_use/llm/aws/chat_anthropic.py +242 -0
- browser_use/llm/aws/chat_bedrock.py +289 -0
- browser_use/llm/aws/serializer.py +257 -0
- browser_use/llm/azure/chat.py +91 -0
- browser_use/llm/base.py +57 -0
- browser_use/llm/browser_use/__init__.py +3 -0
- browser_use/llm/browser_use/chat.py +201 -0
- browser_use/llm/cerebras/chat.py +193 -0
- browser_use/llm/cerebras/serializer.py +109 -0
- browser_use/llm/deepseek/chat.py +212 -0
- browser_use/llm/deepseek/serializer.py +109 -0
- browser_use/llm/exceptions.py +29 -0
- browser_use/llm/google/__init__.py +3 -0
- browser_use/llm/google/chat.py +542 -0
- browser_use/llm/google/serializer.py +120 -0
- browser_use/llm/groq/chat.py +229 -0
- browser_use/llm/groq/parser.py +158 -0
- browser_use/llm/groq/serializer.py +159 -0
- browser_use/llm/messages.py +238 -0
- browser_use/llm/models.py +271 -0
- browser_use/llm/oci_raw/__init__.py +10 -0
- browser_use/llm/oci_raw/chat.py +443 -0
- browser_use/llm/oci_raw/serializer.py +229 -0
- browser_use/llm/ollama/chat.py +97 -0
- browser_use/llm/ollama/serializer.py +143 -0
- browser_use/llm/openai/chat.py +264 -0
- browser_use/llm/openai/like.py +15 -0
- browser_use/llm/openai/serializer.py +165 -0
- browser_use/llm/openrouter/chat.py +211 -0
- browser_use/llm/openrouter/serializer.py +26 -0
- browser_use/llm/schema.py +176 -0
- browser_use/llm/views.py +48 -0
- browser_use/logging_config.py +330 -0
- browser_use/mcp/__init__.py +18 -0
- browser_use/mcp/__main__.py +12 -0
- browser_use/mcp/client.py +544 -0
- browser_use/mcp/controller.py +264 -0
- browser_use/mcp/server.py +1114 -0
- browser_use/observability.py +204 -0
- browser_use/py.typed +0 -0
- browser_use/sandbox/__init__.py +41 -0
- browser_use/sandbox/sandbox.py +637 -0
- browser_use/sandbox/views.py +132 -0
- browser_use/screenshots/__init__.py +1 -0
- browser_use/screenshots/service.py +52 -0
- browser_use/sync/__init__.py +6 -0
- browser_use/sync/auth.py +357 -0
- browser_use/sync/service.py +161 -0
- browser_use/telemetry/__init__.py +51 -0
- browser_use/telemetry/service.py +112 -0
- browser_use/telemetry/views.py +101 -0
- browser_use/tokens/__init__.py +0 -0
- browser_use/tokens/custom_pricing.py +24 -0
- browser_use/tokens/mappings.py +4 -0
- browser_use/tokens/service.py +580 -0
- browser_use/tokens/views.py +108 -0
- browser_use/tools/registry/service.py +572 -0
- browser_use/tools/registry/views.py +174 -0
- browser_use/tools/service.py +1675 -0
- browser_use/tools/utils.py +82 -0
- browser_use/tools/views.py +100 -0
- browser_use/utils.py +670 -0
- optexity_browser_use-0.9.5.dist-info/METADATA +344 -0
- optexity_browser_use-0.9.5.dist-info/RECORD +147 -0
- optexity_browser_use-0.9.5.dist-info/WHEEL +4 -0
- optexity_browser_use-0.9.5.dist-info/entry_points.txt +3 -0
- optexity_browser_use-0.9.5.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,1158 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import tempfile
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from functools import cache
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Annotated, Any, Literal, Self
|
|
8
|
+
from urllib.parse import urlparse
|
|
9
|
+
|
|
10
|
+
from pydantic import AfterValidator, AliasChoices, BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
11
|
+
|
|
12
|
+
from browser_use.browser.cloud.views import CloudBrowserParams
|
|
13
|
+
from browser_use.config import CONFIG
|
|
14
|
+
from browser_use.utils import _log_pretty_path, logger
|
|
15
|
+
|
|
16
|
+
CHROME_DEBUG_PORT = 9242 # use a non-default port to avoid conflicts with other tools / devs using 9222
|
|
17
|
+
DOMAIN_OPTIMIZATION_THRESHOLD = 100 # Convert domain lists to sets for O(1) lookup when >= this size
|
|
18
|
+
CHROME_DISABLED_COMPONENTS = [
|
|
19
|
+
# Playwright defaults: https://github.com/microsoft/playwright/blob/41008eeddd020e2dee1c540f7c0cdfa337e99637/packages/playwright-core/src/server/chromium/chromiumSwitches.ts#L76
|
|
20
|
+
# AcceptCHFrame,AutoExpandDetailsElement,AvoidUnnecessaryBeforeUnloadCheckSync,CertificateTransparencyComponentUpdater,DeferRendererTasksAfterInput,DestroyProfileOnBrowserClose,DialMediaRouteProvider,ExtensionManifestV2Disabled,GlobalMediaControls,HttpsUpgrades,ImprovedCookieControls,LazyFrameLoading,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate
|
|
21
|
+
# See https:#github.com/microsoft/playwright/pull/10380
|
|
22
|
+
'AcceptCHFrame',
|
|
23
|
+
# See https:#github.com/microsoft/playwright/pull/10679
|
|
24
|
+
'AutoExpandDetailsElement',
|
|
25
|
+
# See https:#github.com/microsoft/playwright/issues/14047
|
|
26
|
+
'AvoidUnnecessaryBeforeUnloadCheckSync',
|
|
27
|
+
# See https:#github.com/microsoft/playwright/pull/12992
|
|
28
|
+
'CertificateTransparencyComponentUpdater',
|
|
29
|
+
'DestroyProfileOnBrowserClose',
|
|
30
|
+
# See https:#github.com/microsoft/playwright/pull/13854
|
|
31
|
+
'DialMediaRouteProvider',
|
|
32
|
+
# Chromium is disabling manifest version 2. Allow testing it as long as Chromium can actually run it.
|
|
33
|
+
# Disabled in https:#chromium-review.googlesource.com/c/chromium/src/+/6265903.
|
|
34
|
+
'ExtensionManifestV2Disabled',
|
|
35
|
+
'GlobalMediaControls',
|
|
36
|
+
# See https:#github.com/microsoft/playwright/pull/27605
|
|
37
|
+
'HttpsUpgrades',
|
|
38
|
+
'ImprovedCookieControls',
|
|
39
|
+
'LazyFrameLoading',
|
|
40
|
+
# Hides the Lens feature in the URL address bar. Its not working in unofficial builds.
|
|
41
|
+
'LensOverlay',
|
|
42
|
+
# See https:#github.com/microsoft/playwright/pull/8162
|
|
43
|
+
'MediaRouter',
|
|
44
|
+
# See https:#github.com/microsoft/playwright/issues/28023
|
|
45
|
+
'PaintHolding',
|
|
46
|
+
# See https:#github.com/microsoft/playwright/issues/32230
|
|
47
|
+
'ThirdPartyStoragePartitioning',
|
|
48
|
+
# See https://github.com/microsoft/playwright/issues/16126
|
|
49
|
+
'Translate',
|
|
50
|
+
# 3
|
|
51
|
+
# Added by us:
|
|
52
|
+
'AutomationControlled',
|
|
53
|
+
'BackForwardCache',
|
|
54
|
+
'OptimizationHints',
|
|
55
|
+
'ProcessPerSiteUpToMainFrameThreshold',
|
|
56
|
+
'InterestFeedContentSuggestions',
|
|
57
|
+
'CalculateNativeWinOcclusion', # chrome normally stops rendering tabs if they are not visible (occluded by a foreground window or other app)
|
|
58
|
+
# 'BackForwardCache', # agent does actually use back/forward navigation, but we can disable if we ever remove that
|
|
59
|
+
'HeavyAdPrivacyMitigations',
|
|
60
|
+
'PrivacySandboxSettings4',
|
|
61
|
+
'AutofillServerCommunication',
|
|
62
|
+
'CrashReporting',
|
|
63
|
+
'OverscrollHistoryNavigation',
|
|
64
|
+
'InfiniteSessionRestore',
|
|
65
|
+
'ExtensionDisableUnsupportedDeveloper',
|
|
66
|
+
'ExtensionManifestV2Unsupported',
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
CHROME_HEADLESS_ARGS = [
|
|
70
|
+
'--headless=new',
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
CHROME_DOCKER_ARGS = [
|
|
74
|
+
# '--disable-gpu', # GPU is actually supported in headless docker mode now, but sometimes useful to test without it
|
|
75
|
+
'--no-sandbox',
|
|
76
|
+
'--disable-gpu-sandbox',
|
|
77
|
+
'--disable-setuid-sandbox',
|
|
78
|
+
'--disable-dev-shm-usage',
|
|
79
|
+
'--no-xshm',
|
|
80
|
+
'--no-zygote',
|
|
81
|
+
# '--single-process', # might be the cause of "Target page, context or browser has been closed" errors during CDP page.captureScreenshot https://stackoverflow.com/questions/51629151/puppeteer-protocol-error-page-navigate-target-closed
|
|
82
|
+
'--disable-site-isolation-trials', # lowers RAM use by 10-16% in docker, but could lead to easier bot blocking if pages can detect it?
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
CHROME_DISABLE_SECURITY_ARGS = [
|
|
87
|
+
'--disable-site-isolation-trials',
|
|
88
|
+
'--disable-web-security',
|
|
89
|
+
'--disable-features=IsolateOrigins,site-per-process',
|
|
90
|
+
'--allow-running-insecure-content',
|
|
91
|
+
'--ignore-certificate-errors',
|
|
92
|
+
'--ignore-ssl-errors',
|
|
93
|
+
'--ignore-certificate-errors-spki-list',
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
CHROME_DETERMINISTIC_RENDERING_ARGS = [
|
|
97
|
+
'--deterministic-mode',
|
|
98
|
+
'--js-flags=--random-seed=1157259159',
|
|
99
|
+
'--force-device-scale-factor=2',
|
|
100
|
+
'--enable-webgl',
|
|
101
|
+
# '--disable-skia-runtime-opts',
|
|
102
|
+
# '--disable-2d-canvas-clip-aa',
|
|
103
|
+
'--font-render-hinting=none',
|
|
104
|
+
'--force-color-profile=srgb',
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
CHROME_DEFAULT_ARGS = [
|
|
108
|
+
# # provided by playwright by default: https://github.com/microsoft/playwright/blob/41008eeddd020e2dee1c540f7c0cdfa337e99637/packages/playwright-core/src/server/chromium/chromiumSwitches.ts#L76
|
|
109
|
+
'--disable-field-trial-config', # https://source.chromium.org/chromium/chromium/src/+/main:testing/variations/README.md
|
|
110
|
+
'--disable-background-networking',
|
|
111
|
+
'--disable-background-timer-throttling', # agents might be working on background pages if the human switches to another tab
|
|
112
|
+
'--disable-backgrounding-occluded-windows', # same deal, agents are often working on backgrounded browser windows
|
|
113
|
+
'--disable-back-forward-cache', # Avoids surprises like main request not being intercepted during page.goBack().
|
|
114
|
+
'--disable-breakpad',
|
|
115
|
+
'--disable-client-side-phishing-detection',
|
|
116
|
+
'--disable-component-extensions-with-background-pages',
|
|
117
|
+
'--disable-component-update', # Avoids unneeded network activity after startup.
|
|
118
|
+
'--no-default-browser-check',
|
|
119
|
+
# '--disable-default-apps',
|
|
120
|
+
'--disable-dev-shm-usage', # crucial for docker support, harmless in non-docker environments
|
|
121
|
+
# '--disable-extensions',
|
|
122
|
+
# '--disable-features=' + disabledFeatures(assistantMode).join(','),
|
|
123
|
+
# '--allow-pre-commit-input', # duplicate removed
|
|
124
|
+
'--disable-hang-monitor',
|
|
125
|
+
'--disable-ipc-flooding-protection', # important to be able to make lots of CDP calls in a tight loop
|
|
126
|
+
'--disable-popup-blocking',
|
|
127
|
+
'--disable-prompt-on-repost',
|
|
128
|
+
'--disable-renderer-backgrounding',
|
|
129
|
+
# '--force-color-profile=srgb', # moved to CHROME_DETERMINISTIC_RENDERING_ARGS
|
|
130
|
+
'--metrics-recording-only',
|
|
131
|
+
'--no-first-run',
|
|
132
|
+
# // See https://chromium-review.googlesource.com/c/chromium/src/+/2436773
|
|
133
|
+
'--no-service-autorun',
|
|
134
|
+
'--export-tagged-pdf',
|
|
135
|
+
# // https://chromium-review.googlesource.com/c/chromium/src/+/4853540
|
|
136
|
+
'--disable-search-engine-choice-screen',
|
|
137
|
+
# // https://issues.chromium.org/41491762
|
|
138
|
+
'--unsafely-disable-devtools-self-xss-warnings',
|
|
139
|
+
# added by us:
|
|
140
|
+
'--enable-features=NetworkService,NetworkServiceInProcess',
|
|
141
|
+
'--enable-network-information-downlink-max',
|
|
142
|
+
'--test-type=gpu',
|
|
143
|
+
'--disable-sync',
|
|
144
|
+
'--allow-legacy-extension-manifests',
|
|
145
|
+
'--allow-pre-commit-input',
|
|
146
|
+
'--disable-blink-features=AutomationControlled',
|
|
147
|
+
'--install-autogenerated-theme=0,0,0',
|
|
148
|
+
# '--hide-scrollbars', # leave them visible! the agent uses them to know when it needs to scroll to see more options
|
|
149
|
+
'--log-level=2',
|
|
150
|
+
# '--enable-logging=stderr',
|
|
151
|
+
'--disable-focus-on-load',
|
|
152
|
+
'--disable-window-activation',
|
|
153
|
+
'--generate-pdf-document-outline',
|
|
154
|
+
'--no-pings',
|
|
155
|
+
'--ash-no-nudges',
|
|
156
|
+
'--disable-infobars',
|
|
157
|
+
'--simulate-outdated-no-au="Tue, 31 Dec 2099 23:59:59 GMT"',
|
|
158
|
+
'--hide-crash-restore-bubble',
|
|
159
|
+
'--suppress-message-center-popups',
|
|
160
|
+
'--disable-domain-reliability',
|
|
161
|
+
'--disable-datasaver-prompt',
|
|
162
|
+
'--disable-speech-synthesis-api',
|
|
163
|
+
'--disable-speech-api',
|
|
164
|
+
'--disable-print-preview',
|
|
165
|
+
'--safebrowsing-disable-auto-update',
|
|
166
|
+
'--disable-external-intent-requests',
|
|
167
|
+
'--disable-desktop-notifications',
|
|
168
|
+
'--noerrdialogs',
|
|
169
|
+
'--silent-debugger-extension-api',
|
|
170
|
+
# Extension welcome tab suppression for automation
|
|
171
|
+
'--disable-extensions-http-throttling',
|
|
172
|
+
'--extensions-on-chrome-urls',
|
|
173
|
+
'--disable-default-apps',
|
|
174
|
+
f'--disable-features={",".join(CHROME_DISABLED_COMPONENTS)}',
|
|
175
|
+
]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class ViewportSize(BaseModel):
|
|
179
|
+
width: int = Field(ge=0)
|
|
180
|
+
height: int = Field(ge=0)
|
|
181
|
+
|
|
182
|
+
def __getitem__(self, key: str) -> int:
|
|
183
|
+
return dict(self)[key]
|
|
184
|
+
|
|
185
|
+
def __setitem__(self, key: str, value: int) -> None:
|
|
186
|
+
setattr(self, key, value)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@cache
|
|
190
|
+
def get_display_size() -> ViewportSize | None:
|
|
191
|
+
# macOS
|
|
192
|
+
try:
|
|
193
|
+
from AppKit import NSScreen # type: ignore[import]
|
|
194
|
+
|
|
195
|
+
screen = NSScreen.mainScreen().frame()
|
|
196
|
+
size = ViewportSize(width=int(screen.size.width), height=int(screen.size.height))
|
|
197
|
+
logger.debug(f'Display size: {size}')
|
|
198
|
+
return size
|
|
199
|
+
except Exception:
|
|
200
|
+
pass
|
|
201
|
+
|
|
202
|
+
# Windows & Linux
|
|
203
|
+
try:
|
|
204
|
+
from screeninfo import get_monitors
|
|
205
|
+
|
|
206
|
+
monitors = get_monitors()
|
|
207
|
+
monitor = monitors[0]
|
|
208
|
+
size = ViewportSize(width=int(monitor.width), height=int(monitor.height))
|
|
209
|
+
logger.debug(f'Display size: {size}')
|
|
210
|
+
return size
|
|
211
|
+
except Exception:
|
|
212
|
+
pass
|
|
213
|
+
|
|
214
|
+
logger.debug('No display size found')
|
|
215
|
+
return None
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def get_window_adjustments() -> tuple[int, int]:
|
|
219
|
+
"""Returns recommended x, y offsets for window positioning"""
|
|
220
|
+
|
|
221
|
+
if sys.platform == 'darwin': # macOS
|
|
222
|
+
return -4, 24 # macOS has a small title bar, no border
|
|
223
|
+
elif sys.platform == 'win32': # Windows
|
|
224
|
+
return -8, 0 # Windows has a border on the left
|
|
225
|
+
else: # Linux
|
|
226
|
+
return 0, 0
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def validate_url(url: str, schemes: Iterable[str] = ()) -> str:
|
|
230
|
+
"""Validate URL format and optionally check for specific schemes."""
|
|
231
|
+
parsed_url = urlparse(url)
|
|
232
|
+
if not parsed_url.netloc:
|
|
233
|
+
raise ValueError(f'Invalid URL format: {url}')
|
|
234
|
+
if schemes and parsed_url.scheme and parsed_url.scheme.lower() not in schemes:
|
|
235
|
+
raise ValueError(f'URL has invalid scheme: {url} (expected one of {schemes})')
|
|
236
|
+
return url
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def validate_float_range(value: float, min_val: float, max_val: float) -> float:
|
|
240
|
+
"""Validate that float is within specified range."""
|
|
241
|
+
if not min_val <= value <= max_val:
|
|
242
|
+
raise ValueError(f'Value {value} outside of range {min_val}-{max_val}')
|
|
243
|
+
return value
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def validate_cli_arg(arg: str) -> str:
|
|
247
|
+
"""Validate that arg is a valid CLI argument."""
|
|
248
|
+
if not arg.startswith('--'):
|
|
249
|
+
raise ValueError(f'Invalid CLI argument: {arg} (should start with --, e.g. --some-key="some value here")')
|
|
250
|
+
return arg
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# ===== Enum definitions =====
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class RecordHarContent(str, Enum):
|
|
257
|
+
OMIT = 'omit'
|
|
258
|
+
EMBED = 'embed'
|
|
259
|
+
ATTACH = 'attach'
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
class RecordHarMode(str, Enum):
|
|
263
|
+
FULL = 'full'
|
|
264
|
+
MINIMAL = 'minimal'
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
class BrowserChannel(str, Enum):
|
|
268
|
+
CHROMIUM = 'chromium'
|
|
269
|
+
CHROME = 'chrome'
|
|
270
|
+
CHROME_BETA = 'chrome-beta'
|
|
271
|
+
CHROME_DEV = 'chrome-dev'
|
|
272
|
+
CHROME_CANARY = 'chrome-canary'
|
|
273
|
+
MSEDGE = 'msedge'
|
|
274
|
+
MSEDGE_BETA = 'msedge-beta'
|
|
275
|
+
MSEDGE_DEV = 'msedge-dev'
|
|
276
|
+
MSEDGE_CANARY = 'msedge-canary'
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
# Using constants from central location in browser_use.config
|
|
280
|
+
BROWSERUSE_DEFAULT_CHANNEL = BrowserChannel.CHROMIUM
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
# ===== Type definitions with validators =====
|
|
284
|
+
|
|
285
|
+
UrlStr = Annotated[str, AfterValidator(validate_url)]
|
|
286
|
+
NonNegativeFloat = Annotated[float, AfterValidator(lambda x: validate_float_range(x, 0, float('inf')))]
|
|
287
|
+
CliArgStr = Annotated[str, AfterValidator(validate_cli_arg)]
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
# ===== Base Models =====
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
class BrowserContextArgs(BaseModel):
|
|
294
|
+
"""
|
|
295
|
+
Base model for common browser context parameters used by
|
|
296
|
+
both BrowserType.new_context() and BrowserType.launch_persistent_context().
|
|
297
|
+
|
|
298
|
+
https://playwright.dev/python/docs/api/class-browser#browser-new-context
|
|
299
|
+
"""
|
|
300
|
+
|
|
301
|
+
model_config = ConfigDict(extra='ignore', validate_assignment=False, revalidate_instances='always', populate_by_name=True)
|
|
302
|
+
|
|
303
|
+
# Browser context parameters
|
|
304
|
+
accept_downloads: bool = True
|
|
305
|
+
|
|
306
|
+
# Security options
|
|
307
|
+
# proxy: ProxySettings | None = None
|
|
308
|
+
permissions: list[str] = Field(
|
|
309
|
+
default_factory=lambda: ['clipboardReadWrite', 'notifications'],
|
|
310
|
+
description='Browser permissions to grant (CDP Browser.grantPermissions).',
|
|
311
|
+
# clipboardReadWrite is for google sheets and pyperclip automations
|
|
312
|
+
# notifications are to avoid browser fingerprinting
|
|
313
|
+
)
|
|
314
|
+
# client_certificates: list[ClientCertificate] = Field(default_factory=list)
|
|
315
|
+
# http_credentials: HttpCredentials | None = None
|
|
316
|
+
|
|
317
|
+
# Viewport options
|
|
318
|
+
user_agent: str | None = None
|
|
319
|
+
screen: ViewportSize | None = None
|
|
320
|
+
viewport: ViewportSize | None = Field(default=None)
|
|
321
|
+
no_viewport: bool | None = None
|
|
322
|
+
device_scale_factor: NonNegativeFloat | None = None
|
|
323
|
+
# geolocation: Geolocation | None = None
|
|
324
|
+
|
|
325
|
+
# Recording Options
|
|
326
|
+
record_har_content: RecordHarContent = RecordHarContent.EMBED
|
|
327
|
+
record_har_mode: RecordHarMode = RecordHarMode.FULL
|
|
328
|
+
record_har_path: str | Path | None = Field(default=None, validation_alias=AliasChoices('save_har_path', 'record_har_path'))
|
|
329
|
+
record_video_dir: str | Path | None = Field(
|
|
330
|
+
default=None, validation_alias=AliasChoices('save_recording_path', 'record_video_dir')
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
class BrowserConnectArgs(BaseModel):
|
|
335
|
+
"""
|
|
336
|
+
Base model for common browser connect parameters used by
|
|
337
|
+
both connect_over_cdp() and connect_over_ws().
|
|
338
|
+
|
|
339
|
+
https://playwright.dev/python/docs/api/class-browsertype#browser-type-connect
|
|
340
|
+
https://playwright.dev/python/docs/api/class-browsertype#browser-type-connect-over-cdp
|
|
341
|
+
"""
|
|
342
|
+
|
|
343
|
+
model_config = ConfigDict(extra='ignore', validate_assignment=True, revalidate_instances='always', populate_by_name=True)
|
|
344
|
+
|
|
345
|
+
headers: dict[str, str] | None = Field(default=None, description='Additional HTTP headers to be sent with connect request')
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
class BrowserLaunchArgs(BaseModel):
|
|
349
|
+
"""
|
|
350
|
+
Base model for common browser launch parameters used by
|
|
351
|
+
both launch() and launch_persistent_context().
|
|
352
|
+
|
|
353
|
+
https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch
|
|
354
|
+
"""
|
|
355
|
+
|
|
356
|
+
model_config = ConfigDict(
|
|
357
|
+
extra='ignore',
|
|
358
|
+
validate_assignment=True,
|
|
359
|
+
revalidate_instances='always',
|
|
360
|
+
from_attributes=True,
|
|
361
|
+
validate_by_name=True,
|
|
362
|
+
validate_by_alias=True,
|
|
363
|
+
populate_by_name=True,
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
env: dict[str, str | float | bool] | None = Field(
|
|
367
|
+
default=None,
|
|
368
|
+
description='Extra environment variables to set when launching the browser. If None, inherits from the current process.',
|
|
369
|
+
)
|
|
370
|
+
executable_path: str | Path | None = Field(
|
|
371
|
+
default=None,
|
|
372
|
+
validation_alias=AliasChoices('browser_binary_path', 'chrome_binary_path'),
|
|
373
|
+
description='Path to the chromium-based browser executable to use.',
|
|
374
|
+
)
|
|
375
|
+
headless: bool | None = Field(default=None, description='Whether to run the browser in headless or windowed mode.')
|
|
376
|
+
args: list[CliArgStr] = Field(
|
|
377
|
+
default_factory=list, description='List of *extra* CLI args to pass to the browser when launching.'
|
|
378
|
+
)
|
|
379
|
+
ignore_default_args: list[CliArgStr] | Literal[True] = Field(
|
|
380
|
+
default_factory=lambda: [
|
|
381
|
+
'--enable-automation', # we mask the automation fingerprint via JS and other flags
|
|
382
|
+
'--disable-extensions', # allow browser extensions
|
|
383
|
+
'--hide-scrollbars', # always show scrollbars in screenshots so agent knows there is more content below it can scroll down to
|
|
384
|
+
'--disable-features=AcceptCHFrame,AutoExpandDetailsElement,AvoidUnnecessaryBeforeUnloadCheckSync,CertificateTransparencyComponentUpdater,DeferRendererTasksAfterInput,DestroyProfileOnBrowserClose,DialMediaRouteProvider,ExtensionManifestV2Disabled,GlobalMediaControls,HttpsUpgrades,ImprovedCookieControls,LazyFrameLoading,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate',
|
|
385
|
+
],
|
|
386
|
+
description='List of default CLI args to stop playwright from applying (see https://github.com/microsoft/playwright/blob/41008eeddd020e2dee1c540f7c0cdfa337e99637/packages/playwright-core/src/server/chromium/chromiumSwitches.ts)',
|
|
387
|
+
)
|
|
388
|
+
channel: BrowserChannel | None = None # https://playwright.dev/docs/browsers#chromium-headless-shell
|
|
389
|
+
chromium_sandbox: bool = Field(
|
|
390
|
+
default=not CONFIG.IN_DOCKER, description='Whether to enable Chromium sandboxing (recommended unless inside Docker).'
|
|
391
|
+
)
|
|
392
|
+
devtools: bool = Field(
|
|
393
|
+
default=False, description='Whether to open DevTools panel automatically for every page, only works when headless=False.'
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
# proxy: ProxySettings | None = Field(default=None, description='Proxy settings to use to connect to the browser.')
|
|
397
|
+
downloads_path: str | Path | None = Field(
|
|
398
|
+
default=None,
|
|
399
|
+
description='Directory to save downloads to.',
|
|
400
|
+
validation_alias=AliasChoices('downloads_dir', 'save_downloads_path'),
|
|
401
|
+
)
|
|
402
|
+
traces_dir: str | Path | None = Field(
|
|
403
|
+
default=None,
|
|
404
|
+
description='Directory for saving playwright trace.zip files (playwright actions, screenshots, DOM snapshots, HAR traces).',
|
|
405
|
+
validation_alias=AliasChoices('trace_path', 'traces_dir'),
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
# firefox_user_prefs: dict[str, str | float | bool] = Field(default_factory=dict)
|
|
409
|
+
|
|
410
|
+
@model_validator(mode='after')
|
|
411
|
+
def validate_devtools_headless(self) -> Self:
|
|
412
|
+
"""Cannot open devtools when headless is True"""
|
|
413
|
+
assert not (self.headless and self.devtools), 'headless=True and devtools=True cannot both be set at the same time'
|
|
414
|
+
return self
|
|
415
|
+
|
|
416
|
+
@model_validator(mode='after')
|
|
417
|
+
def set_default_downloads_path(self) -> Self:
|
|
418
|
+
"""Set a unique default downloads path if none is provided."""
|
|
419
|
+
if self.downloads_path is None:
|
|
420
|
+
import uuid
|
|
421
|
+
|
|
422
|
+
# Create unique directory in /tmp for downloads
|
|
423
|
+
unique_id = str(uuid.uuid4())[:8] # 8 characters
|
|
424
|
+
downloads_path = Path(f'/tmp/browser-use-downloads-{unique_id}')
|
|
425
|
+
|
|
426
|
+
# Ensure path doesn't already exist (extremely unlikely but possible)
|
|
427
|
+
while downloads_path.exists():
|
|
428
|
+
unique_id = str(uuid.uuid4())[:8]
|
|
429
|
+
downloads_path = Path(f'/tmp/browser-use-downloads-{unique_id}')
|
|
430
|
+
|
|
431
|
+
self.downloads_path = downloads_path
|
|
432
|
+
self.downloads_path.mkdir(parents=True, exist_ok=True)
|
|
433
|
+
return self
|
|
434
|
+
|
|
435
|
+
@staticmethod
|
|
436
|
+
def args_as_dict(args: list[str]) -> dict[str, str]:
|
|
437
|
+
"""Return the extra launch CLI args as a dictionary."""
|
|
438
|
+
args_dict = {}
|
|
439
|
+
for arg in args:
|
|
440
|
+
key, value, *_ = [*arg.split('=', 1), '', '', '']
|
|
441
|
+
args_dict[key.strip().lstrip('-')] = value.strip()
|
|
442
|
+
return args_dict
|
|
443
|
+
|
|
444
|
+
@staticmethod
|
|
445
|
+
def args_as_list(args: dict[str, str]) -> list[str]:
|
|
446
|
+
"""Return the extra launch CLI args as a list of strings."""
|
|
447
|
+
return [f'--{key.lstrip("-")}={value}' if value else f'--{key.lstrip("-")}' for key, value in args.items()]
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
# ===== API-specific Models =====
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
class BrowserNewContextArgs(BrowserContextArgs):
|
|
454
|
+
"""
|
|
455
|
+
Pydantic model for new_context() arguments.
|
|
456
|
+
Extends BaseContextParams with storage_state parameter.
|
|
457
|
+
|
|
458
|
+
https://playwright.dev/python/docs/api/class-browser#browser-new-context
|
|
459
|
+
"""
|
|
460
|
+
|
|
461
|
+
model_config = ConfigDict(extra='ignore', validate_assignment=False, revalidate_instances='always', populate_by_name=True)
|
|
462
|
+
|
|
463
|
+
# storage_state is not supported in launch_persistent_context()
|
|
464
|
+
storage_state: str | Path | dict[str, Any] | None = None
|
|
465
|
+
# TODO: use StorageState type instead of dict[str, Any]
|
|
466
|
+
|
|
467
|
+
# to apply this to existing contexts (incl cookies, localStorage, IndexedDB), see:
|
|
468
|
+
# - https://github.com/microsoft/playwright/pull/34591/files
|
|
469
|
+
# - playwright-core/src/server/storageScript.ts restore() function
|
|
470
|
+
# - https://github.com/Skn0tt/playwright/blob/c446bc44bac4fbfdf52439ba434f92192459be4e/packages/playwright-core/src/server/storageScript.ts#L84C1-L123C2
|
|
471
|
+
|
|
472
|
+
# @field_validator('storage_state', mode='after')
|
|
473
|
+
# def load_storage_state_from_file(self) -> Self:
|
|
474
|
+
# """Load storage_state from file if it's a path."""
|
|
475
|
+
# if isinstance(self.storage_state, (str, Path)):
|
|
476
|
+
# storage_state_file = Path(self.storage_state)
|
|
477
|
+
# try:
|
|
478
|
+
# parsed_storage_state = json.loads(storage_state_file.read_text())
|
|
479
|
+
# validated_storage_state = StorageState(**parsed_storage_state)
|
|
480
|
+
# self.storage_state = validated_storage_state
|
|
481
|
+
# except Exception as e:
|
|
482
|
+
# raise ValueError(f'Failed to load storage state file {self.storage_state}: {e}') from e
|
|
483
|
+
# return self
|
|
484
|
+
pass
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
class BrowserLaunchPersistentContextArgs(BrowserLaunchArgs, BrowserContextArgs):
|
|
488
|
+
"""
|
|
489
|
+
Pydantic model for launch_persistent_context() arguments.
|
|
490
|
+
Combines browser launch parameters and context parameters,
|
|
491
|
+
plus adds the user_data_dir parameter.
|
|
492
|
+
|
|
493
|
+
https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context
|
|
494
|
+
"""
|
|
495
|
+
|
|
496
|
+
model_config = ConfigDict(extra='ignore', validate_assignment=False, revalidate_instances='always')
|
|
497
|
+
|
|
498
|
+
# Required parameter specific to launch_persistent_context, but can be None to use incognito temp dir
|
|
499
|
+
user_data_dir: str | Path | None = None
|
|
500
|
+
|
|
501
|
+
@field_validator('user_data_dir', mode='after')
|
|
502
|
+
@classmethod
|
|
503
|
+
def validate_user_data_dir(cls, v: str | Path | None) -> str | Path:
|
|
504
|
+
"""Validate user data dir is set to a non-default path."""
|
|
505
|
+
if v is None:
|
|
506
|
+
return tempfile.mkdtemp(prefix='browser-use-user-data-dir-')
|
|
507
|
+
return Path(v).expanduser().resolve()
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
class ProxySettings(BaseModel):
|
|
511
|
+
"""Typed proxy settings for Chromium traffic.
|
|
512
|
+
|
|
513
|
+
- server: Full proxy URL, e.g. "http://host:8080" or "socks5://host:1080"
|
|
514
|
+
- bypass: Comma-separated hosts to bypass (e.g. "localhost,127.0.0.1,*.internal")
|
|
515
|
+
- username/password: Optional credentials for authenticated proxies
|
|
516
|
+
"""
|
|
517
|
+
|
|
518
|
+
server: str | None = Field(default=None, description='Proxy URL, e.g. http://host:8080 or socks5://host:1080')
|
|
519
|
+
bypass: str | None = Field(default=None, description='Comma-separated hosts to bypass, e.g. localhost,127.0.0.1,*.internal')
|
|
520
|
+
username: str | None = Field(default=None, description='Proxy auth username')
|
|
521
|
+
password: str | None = Field(default=None, description='Proxy auth password')
|
|
522
|
+
|
|
523
|
+
def __getitem__(self, key: str) -> str | None:
|
|
524
|
+
return getattr(self, key)
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, BrowserLaunchArgs, BrowserNewContextArgs):
|
|
528
|
+
"""
|
|
529
|
+
A BrowserProfile is a static template collection of kwargs that can be passed to:
|
|
530
|
+
- BrowserType.launch(**BrowserLaunchArgs)
|
|
531
|
+
- BrowserType.connect(**BrowserConnectArgs)
|
|
532
|
+
- BrowserType.connect_over_cdp(**BrowserConnectArgs)
|
|
533
|
+
- BrowserType.launch_persistent_context(**BrowserLaunchPersistentContextArgs)
|
|
534
|
+
- BrowserContext.new_context(**BrowserNewContextArgs)
|
|
535
|
+
- BrowserSession(**BrowserProfile)
|
|
536
|
+
"""
|
|
537
|
+
|
|
538
|
+
model_config = ConfigDict(
|
|
539
|
+
extra='ignore',
|
|
540
|
+
validate_assignment=True,
|
|
541
|
+
revalidate_instances='always',
|
|
542
|
+
from_attributes=True,
|
|
543
|
+
validate_by_name=True,
|
|
544
|
+
validate_by_alias=True,
|
|
545
|
+
)
|
|
546
|
+
|
|
547
|
+
# ... extends options defined in:
|
|
548
|
+
# BrowserLaunchPersistentContextArgs, BrowserLaunchArgs, BrowserNewContextArgs, BrowserConnectArgs
|
|
549
|
+
|
|
550
|
+
# Session/connection configuration
|
|
551
|
+
cdp_url: str | None = Field(default=None, description='CDP URL for connecting to existing browser instance')
|
|
552
|
+
is_local: bool = Field(default=False, description='Whether this is a local browser instance')
|
|
553
|
+
use_cloud: bool = Field(
|
|
554
|
+
default=False,
|
|
555
|
+
description='Use browser-use cloud browser service instead of local browser',
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
@property
|
|
559
|
+
def cloud_browser(self) -> bool:
|
|
560
|
+
"""Alias for use_cloud field for compatibility."""
|
|
561
|
+
return self.use_cloud
|
|
562
|
+
|
|
563
|
+
cloud_browser_params: CloudBrowserParams | None = Field(
|
|
564
|
+
default=None, description='Parameters for creating a cloud browser instance'
|
|
565
|
+
)
|
|
566
|
+
|
|
567
|
+
# custom options we provide that aren't native playwright kwargs
|
|
568
|
+
disable_security: bool = Field(default=False, description='Disable browser security features.')
|
|
569
|
+
deterministic_rendering: bool = Field(default=False, description='Enable deterministic rendering flags.')
|
|
570
|
+
allowed_domains: list[str] | set[str] | None = Field(
|
|
571
|
+
default=None,
|
|
572
|
+
description='List of allowed domains for navigation e.g. ["*.google.com", "https://example.com", "chrome-extension://*"]. Lists with 100+ items are auto-optimized to sets (no pattern matching).',
|
|
573
|
+
)
|
|
574
|
+
prohibited_domains: list[str] | set[str] | None = Field(
|
|
575
|
+
default=None,
|
|
576
|
+
description='List of prohibited domains for navigation e.g. ["*.google.com", "https://example.com", "chrome-extension://*"]. Allowed domains take precedence over prohibited domains. Lists with 100+ items are auto-optimized to sets (no pattern matching).',
|
|
577
|
+
)
|
|
578
|
+
block_ip_addresses: bool = Field(
|
|
579
|
+
default=False,
|
|
580
|
+
description='Block navigation to URLs containing IP addresses (both IPv4 and IPv6). When True, blocks all IP-based URLs including localhost and private networks.',
|
|
581
|
+
)
|
|
582
|
+
keep_alive: bool | None = Field(default=None, description='Keep browser alive after agent run.')
|
|
583
|
+
|
|
584
|
+
# --- Proxy settings ---
|
|
585
|
+
# New consolidated proxy config (typed)
|
|
586
|
+
proxy: ProxySettings | None = Field(
|
|
587
|
+
default=None,
|
|
588
|
+
description='Proxy settings. Use browser_use.browser.profile.ProxySettings(server, bypass, username, password)',
|
|
589
|
+
)
|
|
590
|
+
enable_default_extensions: bool = Field(
|
|
591
|
+
default=True,
|
|
592
|
+
description="Enable automation-optimized extensions: ad blocking (uBlock Origin), cookie handling (I still don't care about cookies), and URL cleaning (ClearURLs). All extensions work automatically without manual intervention. Extensions are automatically downloaded and loaded when enabled.",
|
|
593
|
+
)
|
|
594
|
+
cookie_whitelist_domains: list[str] = Field(
|
|
595
|
+
default_factory=lambda: ['nature.com', 'qatarairways.com'],
|
|
596
|
+
description='List of domains to whitelist in the "I still don\'t care about cookies" extension, preventing automatic cookie banner handling on these sites.',
|
|
597
|
+
)
|
|
598
|
+
|
|
599
|
+
window_size: ViewportSize | None = Field(
|
|
600
|
+
default=None,
|
|
601
|
+
description='Browser window size to use when headless=False.',
|
|
602
|
+
)
|
|
603
|
+
window_height: int | None = Field(default=None, description='DEPRECATED, use window_size["height"] instead', exclude=True)
|
|
604
|
+
window_width: int | None = Field(default=None, description='DEPRECATED, use window_size["width"] instead', exclude=True)
|
|
605
|
+
window_position: ViewportSize | None = Field(
|
|
606
|
+
default=ViewportSize(width=0, height=0),
|
|
607
|
+
description='Window position to use for the browser x,y from the top left when headless=False.',
|
|
608
|
+
)
|
|
609
|
+
cross_origin_iframes: bool = Field(
|
|
610
|
+
default=True,
|
|
611
|
+
description='Enable cross-origin iframe support (OOPIF/Out-of-Process iframes). When False, only same-origin frames are processed to avoid complexity and hanging.',
|
|
612
|
+
)
|
|
613
|
+
max_iframes: int = Field(
|
|
614
|
+
default=100,
|
|
615
|
+
description='Maximum number of iframe documents to process to prevent crashes.',
|
|
616
|
+
)
|
|
617
|
+
max_iframe_depth: int = Field(
|
|
618
|
+
ge=0,
|
|
619
|
+
default=5,
|
|
620
|
+
description='Maximum depth for cross-origin iframe recursion (default: 5 levels deep).',
|
|
621
|
+
)
|
|
622
|
+
|
|
623
|
+
# --- Page load/wait timings ---
|
|
624
|
+
|
|
625
|
+
minimum_wait_page_load_time: float = Field(default=0.25, description='Minimum time to wait before capturing page state.')
|
|
626
|
+
wait_for_network_idle_page_load_time: float = Field(default=0.5, description='Time to wait for network idle.')
|
|
627
|
+
|
|
628
|
+
wait_between_actions: float = Field(default=0.1, description='Time to wait between actions.')
|
|
629
|
+
|
|
630
|
+
# --- UI/viewport/DOM ---
|
|
631
|
+
highlight_elements: bool = Field(default=True, description='Highlight interactive elements on the page.')
|
|
632
|
+
dom_highlight_elements: bool = Field(
|
|
633
|
+
default=False, description='Highlight interactive elements in the DOM (only for debugging purposes).'
|
|
634
|
+
)
|
|
635
|
+
filter_highlight_ids: bool = Field(
|
|
636
|
+
default=True, description='Only show element IDs in highlights if llm_representation is less than 10 characters.'
|
|
637
|
+
)
|
|
638
|
+
paint_order_filtering: bool = Field(default=True, description='Enable paint order filtering. Slightly experimental.')
|
|
639
|
+
interaction_highlight_color: str = Field(
|
|
640
|
+
default='rgb(255, 127, 39)',
|
|
641
|
+
description='Color to use for highlighting elements during interactions (CSS color string).',
|
|
642
|
+
)
|
|
643
|
+
interaction_highlight_duration: float = Field(default=1.0, description='Duration in seconds to show interaction highlights.')
|
|
644
|
+
|
|
645
|
+
# --- Downloads ---
|
|
646
|
+
auto_download_pdfs: bool = Field(default=True, description='Automatically download PDFs when navigating to PDF viewer pages.')
|
|
647
|
+
|
|
648
|
+
profile_directory: str = 'Default' # e.g. 'Profile 1', 'Profile 2', 'Custom Profile', etc.
|
|
649
|
+
|
|
650
|
+
# these can be found in BrowserLaunchArgs, BrowserLaunchPersistentContextArgs, BrowserNewContextArgs, BrowserConnectArgs:
|
|
651
|
+
# save_recording_path: alias of record_video_dir
|
|
652
|
+
# save_har_path: alias of record_har_path
|
|
653
|
+
# trace_path: alias of traces_dir
|
|
654
|
+
|
|
655
|
+
# these shadow the old playwright args on BrowserContextArgs, but it's ok
|
|
656
|
+
# because we handle them ourselves in a watchdog and we no longer use playwright, so they should live in the scope for our own config in BrowserProfile long-term
|
|
657
|
+
record_video_dir: Path | None = Field(
|
|
658
|
+
default=None,
|
|
659
|
+
description='Directory to save video recordings. If set, a video of the session will be recorded.',
|
|
660
|
+
validation_alias=AliasChoices('save_recording_path', 'record_video_dir'),
|
|
661
|
+
)
|
|
662
|
+
record_video_size: ViewportSize | None = Field(
|
|
663
|
+
default=None, description='Video frame size. If not set, it will use the viewport size.'
|
|
664
|
+
)
|
|
665
|
+
record_video_framerate: int = Field(default=30, description='The framerate to use for the video recording.')
|
|
666
|
+
|
|
667
|
+
# TODO: finish implementing extension support in extensions.py
|
|
668
|
+
# extension_ids_to_preinstall: list[str] = Field(
|
|
669
|
+
# default_factory=list, description='List of Chrome extension IDs to preinstall.'
|
|
670
|
+
# )
|
|
671
|
+
# extensions_dir: Path = Field(
|
|
672
|
+
# default_factory=lambda: Path('~/.config/browseruse/cache/extensions').expanduser(),
|
|
673
|
+
# description='Directory containing .crx extension files.',
|
|
674
|
+
# )
|
|
675
|
+
|
|
676
|
+
def __repr__(self) -> str:
|
|
677
|
+
short_dir = _log_pretty_path(self.user_data_dir) if self.user_data_dir else '<incognito>'
|
|
678
|
+
return f'BrowserProfile(user_data_dir= {short_dir}, headless={self.headless})'
|
|
679
|
+
|
|
680
|
+
def __str__(self) -> str:
|
|
681
|
+
return 'BrowserProfile'
|
|
682
|
+
|
|
683
|
+
@field_validator('allowed_domains', 'prohibited_domains', mode='after')
|
|
684
|
+
@classmethod
|
|
685
|
+
def optimize_large_domain_lists(cls, v: list[str] | set[str] | None) -> list[str] | set[str] | None:
|
|
686
|
+
"""Convert large domain lists (>=100 items) to sets for O(1) lookup performance."""
|
|
687
|
+
if v is None or isinstance(v, set):
|
|
688
|
+
return v
|
|
689
|
+
|
|
690
|
+
if len(v) >= DOMAIN_OPTIMIZATION_THRESHOLD:
|
|
691
|
+
logger.warning(
|
|
692
|
+
f'🔧 Optimizing domain list with {len(v)} items to set for O(1) lookup. '
|
|
693
|
+
f'Note: Pattern matching (*.domain.com, etc.) is not supported for lists >= {DOMAIN_OPTIMIZATION_THRESHOLD} items. '
|
|
694
|
+
f'Use exact domains only or keep list size < {DOMAIN_OPTIMIZATION_THRESHOLD} for pattern support.'
|
|
695
|
+
)
|
|
696
|
+
return set(v)
|
|
697
|
+
|
|
698
|
+
return v
|
|
699
|
+
|
|
700
|
+
@model_validator(mode='after')
|
|
701
|
+
def copy_old_config_names_to_new(self) -> Self:
|
|
702
|
+
"""Copy old config window_width & window_height to window_size."""
|
|
703
|
+
if self.window_width or self.window_height:
|
|
704
|
+
logger.warning(
|
|
705
|
+
f'⚠️ BrowserProfile(window_width=..., window_height=...) are deprecated, use BrowserProfile(window_size={"width": 1920, "height": 1080}) instead.'
|
|
706
|
+
)
|
|
707
|
+
window_size = self.window_size or ViewportSize(width=0, height=0)
|
|
708
|
+
window_size['width'] = window_size['width'] or self.window_width or 1920
|
|
709
|
+
window_size['height'] = window_size['height'] or self.window_height or 1080
|
|
710
|
+
self.window_size = window_size
|
|
711
|
+
|
|
712
|
+
return self
|
|
713
|
+
|
|
714
|
+
@model_validator(mode='after')
|
|
715
|
+
def warn_storage_state_user_data_dir_conflict(self) -> Self:
|
|
716
|
+
"""Warn when both storage_state and user_data_dir are set, as this can cause conflicts."""
|
|
717
|
+
has_storage_state = self.storage_state is not None
|
|
718
|
+
has_user_data_dir = (self.user_data_dir is not None) and ('tmp' not in str(self.user_data_dir).lower())
|
|
719
|
+
|
|
720
|
+
if has_storage_state and has_user_data_dir:
|
|
721
|
+
logger.warning(
|
|
722
|
+
f'⚠️ BrowserSession(...) was passed both storage_state AND user_data_dir. storage_state={self.storage_state} will forcibly overwrite '
|
|
723
|
+
f'cookies/localStorage/sessionStorage in user_data_dir={self.user_data_dir}. '
|
|
724
|
+
f'For multiple browsers in parallel, use only storage_state with user_data_dir=None, '
|
|
725
|
+
f'or use a separate user_data_dir for each browser and set storage_state=None.'
|
|
726
|
+
)
|
|
727
|
+
return self
|
|
728
|
+
|
|
729
|
+
@model_validator(mode='after')
|
|
730
|
+
def warn_user_data_dir_non_default_version(self) -> Self:
|
|
731
|
+
"""
|
|
732
|
+
If user is using default profile dir with a non-default channel, force-change it
|
|
733
|
+
to avoid corrupting the default data dir created with a different channel.
|
|
734
|
+
"""
|
|
735
|
+
|
|
736
|
+
is_not_using_default_chromium = self.executable_path or self.channel not in (BROWSERUSE_DEFAULT_CHANNEL, None)
|
|
737
|
+
if self.user_data_dir == CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR and is_not_using_default_chromium:
|
|
738
|
+
alternate_name = (
|
|
739
|
+
Path(self.executable_path).name.lower().replace(' ', '-')
|
|
740
|
+
if self.executable_path
|
|
741
|
+
else self.channel.name.lower()
|
|
742
|
+
if self.channel
|
|
743
|
+
else 'None'
|
|
744
|
+
)
|
|
745
|
+
logger.warning(
|
|
746
|
+
f'⚠️ {self} Changing user_data_dir= {_log_pretty_path(self.user_data_dir)} ➡️ .../default-{alternate_name} to avoid {alternate_name.upper()} corruping default profile created by {BROWSERUSE_DEFAULT_CHANNEL.name}'
|
|
747
|
+
)
|
|
748
|
+
self.user_data_dir = CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR.parent / f'default-{alternate_name}'
|
|
749
|
+
return self
|
|
750
|
+
|
|
751
|
+
@model_validator(mode='after')
|
|
752
|
+
def warn_deterministic_rendering_weirdness(self) -> Self:
|
|
753
|
+
if self.deterministic_rendering:
|
|
754
|
+
logger.warning(
|
|
755
|
+
'⚠️ BrowserSession(deterministic_rendering=True) is NOT RECOMMENDED. It breaks many sites and increases chances of getting blocked by anti-bot systems. '
|
|
756
|
+
'It hardcodes the JS random seed and forces browsers across Linux/Mac/Windows to use the same font rendering engine so that identical screenshots can be generated.'
|
|
757
|
+
)
|
|
758
|
+
return self
|
|
759
|
+
|
|
760
|
+
@model_validator(mode='after')
|
|
761
|
+
def validate_proxy_settings(self) -> Self:
|
|
762
|
+
"""Ensure proxy configuration is consistent."""
|
|
763
|
+
if self.proxy and (self.proxy.bypass and not self.proxy.server):
|
|
764
|
+
logger.warning('BrowserProfile.proxy.bypass provided but proxy has no server; bypass will be ignored.')
|
|
765
|
+
return self
|
|
766
|
+
|
|
767
|
+
@model_validator(mode='after')
|
|
768
|
+
def validate_highlight_elements_conflict(self) -> Self:
|
|
769
|
+
"""Ensure highlight_elements and dom_highlight_elements are not both enabled, with dom_highlight_elements taking priority."""
|
|
770
|
+
if self.highlight_elements and self.dom_highlight_elements:
|
|
771
|
+
logger.warning(
|
|
772
|
+
'⚠️ Both highlight_elements and dom_highlight_elements are enabled. '
|
|
773
|
+
'dom_highlight_elements takes priority. Setting highlight_elements=False.'
|
|
774
|
+
)
|
|
775
|
+
self.highlight_elements = False
|
|
776
|
+
return self
|
|
777
|
+
|
|
778
|
+
def model_post_init(self, __context: Any) -> None:
|
|
779
|
+
"""Called after model initialization to set up display configuration."""
|
|
780
|
+
self.detect_display_configuration()
|
|
781
|
+
|
|
782
|
+
def get_args(self) -> list[str]:
|
|
783
|
+
"""Get the list of all Chrome CLI launch args for this profile (compiled from defaults, user-provided, and system-specific)."""
|
|
784
|
+
|
|
785
|
+
if isinstance(self.ignore_default_args, list):
|
|
786
|
+
default_args = set(CHROME_DEFAULT_ARGS) - set(self.ignore_default_args)
|
|
787
|
+
elif self.ignore_default_args is True:
|
|
788
|
+
default_args = []
|
|
789
|
+
elif not self.ignore_default_args:
|
|
790
|
+
default_args = CHROME_DEFAULT_ARGS
|
|
791
|
+
|
|
792
|
+
assert self.user_data_dir is not None, 'user_data_dir must be set to a non-default path'
|
|
793
|
+
|
|
794
|
+
# Capture args before conversion for logging
|
|
795
|
+
pre_conversion_args = [
|
|
796
|
+
*default_args,
|
|
797
|
+
*self.args,
|
|
798
|
+
f'--user-data-dir={self.user_data_dir}',
|
|
799
|
+
f'--profile-directory={self.profile_directory}',
|
|
800
|
+
*(CHROME_DOCKER_ARGS if (CONFIG.IN_DOCKER or not self.chromium_sandbox) else []),
|
|
801
|
+
*(CHROME_HEADLESS_ARGS if self.headless else []),
|
|
802
|
+
*(CHROME_DISABLE_SECURITY_ARGS if self.disable_security else []),
|
|
803
|
+
*(CHROME_DETERMINISTIC_RENDERING_ARGS if self.deterministic_rendering else []),
|
|
804
|
+
*(
|
|
805
|
+
[f'--window-size={self.window_size["width"]},{self.window_size["height"]}']
|
|
806
|
+
if self.window_size
|
|
807
|
+
else (['--start-maximized'] if not self.headless else [])
|
|
808
|
+
),
|
|
809
|
+
*(
|
|
810
|
+
[f'--window-position={self.window_position["width"]},{self.window_position["height"]}']
|
|
811
|
+
if self.window_position
|
|
812
|
+
else []
|
|
813
|
+
),
|
|
814
|
+
*(self._get_extension_args() if self.enable_default_extensions else []),
|
|
815
|
+
]
|
|
816
|
+
|
|
817
|
+
# Proxy flags
|
|
818
|
+
proxy_server = self.proxy.server if self.proxy else None
|
|
819
|
+
proxy_bypass = self.proxy.bypass if self.proxy else None
|
|
820
|
+
|
|
821
|
+
if proxy_server:
|
|
822
|
+
pre_conversion_args.append(f'--proxy-server={proxy_server}')
|
|
823
|
+
if proxy_bypass:
|
|
824
|
+
pre_conversion_args.append(f'--proxy-bypass-list={proxy_bypass}')
|
|
825
|
+
|
|
826
|
+
# User agent flag
|
|
827
|
+
if self.user_agent:
|
|
828
|
+
pre_conversion_args.append(f'--user-agent={self.user_agent}')
|
|
829
|
+
|
|
830
|
+
# Special handling for --disable-features to merge values instead of overwriting
|
|
831
|
+
# This prevents disable_security=True from breaking extensions by ensuring
|
|
832
|
+
# both default features (including extension-related) and security features are preserved
|
|
833
|
+
disable_features_values = []
|
|
834
|
+
non_disable_features_args = []
|
|
835
|
+
|
|
836
|
+
# Extract and merge all --disable-features values
|
|
837
|
+
for arg in pre_conversion_args:
|
|
838
|
+
if arg.startswith('--disable-features='):
|
|
839
|
+
features = arg.split('=', 1)[1]
|
|
840
|
+
disable_features_values.extend(features.split(','))
|
|
841
|
+
else:
|
|
842
|
+
non_disable_features_args.append(arg)
|
|
843
|
+
|
|
844
|
+
# Remove duplicates while preserving order
|
|
845
|
+
if disable_features_values:
|
|
846
|
+
unique_features = []
|
|
847
|
+
seen = set()
|
|
848
|
+
for feature in disable_features_values:
|
|
849
|
+
feature = feature.strip()
|
|
850
|
+
if feature and feature not in seen:
|
|
851
|
+
unique_features.append(feature)
|
|
852
|
+
seen.add(feature)
|
|
853
|
+
|
|
854
|
+
# Add merged disable-features back
|
|
855
|
+
non_disable_features_args.append(f'--disable-features={",".join(unique_features)}')
|
|
856
|
+
|
|
857
|
+
# convert to dict and back to dedupe and merge other duplicate args
|
|
858
|
+
final_args_list = BrowserLaunchArgs.args_as_list(BrowserLaunchArgs.args_as_dict(non_disable_features_args))
|
|
859
|
+
|
|
860
|
+
return final_args_list
|
|
861
|
+
|
|
862
|
+
def _get_extension_args(self) -> list[str]:
|
|
863
|
+
"""Get Chrome args for enabling default extensions (ad blocker and cookie handler)."""
|
|
864
|
+
extension_paths = self._ensure_default_extensions_downloaded()
|
|
865
|
+
|
|
866
|
+
args = [
|
|
867
|
+
'--enable-extensions',
|
|
868
|
+
'--disable-extensions-file-access-check',
|
|
869
|
+
'--disable-extensions-http-throttling',
|
|
870
|
+
'--enable-extension-activity-logging',
|
|
871
|
+
]
|
|
872
|
+
|
|
873
|
+
if extension_paths:
|
|
874
|
+
args.append(f'--load-extension={",".join(extension_paths)}')
|
|
875
|
+
|
|
876
|
+
return args
|
|
877
|
+
|
|
878
|
+
def _ensure_default_extensions_downloaded(self) -> list[str]:
|
|
879
|
+
"""
|
|
880
|
+
Ensure default extensions are downloaded and cached locally.
|
|
881
|
+
Returns list of paths to extension directories.
|
|
882
|
+
"""
|
|
883
|
+
|
|
884
|
+
# Extension definitions - optimized for automation and content extraction
|
|
885
|
+
# Combines uBlock Origin (ad blocking) + "I still don't care about cookies" (cookie banner handling)
|
|
886
|
+
extensions = [
|
|
887
|
+
{
|
|
888
|
+
'name': 'uBlock Origin',
|
|
889
|
+
'id': 'cjpalhdlnbpafiamejdnhcphjbkeiagm',
|
|
890
|
+
'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dcjpalhdlnbpafiamejdnhcphjbkeiagm%26uc',
|
|
891
|
+
},
|
|
892
|
+
{
|
|
893
|
+
'name': "I still don't care about cookies",
|
|
894
|
+
'id': 'edibdbjcniadpccecjdfdjjppcpchdlm',
|
|
895
|
+
'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dedibdbjcniadpccecjdfdjjppcpchdlm%26uc',
|
|
896
|
+
},
|
|
897
|
+
{
|
|
898
|
+
'name': 'ClearURLs',
|
|
899
|
+
'id': 'lckanjgmijmafbedllaakclkaicjfmnk',
|
|
900
|
+
'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dlckanjgmijmafbedllaakclkaicjfmnk%26uc',
|
|
901
|
+
},
|
|
902
|
+
{
|
|
903
|
+
'name': 'Force Background Tab',
|
|
904
|
+
'id': 'gidlfommnbibbmegmgajdbikelkdcmcl',
|
|
905
|
+
'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dgidlfommnbibbmegmgajdbikelkdcmcl%26uc',
|
|
906
|
+
},
|
|
907
|
+
# {
|
|
908
|
+
# 'name': 'Captcha Solver: Auto captcha solving service',
|
|
909
|
+
# 'id': 'pgojnojmmhpofjgdmaebadhbocahppod',
|
|
910
|
+
# 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dpgojnojmmhpofjgdmaebadhbocahppod%26uc',
|
|
911
|
+
# },
|
|
912
|
+
# Consent-O-Matic disabled - using uBlock Origin's cookie lists instead for simplicity
|
|
913
|
+
# {
|
|
914
|
+
# 'name': 'Consent-O-Matic',
|
|
915
|
+
# 'id': 'mdjildafknihdffpkfmmpnpoiajfjnjd',
|
|
916
|
+
# 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dmdjildafknihdffpkfmmpnpoiajfjnjd%26uc',
|
|
917
|
+
# },
|
|
918
|
+
# {
|
|
919
|
+
# 'name': 'Privacy | Protect Your Payments',
|
|
920
|
+
# 'id': 'hmgpakheknboplhmlicfkkgjipfabmhp',
|
|
921
|
+
# 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dhmgpakheknboplhmlicfkkgjipfabmhp%26uc',
|
|
922
|
+
# },
|
|
923
|
+
]
|
|
924
|
+
|
|
925
|
+
# Create extensions cache directory
|
|
926
|
+
cache_dir = CONFIG.BROWSER_USE_EXTENSIONS_DIR
|
|
927
|
+
cache_dir.mkdir(parents=True, exist_ok=True)
|
|
928
|
+
# logger.debug(f'📁 Extensions cache directory: {_log_pretty_path(cache_dir)}')
|
|
929
|
+
|
|
930
|
+
extension_paths = []
|
|
931
|
+
loaded_extension_names = []
|
|
932
|
+
|
|
933
|
+
for ext in extensions:
|
|
934
|
+
ext_dir = cache_dir / ext['id']
|
|
935
|
+
crx_file = cache_dir / f'{ext["id"]}.crx'
|
|
936
|
+
|
|
937
|
+
# Check if extension is already extracted
|
|
938
|
+
if ext_dir.exists() and (ext_dir / 'manifest.json').exists():
|
|
939
|
+
# logger.debug(f'✅ Using cached {ext["name"]} extension from {_log_pretty_path(ext_dir)}')
|
|
940
|
+
extension_paths.append(str(ext_dir))
|
|
941
|
+
loaded_extension_names.append(ext['name'])
|
|
942
|
+
continue
|
|
943
|
+
|
|
944
|
+
try:
|
|
945
|
+
# Download extension if not cached
|
|
946
|
+
if not crx_file.exists():
|
|
947
|
+
logger.info(f'📦 Downloading {ext["name"]} extension...')
|
|
948
|
+
self._download_extension(ext['url'], crx_file)
|
|
949
|
+
else:
|
|
950
|
+
logger.debug(f'📦 Found cached {ext["name"]} .crx file')
|
|
951
|
+
|
|
952
|
+
# Extract extension
|
|
953
|
+
logger.info(f'📂 Extracting {ext["name"]} extension...')
|
|
954
|
+
self._extract_extension(crx_file, ext_dir)
|
|
955
|
+
|
|
956
|
+
extension_paths.append(str(ext_dir))
|
|
957
|
+
loaded_extension_names.append(ext['name'])
|
|
958
|
+
|
|
959
|
+
except Exception as e:
|
|
960
|
+
logger.warning(f'⚠️ Failed to setup {ext["name"]} extension: {e}')
|
|
961
|
+
continue
|
|
962
|
+
|
|
963
|
+
# Apply minimal patch to cookie extension with configurable whitelist
|
|
964
|
+
for i, path in enumerate(extension_paths):
|
|
965
|
+
if loaded_extension_names[i] == "I still don't care about cookies":
|
|
966
|
+
self._apply_minimal_extension_patch(Path(path), self.cookie_whitelist_domains)
|
|
967
|
+
|
|
968
|
+
if extension_paths:
|
|
969
|
+
logger.debug(f'[BrowserProfile] 🧩 Extensions loaded ({len(extension_paths)}): [{", ".join(loaded_extension_names)}]')
|
|
970
|
+
else:
|
|
971
|
+
logger.warning('[BrowserProfile] ⚠️ No default extensions could be loaded')
|
|
972
|
+
|
|
973
|
+
return extension_paths
|
|
974
|
+
|
|
975
|
+
def _apply_minimal_extension_patch(self, ext_dir: Path, whitelist_domains: list[str]) -> None:
|
|
976
|
+
"""Minimal patch: pre-populate chrome.storage.local with configurable domain whitelist."""
|
|
977
|
+
try:
|
|
978
|
+
bg_path = ext_dir / 'data' / 'background.js'
|
|
979
|
+
if not bg_path.exists():
|
|
980
|
+
return
|
|
981
|
+
|
|
982
|
+
with open(bg_path, encoding='utf-8') as f:
|
|
983
|
+
content = f.read()
|
|
984
|
+
|
|
985
|
+
# Create the whitelisted domains object for JavaScript with proper indentation
|
|
986
|
+
whitelist_entries = [f' "{domain}": true' for domain in whitelist_domains]
|
|
987
|
+
whitelist_js = '{\n' + ',\n'.join(whitelist_entries) + '\n }'
|
|
988
|
+
|
|
989
|
+
# Find the initialize() function and inject storage setup before updateSettings()
|
|
990
|
+
# The actual function uses 2-space indentation, not tabs
|
|
991
|
+
old_init = """async function initialize(checkInitialized, magic) {
|
|
992
|
+
if (checkInitialized && initialized) {
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
loadCachedRules();
|
|
996
|
+
await updateSettings();
|
|
997
|
+
await recreateTabList(magic);
|
|
998
|
+
initialized = true;
|
|
999
|
+
}"""
|
|
1000
|
+
|
|
1001
|
+
# New function with configurable whitelist initialization
|
|
1002
|
+
new_init = f"""// Pre-populate storage with configurable domain whitelist if empty
|
|
1003
|
+
async function ensureWhitelistStorage() {{
|
|
1004
|
+
const result = await chrome.storage.local.get({{ settings: null }});
|
|
1005
|
+
if (!result.settings) {{
|
|
1006
|
+
const defaultSettings = {{
|
|
1007
|
+
statusIndicators: true,
|
|
1008
|
+
whitelistedDomains: {whitelist_js}
|
|
1009
|
+
}};
|
|
1010
|
+
await chrome.storage.local.set({{ settings: defaultSettings }});
|
|
1011
|
+
}}
|
|
1012
|
+
}}
|
|
1013
|
+
|
|
1014
|
+
async function initialize(checkInitialized, magic) {{
|
|
1015
|
+
if (checkInitialized && initialized) {{
|
|
1016
|
+
return;
|
|
1017
|
+
}}
|
|
1018
|
+
loadCachedRules();
|
|
1019
|
+
await ensureWhitelistStorage(); // Add storage initialization
|
|
1020
|
+
await updateSettings();
|
|
1021
|
+
await recreateTabList(magic);
|
|
1022
|
+
initialized = true;
|
|
1023
|
+
}}"""
|
|
1024
|
+
|
|
1025
|
+
if old_init in content:
|
|
1026
|
+
content = content.replace(old_init, new_init)
|
|
1027
|
+
|
|
1028
|
+
with open(bg_path, 'w', encoding='utf-8') as f:
|
|
1029
|
+
f.write(content)
|
|
1030
|
+
|
|
1031
|
+
domain_list = ', '.join(whitelist_domains)
|
|
1032
|
+
logger.info(f'[BrowserProfile] ✅ Cookie extension: {domain_list} pre-populated in storage')
|
|
1033
|
+
else:
|
|
1034
|
+
logger.debug('[BrowserProfile] Initialize function not found for patching')
|
|
1035
|
+
|
|
1036
|
+
except Exception as e:
|
|
1037
|
+
logger.debug(f'[BrowserProfile] Could not patch extension storage: {e}')
|
|
1038
|
+
|
|
1039
|
+
def _download_extension(self, url: str, output_path: Path) -> None:
|
|
1040
|
+
"""Download extension .crx file."""
|
|
1041
|
+
import urllib.request
|
|
1042
|
+
|
|
1043
|
+
try:
|
|
1044
|
+
with urllib.request.urlopen(url) as response:
|
|
1045
|
+
with open(output_path, 'wb') as f:
|
|
1046
|
+
f.write(response.read())
|
|
1047
|
+
except Exception as e:
|
|
1048
|
+
raise Exception(f'Failed to download extension: {e}')
|
|
1049
|
+
|
|
1050
|
+
def _extract_extension(self, crx_path: Path, extract_dir: Path) -> None:
|
|
1051
|
+
"""Extract .crx file to directory."""
|
|
1052
|
+
import os
|
|
1053
|
+
import zipfile
|
|
1054
|
+
|
|
1055
|
+
# Remove existing directory
|
|
1056
|
+
if extract_dir.exists():
|
|
1057
|
+
import shutil
|
|
1058
|
+
|
|
1059
|
+
shutil.rmtree(extract_dir)
|
|
1060
|
+
|
|
1061
|
+
extract_dir.mkdir(parents=True, exist_ok=True)
|
|
1062
|
+
|
|
1063
|
+
try:
|
|
1064
|
+
# CRX files are ZIP files with a header, try to extract as ZIP
|
|
1065
|
+
with zipfile.ZipFile(crx_path, 'r') as zip_ref:
|
|
1066
|
+
zip_ref.extractall(extract_dir)
|
|
1067
|
+
|
|
1068
|
+
# Verify manifest exists
|
|
1069
|
+
if not (extract_dir / 'manifest.json').exists():
|
|
1070
|
+
raise Exception('No manifest.json found in extension')
|
|
1071
|
+
|
|
1072
|
+
except zipfile.BadZipFile:
|
|
1073
|
+
# CRX files have a header before the ZIP data
|
|
1074
|
+
# Skip the CRX header and extract the ZIP part
|
|
1075
|
+
with open(crx_path, 'rb') as f:
|
|
1076
|
+
# Read CRX header to find ZIP start
|
|
1077
|
+
magic = f.read(4)
|
|
1078
|
+
if magic != b'Cr24':
|
|
1079
|
+
raise Exception('Invalid CRX file format')
|
|
1080
|
+
|
|
1081
|
+
version = int.from_bytes(f.read(4), 'little')
|
|
1082
|
+
if version == 2:
|
|
1083
|
+
pubkey_len = int.from_bytes(f.read(4), 'little')
|
|
1084
|
+
sig_len = int.from_bytes(f.read(4), 'little')
|
|
1085
|
+
f.seek(16 + pubkey_len + sig_len) # Skip to ZIP data
|
|
1086
|
+
elif version == 3:
|
|
1087
|
+
header_len = int.from_bytes(f.read(4), 'little')
|
|
1088
|
+
f.seek(12 + header_len) # Skip to ZIP data
|
|
1089
|
+
|
|
1090
|
+
# Extract ZIP data
|
|
1091
|
+
zip_data = f.read()
|
|
1092
|
+
|
|
1093
|
+
# Write ZIP data to temp file and extract
|
|
1094
|
+
import tempfile
|
|
1095
|
+
|
|
1096
|
+
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as temp_zip:
|
|
1097
|
+
temp_zip.write(zip_data)
|
|
1098
|
+
temp_zip.flush()
|
|
1099
|
+
|
|
1100
|
+
with zipfile.ZipFile(temp_zip.name, 'r') as zip_ref:
|
|
1101
|
+
zip_ref.extractall(extract_dir)
|
|
1102
|
+
|
|
1103
|
+
os.unlink(temp_zip.name)
|
|
1104
|
+
|
|
1105
|
+
def detect_display_configuration(self) -> None:
|
|
1106
|
+
"""
|
|
1107
|
+
Detect the system display size and initialize the display-related config defaults:
|
|
1108
|
+
screen, window_size, window_position, viewport, no_viewport, device_scale_factor
|
|
1109
|
+
"""
|
|
1110
|
+
|
|
1111
|
+
display_size = get_display_size()
|
|
1112
|
+
has_screen_available = bool(display_size)
|
|
1113
|
+
self.screen = self.screen or display_size or ViewportSize(width=1920, height=1080)
|
|
1114
|
+
|
|
1115
|
+
# if no headless preference specified, prefer headful if there is a display available
|
|
1116
|
+
if self.headless is None:
|
|
1117
|
+
self.headless = not has_screen_available
|
|
1118
|
+
|
|
1119
|
+
# Determine viewport behavior based on mode and user preferences
|
|
1120
|
+
user_provided_viewport = self.viewport is not None
|
|
1121
|
+
|
|
1122
|
+
if self.headless:
|
|
1123
|
+
# Headless mode: always use viewport for content size control
|
|
1124
|
+
self.viewport = self.viewport or self.window_size or self.screen
|
|
1125
|
+
self.window_position = None
|
|
1126
|
+
self.window_size = None
|
|
1127
|
+
self.no_viewport = False
|
|
1128
|
+
else:
|
|
1129
|
+
# Headful mode: respect user's viewport preference
|
|
1130
|
+
self.window_size = self.window_size or self.screen
|
|
1131
|
+
|
|
1132
|
+
if user_provided_viewport:
|
|
1133
|
+
# User explicitly set viewport - enable viewport mode
|
|
1134
|
+
self.no_viewport = False
|
|
1135
|
+
else:
|
|
1136
|
+
# Default headful: content fits to window (no viewport)
|
|
1137
|
+
self.no_viewport = True if self.no_viewport is None else self.no_viewport
|
|
1138
|
+
|
|
1139
|
+
# Handle special requirements (device_scale_factor forces viewport mode)
|
|
1140
|
+
if self.device_scale_factor and self.no_viewport is None:
|
|
1141
|
+
self.no_viewport = False
|
|
1142
|
+
|
|
1143
|
+
# Finalize configuration
|
|
1144
|
+
if self.no_viewport:
|
|
1145
|
+
# No viewport mode: content adapts to window
|
|
1146
|
+
self.viewport = None
|
|
1147
|
+
self.device_scale_factor = None
|
|
1148
|
+
self.screen = None
|
|
1149
|
+
assert self.viewport is None
|
|
1150
|
+
assert self.no_viewport is True
|
|
1151
|
+
else:
|
|
1152
|
+
# Viewport mode: ensure viewport is set
|
|
1153
|
+
self.viewport = self.viewport or self.screen
|
|
1154
|
+
self.device_scale_factor = self.device_scale_factor or 1.0
|
|
1155
|
+
assert self.viewport is not None
|
|
1156
|
+
assert self.no_viewport is False
|
|
1157
|
+
|
|
1158
|
+
assert not (self.headless and self.no_viewport), 'headless=True and no_viewport=True cannot both be set at the same time'
|