11using System ;
22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
4- using System . IO ;
5- using System . Reflection ;
64using System . Threading . Tasks ;
75using Microsoft . Extensions . Logging ;
86using Newtonsoft . Json . Linq ;
@@ -21,7 +19,6 @@ namespace PuppeteerSharp
2119
2220 internal class IsolatedWorld
2321 {
24- private static string _injectedSource ;
2522 private readonly FrameManager _frameManager ;
2623 private readonly TimeoutSettings _timeoutSettings ;
2724 private readonly CDPSession _client ;
@@ -56,10 +53,6 @@ public IsolatedWorld(
5653
5754 internal ConcurrentDictionary < string , Delegate > BoundFunctions { get ; } = new ( ) ;
5855
59- internal TaskCompletionSource < IJSHandle > PuppeteerUtilTaskCompletionSource { get ; private set ; } = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
60-
61- internal Task < IJSHandle > GetPuppeteerUtilAsync ( ) => PuppeteerUtilTaskCompletionSource . Task ;
62-
6356 internal async Task AddBindingToContextAsync ( ExecutionContext context , string name )
6457 {
6558 // Previous operation added the binding so we are done.
@@ -282,7 +275,7 @@ internal async Task<IElementHandle> WaitForSelectorInPageAsync(string queryOne,
282275
283276 var args = new List < object >
284277 {
285- await GetPuppeteerUtilAsync ( ) . ConfigureAwait ( false ) ,
278+ new LazyArg ( async context => await context . GetPuppeteerUtilAsync ( ) . ConfigureAwait ( false ) ) ,
286279 queryOne ,
287280 selector ,
288281 root ,
@@ -438,7 +431,6 @@ internal void ClearContext()
438431 {
439432 _documentTask = null ;
440433 _contextResolveTaskWrapper = new TaskCompletionSource < ExecutionContext > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
441- PuppeteerUtilTaskCompletionSource = new TaskCompletionSource < IJSHandle > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
442434 }
443435
444436 internal void SetContext ( ExecutionContext context )
@@ -448,42 +440,11 @@ internal void SetContext(ExecutionContext context)
448440 throw new ArgumentNullException ( nameof ( context ) ) ;
449441 }
450442
451- _ = InjectPuppeteerUtil ( context ) ;
452443 _ctxBindings . Clear ( ) ;
453444 _contextResolveTaskWrapper . TrySetResult ( context ) ;
454445 TaskManager . RerunAll ( ) ;
455446 }
456447
457- private static string GetInjectedSource ( )
458- {
459- if ( string . IsNullOrEmpty ( _injectedSource ) )
460- {
461- var assembly = Assembly . GetExecutingAssembly ( ) ;
462- var resourceName = "PuppeteerSharp.Injected.injected.js" ;
463-
464- using var stream = assembly . GetManifestResourceStream ( resourceName ) ;
465- using var reader = new StreamReader ( stream ) ;
466- var fileContent = reader . ReadToEnd ( ) ;
467- _injectedSource = fileContent ;
468- }
469-
470- return _injectedSource ;
471- }
472-
473- private async Task InjectPuppeteerUtil ( ExecutionContext context )
474- {
475- try
476- {
477- var injectedSource = GetInjectedSource ( ) ;
478- var handle = await context . EvaluateExpressionHandleAsync ( injectedSource ) . ConfigureAwait ( false ) ;
479- PuppeteerUtilTaskCompletionSource . TrySetResult ( handle ) ;
480- }
481- catch ( Exception ex )
482- {
483- _logger . LogError ( ex . ToString ( ) ) ;
484- }
485- }
486-
487448 private async void Client_MessageReceived ( object sender , MessageEventArgs e )
488449 {
489450 try
0 commit comments