@@ -252,7 +252,7 @@ public Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelector
252252 /// <seealso cref="Page.WaitForXPathAsync(string, WaitForSelectorOptions)"/>
253253 public Task < ElementHandle > WaitForXPathAsync ( string xpath , WaitForSelectorOptions options = null )
254254 => WaitForSelectorOrXPathAsync ( xpath , true , options ) ;
255-
255+
256256 /// <summary>
257257 /// Waits for a timeout
258258 /// </summary>
@@ -406,31 +406,40 @@ public async Task<ElementHandle> AddStyleTag(AddTagOptions options)
406406 /// <seealso cref="Page.AddScriptTagAsync(string)"/>
407407 public async Task < ElementHandle > AddScriptTag ( AddTagOptions options )
408408 {
409- const string addScriptUrl = @"async function addScriptUrl(url) {
409+ const string addScriptUrl = @"async function addScriptUrl(url, type ) {
410410 const script = document.createElement('script');
411411 script.src = url;
412+ if(type)
413+ script.type = type;
412414 document.head.appendChild(script);
413415 await new Promise((res, rej) => {
414416 script.onload = res;
415417 script.onerror = rej;
416418 });
417419 return script;
418420 }" ;
419- const string addScriptContent = @"function addScriptContent(content) {
421+ const string addScriptContent = @"function addScriptContent(content, type = 'text/javascript' ) {
420422 const script = document.createElement('script');
421- script.type = 'text/javascript' ;
423+ script.type = type ;
422424 script.text = content;
423425 document.head.appendChild(script);
424426 return script;
425427 }" ;
426428
429+ async Task < ElementHandle > AddScriptTagPrivate ( string script , string urlOrContent , string type )
430+ {
431+ var context = await GetExecutionContextAsync ( ) ;
432+ return ( string . IsNullOrEmpty ( type )
433+ ? await context . EvaluateFunctionHandleAsync ( script , urlOrContent )
434+ : await context . EvaluateFunctionHandleAsync ( script , urlOrContent , type ) ) as ElementHandle ;
435+ }
436+
427437 if ( ! string . IsNullOrEmpty ( options . Url ) )
428438 {
429439 var url = options . Url ;
430440 try
431441 {
432- var context = await GetExecutionContextAsync ( ) ;
433- return ( await context . EvaluateFunctionHandleAsync ( addScriptUrl , url ) ) as ElementHandle ;
442+ return await AddScriptTagPrivate ( addScriptUrl , url , options . Type ) ;
434443 }
435444 catch ( PuppeteerException )
436445 {
@@ -442,14 +451,12 @@ public async Task<ElementHandle> AddScriptTag(AddTagOptions options)
442451 {
443452 var contents = File . ReadAllText ( options . Path , Encoding . UTF8 ) ;
444453 contents += "//# sourceURL=" + options . Path . Replace ( "\n " , string . Empty ) ;
445- var context = await GetExecutionContextAsync ( ) ;
446- return ( await context . EvaluateFunctionHandleAsync ( addScriptContent , contents ) ) as ElementHandle ;
454+ return await AddScriptTagPrivate ( addScriptContent , contents , options . Type ) ;
447455 }
448456
449457 if ( ! string . IsNullOrEmpty ( options . Content ) )
450458 {
451- var context = await GetExecutionContextAsync ( ) ;
452- return ( await context . EvaluateFunctionHandleAsync ( addScriptContent , options . Content ) ) as ElementHandle ;
459+ return await AddScriptTagPrivate ( addScriptContent , options . Content , options . Type ) ;
453460 }
454461
455462 throw new ArgumentException ( "Provide options with a `Url`, `Path` or `Content` property" ) ;
0 commit comments