Skip to content

WebContents: add executeJavaScript and dom-ready event #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/ElectronNET.API/WebContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ public event Action<InputEvent> InputEvent

private event Action<InputEvent> _inputEvent;

/// <summary>
/// Emitted when the document in the top-level frame is loaded.
/// </summary>
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;
Expand Down Expand Up @@ -215,6 +244,37 @@ public Task<bool> PrintToPDFAsync(string path, PrintToPDFOptions options = null)
return taskCompletionSource.Task;
}

/// <summary>
/// Evaluates script code in page.
/// </summary>
/// <param name="code">The code to execute.</param>
/// <param name="userGesture">if set to <c>true</c> simulate a user gesture.</param>
/// <returns>The result of the executed code.</returns>
/// <remarks>
/// <para>
/// 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.
/// </para>
/// <para>
/// Code execution will be suspended until web page stop loading.
/// </para>
/// </remarks>
public Task<object> ExecuteJavaScriptAsync(string code, bool userGesture = false)
{
var taskCompletionSource = new TaskCompletionSource<object>();

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;
}

/// <summary>
/// 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.
Expand Down
16 changes: 16 additions & 0 deletions src/ElectronNET.Host/api/webContents.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/ElectronNET.Host/api/webContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down