Skip to content

Commit df3bd12

Browse files
authored
Merge pull request #813 from softworkz/submit_execjs_domready
WebContents: add executeJavaScript and dom-ready event
2 parents 2b54353 + 73a3e33 commit df3bd12

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/ElectronNET.API/WebContents.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,35 @@ public event Action<InputEvent> InputEvent
262262

263263
private event Action<InputEvent> _inputEvent;
264264

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+
265294
internal WebContents(int id)
266295
{
267296
Id = id;
@@ -363,6 +392,37 @@ public Task<bool> PrintToPDFAsync(string path, PrintToPDFOptions options = null)
363392
return taskCompletionSource.Task;
364393
}
365394

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+
366426
/// <summary>
367427
/// Is used to get the Url of the loaded page.
368428
/// 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.

src/ElectronNET.Host/api/webContents.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/webContents.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ export = (socket: Socket) => {
8080
});
8181
});
8282

83+
socket.on('register-webContents-domReady', (id) => {
84+
const browserWindow = getWindowById(id);
85+
86+
browserWindow.webContents.removeAllListeners('dom-ready');
87+
browserWindow.webContents.on('dom-ready', () => {
88+
electronSocket.emit('webContents-domReady' + id);
89+
});
90+
});
91+
8392
socket.on('webContentsOpenDevTools', (id, options) => {
8493
if (options) {
8594
getWindowById(id).webContents.openDevTools(options);
@@ -110,6 +119,11 @@ export = (socket: Socket) => {
110119
});
111120
});
112121

122+
socket.on('webContents-executeJavaScript', async (id, code, userGesture = false) => {
123+
const result = await getWindowById(id).webContents.executeJavaScript(code, userGesture);
124+
electronSocket.emit('webContents-executeJavaScript-completed', result);
125+
});
126+
113127
socket.on('webContents-getUrl', function (id) {
114128
const browserWindow = getWindowById(id);
115129
electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy