@@ -84,6 +84,154 @@ public event Action OnDidFinishLoad
8484
8585 private event Action _didFinishLoad ;
8686
87+ /// <summary>
88+ /// Emitted when any frame (including main) starts navigating.
89+ /// </summary>
90+ public event Action < string > OnDidStartNavigation
91+ {
92+ add
93+ {
94+ if ( _didStartNavigation == null )
95+ {
96+ BridgeConnector . Socket . On < string > ( "webContents-didStartNavigation" + Id , ( url ) =>
97+ {
98+ _didStartNavigation ( url ) ;
99+ } ) ;
100+
101+ BridgeConnector . Socket . Emit ( "register-webContents-didStartNavigation" , Id ) ;
102+ }
103+ _didStartNavigation += value ;
104+ }
105+ remove
106+ {
107+ _didStartNavigation -= value ;
108+
109+ if ( _didStartNavigation == null )
110+ BridgeConnector . Socket . Off ( "webContents-didStartNavigation" + Id ) ;
111+ }
112+ }
113+
114+ private event Action < string > _didStartNavigation ;
115+
116+ /// <summary>
117+ /// Emitted when a main frame navigation is done.
118+ /// This event is not emitted for in-page navigations, such as clicking anchor links or updating the window.location.hash. Use did-navigate-in-page event for this purpose.
119+ /// </summary>
120+ public event Action < OnDidNavigateInfo > OnDidNavigate
121+ {
122+ add
123+ {
124+ if ( _didNavigate == null )
125+ {
126+ BridgeConnector . Socket . On < OnDidNavigateInfo > ( "webContents-didNavigate" + Id , ( data ) =>
127+ {
128+ _didNavigate ( data ) ;
129+ } ) ;
130+
131+ BridgeConnector . Socket . Emit ( "register-webContents-didNavigate" , Id ) ;
132+ }
133+ _didNavigate += value ;
134+ }
135+ remove
136+ {
137+ _didNavigate -= value ;
138+
139+ if ( _didNavigate == null )
140+ BridgeConnector . Socket . Off ( "webContents-didNavigate" + Id ) ;
141+ }
142+ }
143+
144+ private event Action < OnDidNavigateInfo > _didNavigate ;
145+
146+ /// <summary>
147+ /// Emitted when a server side redirect occurs during navigation. For example a 302 redirect.
148+ /// This event will be emitted after OnDidStartNavigation and always before the OnDidRedirectNavigation event for the same navigation.
149+ /// </summary>
150+ public event Action < string > OnWillRedirect
151+ {
152+ add
153+ {
154+ if ( _willRedirect == null )
155+ {
156+ BridgeConnector . Socket . On < string > ( "webContents-willRedirect" + Id , ( url ) =>
157+ {
158+ _willRedirect ( url ) ;
159+ } ) ;
160+
161+ BridgeConnector . Socket . Emit ( "register-webContents-willRedirect" , Id ) ;
162+ }
163+ _willRedirect += value ;
164+ }
165+ remove
166+ {
167+ _willRedirect -= value ;
168+
169+ if ( _willRedirect == null )
170+ BridgeConnector . Socket . Off ( "webContents-willRedirect" + Id ) ;
171+ }
172+ }
173+
174+ private event Action < string > _willRedirect ;
175+
176+ /// <summary>
177+ /// Emitted after a server side redirect occurs during navigation. For example a 302 redirect.
178+ /// </summary>
179+ public event Action < string > OnDidRedirectNavigation
180+ {
181+ add
182+ {
183+ if ( _didRedirectNavigation == null )
184+ {
185+ BridgeConnector . Socket . On ( "webContents-didRedirectNavigation" + Id , ( url ) =>
186+ {
187+ _didRedirectNavigation ( url ? . ToString ( ) ) ;
188+ } ) ;
189+
190+ BridgeConnector . Socket . Emit ( "register-webContents-didRedirectNavigation" , Id ) ;
191+ }
192+ _didRedirectNavigation += value ;
193+ }
194+ remove
195+ {
196+ _didRedirectNavigation -= value ;
197+
198+ if ( _didRedirectNavigation == null )
199+ BridgeConnector . Socket . Off ( "webContents-didRedirectNavigation" + Id ) ;
200+ }
201+ }
202+
203+ private event Action < string > _didRedirectNavigation ;
204+
205+
206+ /// <summary>
207+ /// This event is like OnDidFinishLoad but emitted when the load failed.
208+ /// </summary>
209+ public event Action < OnDidFailLoadInfo > OnDidFailLoad
210+ {
211+ add
212+ {
213+ if ( _didFailLoad == null )
214+ {
215+ BridgeConnector . Socket . On ( "webContents-willRedirect" + Id , ( data ) =>
216+ {
217+ _didFailLoad ( ( ( JObject ) data ) . ToObject < OnDidFailLoadInfo > ( ) ) ;
218+ } ) ;
219+
220+ BridgeConnector . Socket . Emit ( "register-webContents-willRedirect" , Id ) ;
221+ }
222+ _didFailLoad += value ;
223+ }
224+ remove
225+ {
226+ _didFailLoad -= value ;
227+
228+ if ( _didFailLoad == null )
229+ BridgeConnector . Socket . Off ( "webContents-willRedirect" + Id ) ;
230+ }
231+ }
232+
233+ private event Action < OnDidFailLoadInfo > _didFailLoad ;
234+
87235 /// <summary>
88236 /// Emitted when an input event is sent to the WebContents.
89237 /// </summary>
@@ -114,6 +262,35 @@ public event Action<InputEvent> InputEvent
114262
115263 private event Action < InputEvent > _inputEvent ;
116264
265+ /// <summary>
266+ /// Emitted when the document in the top-level frame is loaded.
267+ /// </summary>
268+ public event Action OnDomReady
269+ {
270+ add
271+ {
272+ if ( _domReady == null )
273+ {
274+ BridgeConnector . Socket . On ( "webContents-domReady" + Id , ( ) =>
275+ {
276+ _domReady ( ) ;
277+ } ) ;
278+
279+ BridgeConnector . Socket . Emit ( "register-webContents-domReady" , Id ) ;
280+ }
281+ _domReady += value ;
282+ }
283+ remove
284+ {
285+ _domReady -= value ;
286+
287+ if ( _domReady == null )
288+ BridgeConnector . Socket . Off ( "webContents-domReady" + Id ) ;
289+ }
290+ }
291+
292+ private event Action _domReady ;
293+
117294 internal WebContents ( int id )
118295 {
119296 Id = id ;
@@ -215,6 +392,37 @@ public Task<bool> PrintToPDFAsync(string path, PrintToPDFOptions options = null)
215392 return taskCompletionSource . Task ;
216393 }
217394
395+ /// <summary>
396+ /// Evaluates script code in page.
397+ /// </summary>
398+ /// <param name="code">The code to execute.</param>
399+ /// <param name="userGesture">if set to <c>true</c> simulate a user gesture.</param>
400+ /// <returns>The result of the executed code.</returns>
401+ /// <remarks>
402+ /// <para>
403+ /// In the browser window some HTML APIs like `requestFullScreen` can only be
404+ /// invoked by a gesture from the user. Setting `userGesture` to `true` will remove
405+ /// this limitation.
406+ /// </para>
407+ /// <para>
408+ /// Code execution will be suspended until web page stop loading.
409+ /// </para>
410+ /// </remarks>
411+ public Task < object > ExecuteJavaScriptAsync ( string code , bool userGesture = false )
412+ {
413+ var taskCompletionSource = new TaskCompletionSource < object > ( ) ;
414+
415+ BridgeConnector . Socket . On ( "webContents-executeJavaScript-completed" , ( result ) =>
416+ {
417+ BridgeConnector . Socket . Off ( "webContents-executeJavaScript-completed" ) ;
418+ taskCompletionSource . SetResult ( result ) ;
419+ } ) ;
420+
421+ BridgeConnector . Socket . Emit ( "webContents-executeJavaScript" , Id , code , userGesture ) ;
422+
423+ return taskCompletionSource . Task ;
424+ }
425+
218426 /// <summary>
219427 /// Is used to get the Url of the loaded page.
220428 /// It's usefull if a web-server redirects you and you need to know where it redirects. For instance, It's useful in case of Implicit Authorization.
0 commit comments