diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts index f50c8f87ff2..3de32d813e7 100644 --- a/packages/puppeteer-core/src/api/Frame.ts +++ b/packages/puppeteer-core/src/api/Frame.ts @@ -764,9 +764,64 @@ export class Frame { */ async addScriptTag( options: FrameAddScriptTagOptions - ): Promise>; - async addScriptTag(): Promise> { - throw new Error('Not implemented'); + ): Promise> { + let {content = '', type} = options; + const {path} = options; + if (+!!options.url + +!!path + +!!content !== 1) { + throw new Error( + 'Exactly one of `url`, `path`, or `content` must be specified.' + ); + } + + if (path) { + const fs = await importFSPromises(); + content = await fs.readFile(path, 'utf8'); + content += `//# sourceURL=${path.replace(/\n/g, '')}`; + } + + type = type ?? 'text/javascript'; + + return this.mainRealm().transferHandle( + await this.isolatedRealm().evaluateHandle( + async ({Deferred}, {url, id, type, content}) => { + const deferred = Deferred.create(); + const script = document.createElement('script'); + script.type = type; + script.text = content; + if (url) { + script.src = url; + script.addEventListener( + 'load', + () => { + return deferred.resolve(); + }, + {once: true} + ); + script.addEventListener( + 'error', + event => { + deferred.reject( + new Error(event.message ?? 'Could not load script') + ); + }, + {once: true} + ); + } else { + deferred.resolve(); + } + if (id) { + script.id = id; + } + document.head.appendChild(script); + await deferred.valueOrThrow(); + return script; + }, + LazyArg.create(context => { + return context.puppeteerUtil; + }), + {...options, type, content} + ) + ); } /** diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts index 767128bdacb..2fafdc6eaa7 100644 --- a/packages/puppeteer-core/src/api/Page.ts +++ b/packages/puppeteer-core/src/api/Page.ts @@ -1183,9 +1183,8 @@ export class Page extends EventEmitter { */ async addScriptTag( options: FrameAddScriptTagOptions - ): Promise>; - async addScriptTag(): Promise> { - throw new Error('Not implemented'); + ): Promise> { + return this.mainFrame().addScriptTag(options); } /** diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index 43c315a3396..ea32041ace1 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -17,7 +17,7 @@ import {Protocol} from 'devtools-protocol'; import {ElementHandle} from '../api/ElementHandle.js'; -import {Frame as BaseFrame, FrameAddScriptTagOptions} from '../api/Frame.js'; +import {Frame as BaseFrame} from '../api/Frame.js'; import {HTTPResponse} from '../api/HTTPResponse.js'; import {Page, WaitTimeoutOptions} from '../api/Page.js'; import {assert} from '../util/assert.js'; @@ -33,10 +33,9 @@ import {ExecutionContext} from './ExecutionContext.js'; import {FrameManager} from './FrameManager.js'; import {IsolatedWorld} from './IsolatedWorld.js'; import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js'; -import {LazyArg} from './LazyArg.js'; import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js'; import {EvaluateFunc, EvaluateFuncWith, HandleFor, NodeFor} from './types.js'; -import {importFSPromises, withSourcePuppeteerURLIfNone} from './util.js'; +import {withSourcePuppeteerURLIfNone} from './util.js'; /** * @internal @@ -333,68 +332,6 @@ export class Frame extends BaseFrame { return this.#detached; } - override async addScriptTag( - options: FrameAddScriptTagOptions - ): Promise> { - let {content = '', type} = options; - const {path} = options; - if (+!!options.url + +!!path + +!!content !== 1) { - throw new Error( - 'Exactly one of `url`, `path`, or `content` must be specified.' - ); - } - - if (path) { - const fs = await importFSPromises(); - content = await fs.readFile(path, 'utf8'); - content += `//# sourceURL=${path.replace(/\n/g, '')}`; - } - - type = type ?? 'text/javascript'; - - return this.mainRealm().transferHandle( - await this.isolatedRealm().evaluateHandle( - async ({Deferred}, {url, id, type, content}) => { - const deferred = Deferred.create(); - const script = document.createElement('script'); - script.type = type; - script.text = content; - if (url) { - script.src = url; - script.addEventListener( - 'load', - () => { - return deferred.resolve(); - }, - {once: true} - ); - script.addEventListener( - 'error', - event => { - deferred.reject( - new Error(event.message ?? 'Could not load script') - ); - }, - {once: true} - ); - } else { - deferred.resolve(); - } - if (id) { - script.id = id; - } - document.head.appendChild(script); - await deferred.valueOrThrow(); - return script; - }, - LazyArg.create(context => { - return context.puppeteerUtil; - }), - {...options, type, content} - ) - ); - } - override async title(): Promise { return this.isolatedRealm().title(); } diff --git a/packages/puppeteer-core/src/common/Page.ts b/packages/puppeteer-core/src/common/Page.ts index b8e12f3099b..1b7707ad2be 100644 --- a/packages/puppeteer-core/src/common/Page.ts +++ b/packages/puppeteer-core/src/common/Page.ts @@ -21,7 +21,7 @@ import {Protocol} from 'devtools-protocol'; import type {Browser} from '../api/Browser.js'; import type {BrowserContext} from '../api/BrowserContext.js'; import {ElementHandle} from '../api/ElementHandle.js'; -import {Frame, FrameAddScriptTagOptions} from '../api/Frame.js'; +import {Frame} from '../api/Frame.js'; import {HTTPRequest} from '../api/HTTPRequest.js'; import {HTTPResponse} from '../api/HTTPResponse.js'; import {JSHandle} from '../api/JSHandle.js'; @@ -578,12 +578,6 @@ export class CDPPage extends Page { } } - override async addScriptTag( - options: FrameAddScriptTagOptions - ): Promise> { - return this.mainFrame().addScriptTag(options); - } - override async exposeFunction( name: string, pptrFunction: Function | {default: Function} diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 290a951d1ac..b15fd68e124 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -155,6 +155,12 @@ "parameters": ["webDriverBiDi"], "expectations": ["FAIL", "TIMEOUT"] }, + { + "testIdPattern": "[page.spec] Page Page.addScriptTag *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[page.spec] Page Page.addStyleTag *", "platforms": ["darwin", "linux", "win32"], @@ -311,6 +317,12 @@ "parameters": ["webDriverBiDi"], "expectations": ["PASS"] }, + { + "testIdPattern": "[CDPSession.spec] Target.createCDPSession *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["firefox", "webDriverBiDi"], + "expectations": ["FAIL"] + }, { "testIdPattern": "[CDPSession.spec] Target.createCDPSession should not report created targets for custom CDP sessions", "platforms": ["darwin", "linux", "win32"], @@ -1107,13 +1119,7 @@ "testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with content to the CSP page", "platforms": ["darwin", "linux", "win32"], "parameters": ["webDriverBiDi"], - "expectations": ["PASS"] - }, - { - "testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with URL to the CSP page", - "platforms": ["darwin", "linux", "win32"], - "parameters": ["webDriverBiDi"], - "expectations": ["PASS"] + "expectations": ["FAIL"] }, { "testIdPattern": "[page.spec] Page Page.addStyleTag should throw when added with content to the CSP page", @@ -3053,12 +3059,6 @@ "parameters": ["cdp", "firefox"], "expectations": ["FAIL"] }, - { - "testIdPattern": "[page.spec] Page Page.addScriptTag should throw an error if loading from url fail", - "platforms": ["darwin", "linux", "win32"], - "parameters": ["firefox", "webDriverBiDi"], - "expectations": ["PASS"] - }, { "testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with content to the CSP page", "platforms": ["darwin", "linux", "win32"],