mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: remove deferred from addStyleTag (#12166)
This commit is contained in:
parent
957cb8ee6c
commit
b1d30a6158
@ -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
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user