Add init_scripts support to BrowserConfig for pre-page-load JS injection
This adds the ability to inject JavaScript that runs before any page loads, useful for stealth evasions (canvas/audio fingerprinting, userAgentData). - Add init_scripts parameter to BrowserConfig (list of JS strings) - Apply init_scripts in setup_context() via context.add_init_script() - Update from_kwargs() and to_dict() for serialization
This commit is contained in:
@@ -477,6 +477,7 @@ class BrowserConfig:
|
||||
debugging_port: int = 9222,
|
||||
host: str = "localhost",
|
||||
enable_stealth: bool = False,
|
||||
init_scripts: List[str] = None,
|
||||
):
|
||||
|
||||
self.browser_type = browser_type
|
||||
@@ -536,6 +537,7 @@ class BrowserConfig:
|
||||
self.debugging_port = debugging_port
|
||||
self.host = host
|
||||
self.enable_stealth = enable_stealth
|
||||
self.init_scripts = init_scripts if init_scripts is not None else []
|
||||
|
||||
fa_user_agenr_generator = ValidUAGenerator()
|
||||
if self.user_agent_mode == "random":
|
||||
@@ -615,6 +617,7 @@ class BrowserConfig:
|
||||
debugging_port=kwargs.get("debugging_port", 9222),
|
||||
host=kwargs.get("host", "localhost"),
|
||||
enable_stealth=kwargs.get("enable_stealth", False),
|
||||
init_scripts=kwargs.get("init_scripts", []),
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
@@ -654,9 +657,10 @@ class BrowserConfig:
|
||||
"debugging_port": self.debugging_port,
|
||||
"host": self.host,
|
||||
"enable_stealth": self.enable_stealth,
|
||||
"init_scripts": self.init_scripts,
|
||||
}
|
||||
|
||||
|
||||
|
||||
return result
|
||||
|
||||
def clone(self, **kwargs):
|
||||
|
||||
@@ -890,7 +890,12 @@ class BrowserManager:
|
||||
or crawlerRunConfig.simulate_user
|
||||
or crawlerRunConfig.magic
|
||||
):
|
||||
await context.add_init_script(load_js_script("navigator_overrider"))
|
||||
await context.add_init_script(load_js_script("navigator_overrider"))
|
||||
|
||||
# Apply custom init_scripts from BrowserConfig (for stealth evasions, etc.)
|
||||
if self.config.init_scripts:
|
||||
for script in self.config.init_scripts:
|
||||
await context.add_init_script(script)
|
||||
|
||||
async def create_browser_context(self, crawlerRunConfig: CrawlerRunConfig = None):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user