Preload¶
The preload module injects JavaScript before page load. For full usage examples, see Preload Scripts.
Methods¶
add_preload_script¶
Inject a function that runs before the page's own scripts:
result = await client.preload.add_preload_script(
function_declaration="() => { window.myFlag = true; }",
contexts=["context-id-1"],
)
# result.script = "preload-script-id"
Preload scripts can also be scoped to specific user contexts:
result = await client.preload.add_preload_script(
function_declaration="() => { window.myFlag = true; }",
user_contexts=["user-context-id-1"],
)
remove_preload_script¶
Remove a previously added preload script:
Reference¶
PreloadModule ¶
Module for managing preload scripts.
Preload scripts are injected into pages before any other script runs, allowing you to modify the page environment early.
Commands
- add_preload_script — inject a script before page load
- remove_preload_script — remove a previously added script
Example
script_id = await client.preload.add_script( "() => { window.__injected = true; }", )
... navigate and verify ...¶
await client.preload.remove_script(script_id)
Source code in bidiwave/modules/preload.py
add_script
async
¶
add_script(function_declaration: str, arguments: list[dict[str, Any]] | None = None, contexts: list[str] | None = None, user_contexts: list[str] | None = None, sandbox: str | None = None) -> str
Adds a preload script.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function_declaration
|
str
|
JS function to execute before page load. |
required |
arguments
|
list[dict[str, Any]] | None
|
Arguments to pass to the function. |
None
|
contexts
|
list[str] | None
|
Context IDs where the script should run. None = all. |
None
|
user_contexts
|
list[str] | None
|
User context IDs where the script should run. None = all. |
None
|
sandbox
|
str | None
|
Sandbox name to run the script in. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Preload script ID for later removal. |
Source code in bidiwave/modules/preload.py
remove_script
async
¶
Removes a preload script.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_id
|
str
|
ID returned by add_script. |
required |