chore: add DeferredPromise to injection (#8883)

This commit is contained in:
jrandolf 2022-09-01 18:44:59 +02:00 committed by GitHub
parent 6b4a99fcfa
commit 2d50ec5b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 53 deletions

View File

@ -777,38 +777,35 @@ export class Frame {
return this.worlds[MAIN_WORLD].transferHandle( return this.worlds[MAIN_WORLD].transferHandle(
await this.worlds[PUPPETEER_WORLD].evaluateHandle( await this.worlds[PUPPETEER_WORLD].evaluateHandle(
async ({url, id, type, content}) => { async ({url, id, type, content}) => {
const promise = InjectedUtil.createDeferredPromise<void>();
const script = document.createElement('script'); const script = document.createElement('script');
script.type = type; script.type = type;
script.text = content; script.text = content;
if (url) { if (url) {
script.src = url; script.src = url;
script.addEventListener(
'load',
() => {
return promise.resolve();
},
{once: true}
);
script.addEventListener(
'error',
event => {
promise.reject(
new Error(event.message ?? 'Could not load script')
);
},
{once: true}
);
} else {
promise.resolve();
} }
if (id) { if (id) {
script.id = id; script.id = id;
} }
let resolve: undefined | ((value?: unknown) => void);
const promise = new Promise((res, rej) => {
if (url) {
script.addEventListener('load', res, {once: true});
} else {
resolve = res;
}
script.addEventListener(
'error',
event => {
let message = 'Could not load script';
if (event instanceof ErrorEvent) {
message = event.message ?? message;
}
rej(message);
},
{once: true}
);
});
document.head.appendChild(script); document.head.appendChild(script);
if (resolve) {
resolve();
}
await promise; await promise;
return script; return script;
}, },
@ -862,39 +859,38 @@ export class Frame {
return this.worlds[MAIN_WORLD].transferHandle( return this.worlds[MAIN_WORLD].transferHandle(
await this.worlds[PUPPETEER_WORLD].evaluateHandle( await this.worlds[PUPPETEER_WORLD].evaluateHandle(
async ({url, content}) => { async ({url, content}) => {
const promise = InjectedUtil.createDeferredPromise<void>();
let element: HTMLStyleElement | HTMLLinkElement;
if (!url) { if (!url) {
const style = document.createElement('style'); element = document.createElement('style');
style.appendChild(document.createTextNode(content!)); element.appendChild(document.createTextNode(content!));
const promise = new Promise((res, rej) => { } else {
style.addEventListener('load', res, {once: true});
style.addEventListener(
'error',
event => {
rej(event.message ?? 'Could not load style');
},
{once: true}
);
});
document.head.appendChild(style);
await promise;
return style;
}
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.href = url; link.href = url;
const promise = new Promise((res, rej) => { element = link;
link.addEventListener('load', res, {once: true}); }
link.addEventListener( element.addEventListener(
'error', 'load',
event => { () => {
rej(event.message ?? 'Could not load style'); promise.resolve();
}, },
{once: true} {once: true}
); );
}); element.addEventListener(
document.head.appendChild(link); 'error',
event => {
promise.reject(
new Error(
(event as ErrorEvent).message ?? 'Could not load style'
)
);
},
{once: true}
);
document.head.appendChild(element);
await promise; await promise;
return link; return element;
}, },
options options
) )

View File

@ -1,3 +1,4 @@
import {createDeferredPromise} from '../util/DeferredPromise.js';
import * as Poller from './Poller.js'; import * as Poller from './Poller.js';
import * as util from './util.js'; import * as util from './util.js';
@ -7,6 +8,7 @@ Object.assign(
InjectedUtil: { InjectedUtil: {
...Poller, ...Poller,
...util, ...util,
createDeferredPromise,
}, },
}) })
); );

View File

@ -1,8 +1,11 @@
import * as Poller from '../injected/Poller.js'; import * as Poller from '../injected/Poller.js';
import * as util from '../injected/util.js'; import * as util from '../injected/util.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
declare global { declare global {
const InjectedUtil: Readonly<typeof Poller & typeof util>; const InjectedUtil: Readonly<typeof Poller & typeof util> & {
createDeferredPromise: typeof createDeferredPromise;
};
} }
/** @internal */ /** @internal */