Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/firefox/bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import type {
EmptyParams,
EmptyResult,
Network,
Script,
} from 'webdriver-bidi-protocol';
import { logDebug } from '../utils/logger.js';
import { remoteValueToNative } from '../utils/remote-value.js';

// Firefox-specific events
type DebuggingPausedEvent = {
Expand Down Expand Up @@ -154,6 +156,57 @@ export class BiDiFacade extends EventEmitter<FirefoxEventMap> {
});
}

async evaluate<T = unknown>(
context: BrowsingContext.BrowsingContext,
expression: string
): Promise<T> {
return remoteValueToNative(await this.evaluateRaw(expression, context)) as T;
}

async evaluateRaw(
context: BrowsingContext.BrowsingContext,
expression: string
): Promise<Script.RemoteValue> {
const result = await this.sendCommand('script.evaluate', {
expression,
awaitPromise: true,
target: { context },
});
if (result.type === 'success') {
return result.result;
}
throw new Error(
`Script evaluation failed: ${result.exceptionDetails?.text ?? 'unknown error'}`
);
}

async callFunction<T = unknown>(
context: BrowsingContext.BrowsingContext,
functionDeclaration: string,
args: Script.LocalValue[]
): Promise<T> {
return remoteValueToNative(await this.callFunctionRaw(context, functionDeclaration, args)) as T;
}

async callFunctionRaw(
context: BrowsingContext.BrowsingContext,
functionDeclaration: string,
args: Script.LocalValue[]
): Promise<Script.RemoteValue> {
const result = await this.sendCommand('script.callFunction', {
functionDeclaration,
arguments: args,
awaitPromise: true,
target: { context },
});
if (result.type === 'success') {
return result.result;
}
throw new Error(
`Script evaluation failed: ${result.exceptionDetails?.text ?? 'unknown error'}`
);
}

private listenForEvents(ws: any) {
ws.on('message', (data: any) => {
let payload: any;
Expand Down
Loading
Loading