From 73a3e331dc670c20ba24bac3010d798e96bee234 Mon Sep 17 00:00:00 2001 From: softworkz Date: Sun, 24 Sep 2023 16:36:49 +0200 Subject: [PATCH] Add executeJavaScript method to WebContents --- src/ElectronNET.API/WebContents.cs | 31 +++++++++++++++++++++++++ src/ElectronNET.Host/api/webContents.js | 6 +++++ src/ElectronNET.Host/api/webContents.ts | 5 ++++ 3 files changed, 42 insertions(+) diff --git a/src/ElectronNET.API/WebContents.cs b/src/ElectronNET.API/WebContents.cs index f77367c..187ba6e 100644 --- a/src/ElectronNET.API/WebContents.cs +++ b/src/ElectronNET.API/WebContents.cs @@ -244,6 +244,37 @@ public class WebContents return taskCompletionSource.Task; } + /// + /// Evaluates script code in page. + /// + /// The code to execute. + /// if set to true simulate a user gesture. + /// The result of the executed code. + /// + /// + /// In the browser window some HTML APIs like `requestFullScreen` can only be + /// invoked by a gesture from the user. Setting `userGesture` to `true` will remove + /// this limitation. + /// + /// + /// Code execution will be suspended until web page stop loading. + /// + /// + public Task ExecuteJavaScriptAsync(string code, bool userGesture = false) + { + var taskCompletionSource = new TaskCompletionSource(); + + BridgeConnector.Socket.On("webContents-executeJavaScript-completed", (result) => + { + BridgeConnector.Socket.Off("webContents-executeJavaScript-completed"); + taskCompletionSource.SetResult(result); + }); + + BridgeConnector.Socket.Emit("webContents-executeJavaScript", Id, code, userGesture); + + return taskCompletionSource.Task; + } + /// /// Is used to get the Url of the loaded page. /// 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. diff --git a/src/ElectronNET.Host/api/webContents.js b/src/ElectronNET.Host/api/webContents.js index cef8980..777d985 100644 --- a/src/ElectronNET.Host/api/webContents.js +++ b/src/ElectronNET.Host/api/webContents.js @@ -65,6 +65,12 @@ module.exports = (socket) => { } }); }); + + socket.on('webContents-executeJavaScript', async (id, code, userGesture = false) => { + const result = await getWindowById(id).webContents.executeJavaScript(code, userGesture); + electronSocket.emit('webContents-executeJavaScript-completed', result); + }); + socket.on('webContents-getUrl', function (id) { const browserWindow = getWindowById(id); electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL()); diff --git a/src/ElectronNET.Host/api/webContents.ts b/src/ElectronNET.Host/api/webContents.ts index 3f4b7af..9e0d204 100644 --- a/src/ElectronNET.Host/api/webContents.ts +++ b/src/ElectronNET.Host/api/webContents.ts @@ -74,6 +74,11 @@ export = (socket: Socket) => { }); }); + socket.on('webContents-executeJavaScript', async (id, code, userGesture = false) => { + const result = await getWindowById(id).webContents.executeJavaScript(code, userGesture); + electronSocket.emit('webContents-executeJavaScript-completed', result); + }); + socket.on('webContents-getUrl', function (id) { const browserWindow = getWindowById(id); electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());