From 957cb8ee6c2745db0e970677f90beaa43418cf6d Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 27 Mar 2024 17:23:51 +0100 Subject: [PATCH] refactor: remove deferred from addScriptTag (#12165) --- packages/puppeteer-core/src/api/Frame.ts | 53 +++++++++++------------- test/TestExpectations.json | 4 +- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts index dd9bd6a44d2..0252f12ddb3 100644 --- a/packages/puppeteer-core/src/api/Frame.ts +++ b/packages/puppeteer-core/src/api/Frame.ts @@ -841,42 +841,37 @@ export abstract class Frame extends EventEmitter { return await 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} - ); + async ({url, id, type, content}) => { + return await new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.type = type; + script.text = content; script.addEventListener( 'error', event => { - deferred.reject( - new Error(event.message ?? 'Could not load script') - ); + 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; + if (id) { + script.id = id; + } + if (url) { + script.src = url; + script.addEventListener( + 'load', + () => { + resolve(script); + }, + {once: true} + ); + document.head.appendChild(script); + } else { + document.head.appendChild(script); + resolve(script); + } + }); }, - LazyArg.create(context => { - return context.puppeteerUtil; - }), {...options, type, content} ) ); diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 490f46a903e..9cb4dbac456 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -649,7 +649,7 @@ "platforms": ["darwin", "linux", "win32"], "parameters": ["webDriverBiDi"], "expectations": ["FAIL"], - "comment": "TODO: add a comment explaining why this expectation is required (include links to issues)" + "comment": "See https://github.com/puppeteer/puppeteer/issues/4840" }, { "testIdPattern": "[page.spec] Page Page.addStyleTag should throw when added with content to the CSP page", @@ -3010,7 +3010,7 @@ "platforms": ["darwin", "linux", "win32"], "parameters": ["cdp", "chrome"], "expectations": ["SKIP"], - "comment": "TODO: add a comment explaining why this expectation is required (include links to issues)" + "comment": "See https://github.com/puppeteer/puppeteer/issues/4840" }, { "testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with content to the CSP page",