Skip to content

Commit

Permalink
feat: preload scripts: support sandboxes
Browse files Browse the repository at this point in the history
Bug: #293
Docs: go/chrome-devtools:preload-script-world-name
  • Loading branch information
Thiago Perrotta authored and OrKoN committed Jul 7, 2023
1 parent f8ef01e commit 2c591c4
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 32 deletions.
9 changes: 4 additions & 5 deletions src/bidiMapper/domains/context/bidiPreloadScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class BidiPreloadScript {
readonly #targetIds = new Set<string>();
/** Channels to be added as arguments to functionDeclaration. */
readonly #channels: ChannelProxy[];
/** The script sandbox / world name. */
readonly #sandbox?: string;

get id(): string {
return this.#id;
Expand All @@ -60,14 +62,10 @@ export class BidiPreloadScript {
}

constructor(params: Script.AddPreloadScriptParameters) {
if (params.sandbox !== undefined) {
// TODO: Handle sandbox.
throw new Error('Sandbox is not supported yet');
}

this.#channels =
params.arguments?.map((a) => new ChannelProxy(a.value)) ?? [];
this.#functionDeclaration = params.functionDeclaration;
this.#sandbox = params.sandbox;
}

/** Channels of the preload script. */
Expand Down Expand Up @@ -114,6 +112,7 @@ export class BidiPreloadScript {
'Page.addScriptToEvaluateOnNewDocument',
{
source: this.#getEvaluateString(),
worldName: this.#sandbox,
runImmediately,
}
);
Expand Down
54 changes: 54 additions & 0 deletions tests/test_preload_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,60 @@ async def test_preloadScript_add_loadedInNewContexts(websocket, context_id,
assert result["result"] == {"type": "string", "value": 'bar'}


@pytest.mark.asyncio
async def test_preloadScript_add_sandbox(websocket, context_id, html):
result = await execute_command(
websocket, {
"method": "script.addPreloadScript",
"params": {
"functionDeclaration": "() => { window.foo='bar'; }",
"sandbox": "MY_SANDBOX",
}
})
assert result == {'script': ANY_STR}

await execute_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": html(),
"wait": "complete",
"context": context_id
}
})

# Evaluate in the standard sandbox, script takes no effect.
result = await execute_command(
websocket, {
"method": "script.evaluate",
"params": {
"expression": "window.foo",
"target": {
"context": context_id
},
"awaitPromise": True,
"resultOwnership": "root"
}
})
assert result["result"] == {"type": "undefined"}

# Evaluate in the given sandbox, script takes effect.
result = await execute_command(
websocket, {
"method": "script.evaluate",
"params": {
"expression": "window.foo",
"target": {
"context": context_id,
"sandbox": "MY_SANDBOX",
},
"awaitPromise": True,
"resultOwnership": "root"
}
})
assert result["result"] == {"type": "string", "value": 'bar'}


@pytest.mark.asyncio
@pytest.mark.parametrize("with_reload", [True, False],
ids=["with reload", "without reload"])
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 2c591c4

Please sign in to comment.