Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/execute-js/lib/CorePlugin.ts
2579 views
1
import { IOnClientCommandMeta } from '@secret-agent/interfaces/ICorePlugin';
2
import CorePlugin from '@secret-agent/plugin-utils/lib/CorePlugin';
3
import { IExecuteJsArgs } from './IExecuteJsArgs';
4
5
const { name: pluginId } = require('../package.json');
6
7
export default class ExecuteJsCorePlugin extends CorePlugin {
8
public static id = pluginId;
9
10
public async onClientCommand(
11
{ puppetFrame, puppetPage }: IOnClientCommandMeta,
12
args: IExecuteJsArgs,
13
): Promise<any> {
14
const { fnName, fnSerialized, isolateFromWebPageEnvironment } = args;
15
const frame = puppetFrame ?? puppetPage.mainFrame;
16
const result = await frame.evaluate<any>(fnSerialized, isolateFromWebPageEnvironment);
17
18
if ((result as any)?.error) {
19
this.logger.error<any>(fnName, { error: result.error });
20
throw new Error((result as any).error as string);
21
} else {
22
return result as any;
23
}
24
}
25
}
26
27