feat: support running Puppeteer in extensions (#12459)

This commit is contained in:
Alex Rudenko
2024-05-21 12:41:15 +02:00
committed by GitHub
parent c8a64c0f79
commit 3c6f01a31d
10 changed files with 335 additions and 3 deletions

View File

@@ -30,7 +30,7 @@ const pageTargetInfo = {
* implements missing commands and events.
*
* @experimental
* @internal
* @public
*/
export class ExtensionTransport implements ConnectionTransport {
static async connectTab(tabId: number): Promise<ExtensionTransport> {
@@ -178,5 +178,6 @@ export class ExtensionTransport implements ConnectionTransport {
close(): void {
chrome.debugger.onEvent.removeListener(this.#debuggerEventHandler);
void chrome.debugger.detach({tabId: this.#tabId});
}
}

View File

@@ -31,7 +31,16 @@ export function stringifyFunction(fn: (...args: never) => unknown): string {
let value = fn.toString();
try {
new Function(`(${value})`);
} catch {
} catch (err) {
if (
(err as Error).message.includes(
`Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive`
)
) {
// The content security policy does not allow Function eval. Let's
// assume the value might be valid as is.
return value;
}
// This means we might have a function shorthand (e.g. `test(){}`). Let's
// try prefixing.
let prefix = 'function ';