Skip to content

Commit

Permalink
refactor: remove deferred from addScriptTag (#12165)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored Mar 27, 2024
1 parent c9ad6b4 commit 957cb8e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
53 changes: 24 additions & 29 deletions packages/puppeteer-core/src/api/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,42 +841,37 @@ export abstract class Frame extends EventEmitter<FrameEvents> {

return await this.mainRealm().transferHandle(
await this.isolatedRealm().evaluateHandle(
async ({Deferred}, {url, id, type, content}) => {
const deferred = Deferred.create<void>();
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<HTMLScriptElement>((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}
)
);
Expand Down
4 changes: 2 additions & 2 deletions test/TestExpectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 957cb8e

Please sign in to comment.