refactor: remove deferred from addStyleTag (#12166)

This commit is contained in:
Alex Rudenko 2024-03-27 19:30:20 +01:00 committed by GitHub
parent 957cb8ee6c
commit b1d30a6158
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,7 +18,6 @@ import type {PuppeteerLifeCycleEvent} from '../cdp/LifecycleWatcher.js';
import {EventEmitter, type EventType} from '../common/EventEmitter.js'; import {EventEmitter, type EventType} from '../common/EventEmitter.js';
import {getQueryHandlerAndSelector} from '../common/GetQueryHandler.js'; import {getQueryHandlerAndSelector} from '../common/GetQueryHandler.js';
import {transposeIterableHandle} from '../common/HandleIterator.js'; import {transposeIterableHandle} from '../common/HandleIterator.js';
import {LazyArg} from '../common/LazyArg.js';
import type { import type {
Awaitable, Awaitable,
EvaluateFunc, EvaluateFunc,
@ -921,46 +920,42 @@ export abstract class Frame extends EventEmitter<FrameEvents> {
} }
return await this.mainRealm().transferHandle( return await this.mainRealm().transferHandle(
await this.isolatedRealm().evaluateHandle( await this.isolatedRealm().evaluateHandle(async ({url, content}) => {
async ({Deferred}, {url, content}) => { return await new Promise<HTMLStyleElement | HTMLLinkElement>(
const deferred = Deferred.create<void>(); (resolve, reject) => {
let element: HTMLStyleElement | HTMLLinkElement; let element: HTMLStyleElement | HTMLLinkElement;
if (!url) { if (!url) {
element = document.createElement('style'); element = document.createElement('style');
element.appendChild(document.createTextNode(content!)); element.appendChild(document.createTextNode(content!));
} else { } else {
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.href = url; link.href = url;
element = link; element = link;
}
element.addEventListener(
'load',
() => {
resolve(element);
},
{once: true}
);
element.addEventListener(
'error',
event => {
reject(
new Error(
(event as ErrorEvent).message ?? 'Could not load style'
)
);
},
{once: true}
);
document.head.appendChild(element);
return element;
} }
element.addEventListener( );
'load', }, options)
() => {
deferred.resolve();
},
{once: true}
);
element.addEventListener(
'error',
event => {
deferred.reject(
new Error(
(event as ErrorEvent).message ?? 'Could not load style'
)
);
},
{once: true}
);
document.head.appendChild(element);
await deferred.valueOrThrow();
return element;
},
LazyArg.create(context => {
return context.puppeteerUtil;
}),
options
)
); );
} }