diff --git a/src/ElectronNET.API/WebContents.cs b/src/ElectronNET.API/WebContents.cs index cf3c0eb9..187ba6e0 100644 --- a/src/ElectronNET.API/WebContents.cs +++ b/src/ElectronNET.API/WebContents.cs @@ -114,6 +114,35 @@ public event Action InputEvent private event Action _inputEvent; + /// + /// Emitted when the document in the top-level frame is loaded. + /// + public event Action OnDomReady + { + add + { + if (_domReady == null) + { + BridgeConnector.Socket.On("webContents-domReady" + Id, () => + { + _domReady(); + }); + + BridgeConnector.Socket.Emit("register-webContents-domReady", Id); + } + _domReady += value; + } + remove + { + _domReady -= value; + + if (_domReady == null) + BridgeConnector.Socket.Off("webContents-domReady" + Id); + } + } + + private event Action _domReady; + internal WebContents(int id) { Id = id; @@ -215,6 +244,37 @@ public Task PrintToPDFAsync(string path, PrintToPDFOptions options = null) 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 8df97114..777d985e 100644 --- a/src/ElectronNET.Host/api/webContents.js +++ b/src/ElectronNET.Host/api/webContents.js @@ -28,6 +28,16 @@ module.exports = (socket) => { } }); }); + + socket.on('register-webContents-domReady', (id) => { + const browserWindow = getWindowById(id); + + browserWindow.webContents.removeAllListeners('dom-ready'); + browserWindow.webContents.on('dom-ready', () => { + electronSocket.emit('webContents-domReady' + id); + }); + }); + socket.on('webContentsOpenDevTools', (id, options) => { if (options) { getWindowById(id).webContents.openDevTools(options); @@ -55,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 4bf6fddf..9e0d2041 100644 --- a/src/ElectronNET.Host/api/webContents.ts +++ b/src/ElectronNET.Host/api/webContents.ts @@ -35,6 +35,15 @@ export = (socket: Socket) => { }); }); + socket.on('register-webContents-domReady', (id) => { + const browserWindow = getWindowById(id); + + browserWindow.webContents.removeAllListeners('dom-ready'); + browserWindow.webContents.on('dom-ready', () => { + electronSocket.emit('webContents-domReady' + id); + }); + }); + socket.on('webContentsOpenDevTools', (id, options) => { if (options) { getWindowById(id).webContents.openDevTools(options); @@ -65,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());