-
Notifications
You must be signed in to change notification settings - Fork 39
Bug 2018268 - fix: migrate script evaluation tools to WebDriver BiDi #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliandescottes
merged 4 commits into
mozilla:main
from
juliandescottes:bug2018268-part1
Jun 12, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
457b8af
Bug 2018268 - refactor: move js validation helpers to dedicated module
juliandescottes 0a06d42
Bug 2018268 - refactor: update script module to use WebDriver BiDi fo…
juliandescottes 9fda9db
Bug 2018268 - refactor: update privileged-context module to use WebDr…
juliandescottes 5e8d8f5
Bug 2018268 - refactor: remove now unused isLikelyFunctionBody and is…
juliandescottes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| const MAX_FUNCTION_SIZE = 16 * 1024; // 16 KB | ||
|
|
||
| /** | ||
| * Validates that a string is a function or arrow function declaration suitable | ||
| * for use with WebDriver BiDi script.callFunction. | ||
| * Throws with a descriptive error if validation fails. | ||
| */ | ||
| export function validateFunction(fnString: string): void { | ||
| if (!fnString || typeof fnString !== 'string') { | ||
| throw new Error('function parameter is required and must be a string'); | ||
| } | ||
|
|
||
| if (fnString.length > MAX_FUNCTION_SIZE) { | ||
| throw new Error( | ||
| `Function too large (${fnString.length} bytes, max ${MAX_FUNCTION_SIZE} bytes). ` + | ||
| 'This tool is not designed for massive scripts.' | ||
| ); | ||
| } | ||
|
|
||
| const trimmed = fnString.trim(); | ||
| const isFunctionLike = | ||
| trimmed.startsWith('function') || | ||
| trimmed.startsWith('async function') || | ||
| trimmed.startsWith('(') || | ||
| trimmed.startsWith('async ('); | ||
|
|
||
| if (!isFunctionLike) { | ||
| throw new Error( | ||
| `Invalid function format. Expected a function or arrow function, got: "${trimmed.substring(0, 50)}...".\n\n` + | ||
| 'Valid examples:\n' + | ||
| ' () => document.title\n' + | ||
| ' async () => { return await fetch("/api") }\n' + | ||
| ' (el) => el.innerText\n' + | ||
| ' function() { return window.location.href }' | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /** | ||
| * Converts a WebDriver BiDi RemoteValue to a native JavaScript value. | ||
| * Special number values (NaN, Infinity, -0) are returned as strings since | ||
| * they cannot be represented in JSON. | ||
| */ | ||
| export function remoteValueToNative(rv: unknown): unknown { | ||
| if (!rv || typeof rv !== 'object') { | ||
| return rv; | ||
| } | ||
|
|
||
| const { type, value } = rv as { type: string; value?: unknown }; | ||
|
|
||
| switch (type) { | ||
| case 'undefined': | ||
| return undefined; | ||
| case 'null': | ||
| return null; | ||
| case 'string': | ||
| case 'boolean': | ||
| return value; | ||
| case 'number': | ||
| if (value === 'NaN') { | ||
| return 'NaN'; | ||
| } | ||
| if (value === 'Infinity') { | ||
| return 'Infinity'; | ||
| } | ||
| if (value === '-Infinity') { | ||
| return '-Infinity'; | ||
| } | ||
| if (value === '-0') { | ||
| return '-0'; | ||
| } | ||
| return value; | ||
| case 'bigint': | ||
| return `${value as string}n`; | ||
| case 'array': | ||
| return (value as unknown[]).map(remoteValueToNative); | ||
| case 'object': | ||
| return Object.fromEntries( | ||
| (value as [string, unknown][]).map(([k, v]) => [k, remoteValueToNative(v)]) | ||
| ); | ||
| case 'map': | ||
| return Object.fromEntries( | ||
| (value as [unknown, unknown][]).map(([k, v]) => [ | ||
| typeof k === 'object' | ||
| ? JSON.stringify(remoteValueToNative(k)) | ||
| : String(k as string | number | boolean), | ||
| remoteValueToNative(v), | ||
| ]) | ||
| ); | ||
| case 'set': | ||
| return (value as unknown[]).map(remoteValueToNative); | ||
| case 'regexp': { | ||
| const { pattern, flags } = value as { pattern: string; flags?: string }; | ||
| return `/${pattern}/${flags ?? ''}`; | ||
| } | ||
| case 'date': | ||
| return value; | ||
| default: | ||
| return `[${type}]`; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is https://www.w3.org/TR/webdriver-bidi/#issue-6eade172
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, the topic hasn't moved much since we landed the initial script commands (to be clear, evaluate has the same issue, but it has a clearer TODO in the spec :) )