33using System . Text . Json ;
44using System . Threading . Tasks ;
55using Microsoft . Extensions . Logging ;
6+ using PuppeteerSharp . Helpers ;
67using PuppeteerSharp . Input ;
78using PuppeteerSharp . QueryHandlers ;
89
@@ -196,7 +197,7 @@ public async Task<IElementHandle[]> XPathAsync(string expression)
196197 }
197198
198199 /// <inheritdoc/>
199- public Task < DeviceRequestPrompt > WaitForDevicePromptAsync ( WaitForOptions options = default )
200+ public Task < DeviceRequestPrompt > WaitForDevicePromptAsync ( WaitForOptions options = null )
200201 => GetDeviceRequestPromptManager ( ) . WaitForDevicePromptAsync ( options ) ;
201202
202203 /// <inheritdoc/>
@@ -220,45 +221,50 @@ public async Task<IElementHandle> AddScriptTagAsync(AddTagOptions options)
220221
221222 if ( ! string . IsNullOrEmpty ( options . Path ) )
222223 {
223- content = await Helpers . AsyncFileHelper . ReadAllText ( options . Path ) . ConfigureAwait ( false ) ;
224+ content = await AsyncFileHelper . ReadAllText ( options . Path ) . ConfigureAwait ( false ) ;
224225 content += "//# sourceURL=" + options . Path . Replace ( "\n " , string . Empty ) ;
225226 }
226227
227- var type = options . Type ?? "text/javascript" ;
228-
229228 var handle = await IsolatedRealm . EvaluateFunctionHandleAsync (
230- @"async ({url, id, type, content}) => {
231- return await new Promise((resolve, reject) => {
232- const script = document.createElement('script');
233- script.type = type;
234- script.text = content;
229+ @"async (puppeteerUtil, url, id, type, content) => {
230+ const createDeferredPromise = puppeteerUtil.createDeferredPromise;
231+ const promise = createDeferredPromise();
232+ const script = document.createElement('script');
233+ script.type = type;
234+ script.text = content;
235+ if (url) {
236+ script.src = url;
237+ script.addEventListener(
238+ 'load',
239+ () => {
240+ return promise.resolve();
241+ },
242+ {once: true}
243+ );
235244 script.addEventListener(
236245 'error',
237246 event => {
238- reject(new Error(event.message ?? 'Could not load script'));
247+ promise.reject(
248+ new Error(event.message ?? 'Could not load script')
249+ );
239250 },
240251 {once: true}
241252 );
242- if (id) {
243- script.id = id;
244- }
245- if (url) {
246- script.src = url;
247- script.addEventListener(
248- 'load',
249- () => {
250- resolve(script);
251- },
252- {once: true}
253- );
254- document.head.appendChild(script);
255- } else {
256- document.head.appendChild(script);
257- resolve(script);
258- }
259- });
253+ } else {
254+ promise.resolve();
255+ }
256+ if (id) {
257+ script.id = id;
258+ }
259+ document.head.appendChild(script);
260+ await promise;
261+ return script;
260262 }" ,
261- new { url = options . Url , id = options . Id , type , content } ) . ConfigureAwait ( false ) ;
263+ new LazyArg ( async context => await context . GetPuppeteerUtilAsync ( ) . ConfigureAwait ( false ) ) ,
264+ options . Url ,
265+ options . Id ,
266+ options . Type ,
267+ content ) . ConfigureAwait ( false ) ;
262268
263269 return ( await MainRealm . TransferHandleAsync ( handle ) . ConfigureAwait ( false ) ) as IElementHandle ;
264270 }
0 commit comments