mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: add DeferredPromise
to injection (#8883)
This commit is contained in:
parent
6b4a99fcfa
commit
2d50ec5b38
@ -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
|
||||||
)
|
)
|
||||||
|
@ -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,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -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 */
|
||||||
|
Loading…
Reference in New Issue
Block a user