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
9 changes: 5 additions & 4 deletions src/main/cdp/execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,18 @@ export class ExecutionContext {
throw err;
}

initContentScripts(scripts: ContentScript[]) {
async initContentScripts(scripts: ContentScript[]) {
const options = {
toolkitBinding: this.page.toolkitBinding,
};
for (const { fn, filename } of scripts) {
const promises = scripts.map(async ({ fn, filename }) => {
const source = `(${fn.toString()})(${JSON.stringify(options)})\n//# sourceURL=${filename}\n`;
this.page.sendAndForget('Runtime.evaluate', {
await this.page.send('Runtime.evaluate', {
contextId: this.executionContextId,
expression: source,
});
}
});
await Promise.allSettled(promises);
}

protected onExecutionContextsCleared() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/cdp/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Frame extends EventEmitter {
grantUniveralAccess: true,
});
this._isolatedWorld = new ExecutionContext(this, executionContextId);
this._isolatedWorld.initContentScripts(runtimeScripts);
await this._isolatedWorld.initContentScripts(runtimeScripts);
return this._isolatedWorld;
}

Expand Down
9 changes: 1 addition & 8 deletions src/main/cdp/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter } from 'events';

import { Exception } from '../exception.js';
import { Browser } from './browser.js';
import { runtimeScripts, stubScripts } from './inject/index.js';
import { stubScripts } from './inject/index.js';
import { InterceptedRequest } from './interceptor.js';
import { Page, PageNavigateOptions, PageWaitOptions } from './page.js';
import { CdpFrame, CdpLifecycleEvent, CdpLoadingFailed, CdpRequestPaused, CdpRequestWillBeSent, CdpResponse, CdpResponseReceived, CdpTargetInfo, CdpTargetType } from './types.js';
Expand Down Expand Up @@ -382,12 +382,5 @@ export class Target extends EventEmitter {
source: `(${fn.toString()})()`
});
});
runtimeScripts.forEach(({ fn, filename }) => {
const options = {
toolkitBinding: this.browser.config.toolkitBinding,
};
const source = `(${fn.toString()})(${JSON.stringify(options)})\n//# sourceURL=${filename}\n`;
this.sendAndForget('Page.addScriptToEvaluateOnNewDocument', { source });
});
}
}