Skip to content

WebContent additional events #819

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
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
18 changes: 18 additions & 0 deletions src/ElectronNET.API/Entities/OnDidFailLoadInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ElectronNET.API.Entities;

/// <summary>
/// 'OnDidFailLoad' event details.
/// </summary>
public class OnDidFailLoadInfo
{
/// <summary>
/// The full list of error codes and their meaning is available here
/// https://source.chromium.org/chromium/chromium/src/+/main:net/base/net_error_list.h
/// </summary>
public int ErrorCode { get; set; }

/// <summary>
/// Validated URL.
/// </summary>
public string ValidatedUrl { get; set; }
}
17 changes: 17 additions & 0 deletions src/ElectronNET.API/Entities/OnDidNavigateInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ElectronNET.API.Entities;

/// <summary>
/// 'OnDidNavigate' event details.
/// </summary>
public class OnDidNavigateInfo
{
/// <summary>
/// Navigated URL.
/// </summary>
public string Url { get; set; }

/// <summary>
/// HTTP response code.
/// </summary>
public int HttpResponseCode { get; set; }
}
148 changes: 148 additions & 0 deletions src/ElectronNET.API/WebContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,154 @@ public event Action OnDidFinishLoad

private event Action _didFinishLoad;

/// <summary>
/// Emitted when any frame (including main) starts navigating.
/// </summary>
public event Action<string> OnDidStartNavigation
{
add
{
if (_didStartNavigation == null)
{
BridgeConnector.Socket.On<string>("webContents-didStartNavigation" + Id, (url) =>
{
_didStartNavigation(url);
});

BridgeConnector.Socket.Emit("register-webContents-didStartNavigation", Id);
}
_didStartNavigation += value;
}
remove
{
_didStartNavigation -= value;

if (_didStartNavigation == null)
BridgeConnector.Socket.Off("webContents-didStartNavigation" + Id);
}
}

private event Action<string> _didStartNavigation;

/// <summary>
/// Emitted when a main frame navigation is done.
/// 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.
/// </summary>
public event Action<OnDidNavigateInfo> OnDidNavigate
{
add
{
if (_didNavigate == null)
{
BridgeConnector.Socket.On<OnDidNavigateInfo>("webContents-didNavigate" + Id, (data) =>
{
_didNavigate(data);
});

BridgeConnector.Socket.Emit("register-webContents-didNavigate", Id);
}
_didNavigate += value;
}
remove
{
_didNavigate -= value;

if (_didNavigate == null)
BridgeConnector.Socket.Off("webContents-didNavigate" + Id);
}
}

private event Action<OnDidNavigateInfo> _didNavigate;

/// <summary>
/// Emitted when a server side redirect occurs during navigation. For example a 302 redirect.
/// This event will be emitted after OnDidStartNavigation and always before the OnDidRedirectNavigation event for the same navigation.
/// </summary>
public event Action<string> OnWillRedirect
{
add
{
if (_willRedirect == null)
{
BridgeConnector.Socket.On<string>("webContents-willRedirect" + Id, (url) =>
{
_willRedirect(url);
});

BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
}
_willRedirect += value;
}
remove
{
_willRedirect -= value;

if (_willRedirect == null)
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
}
}

private event Action<string> _willRedirect;

/// <summary>
/// Emitted after a server side redirect occurs during navigation. For example a 302 redirect.
/// </summary>
public event Action<string> OnDidRedirectNavigation
{
add
{
if (_didRedirectNavigation == null)
{
BridgeConnector.Socket.On("webContents-didRedirectNavigation" + Id, (url) =>
{
_didRedirectNavigation(url?.ToString());
});

BridgeConnector.Socket.Emit("register-webContents-didRedirectNavigation", Id);
}
_didRedirectNavigation += value;
}
remove
{
_didRedirectNavigation -= value;

if (_didRedirectNavigation == null)
BridgeConnector.Socket.Off("webContents-didRedirectNavigation" + Id);
}
}

private event Action<string> _didRedirectNavigation;


/// <summary>
/// This event is like OnDidFinishLoad but emitted when the load failed.
/// </summary>
public event Action<OnDidFailLoadInfo> OnDidFailLoad
{
add
{
if (_didFailLoad == null)
{
BridgeConnector.Socket.On("webContents-willRedirect" + Id, (data) =>
{
_didFailLoad(((JObject) data).ToObject<OnDidFailLoadInfo>());
});

BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
}
_didFailLoad += value;
}
remove
{
_didFailLoad -= value;

if (_didFailLoad == null)
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
}
}

private event Action<OnDidFailLoadInfo> _didFailLoad;

/// <summary>
/// Emitted when an input event is sent to the WebContents.
/// </summary>
Expand Down
35 changes: 35 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.

2 changes: 1 addition & 1 deletion src/ElectronNET.Host/api/webContents.js.map

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions src/ElectronNET.Host/api/webContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ export = (socket: Socket) => {
});
});

socket.on('register-webContents-didStartNavigation', (id) => {
const browserWindow = getWindowById(id);

browserWindow.webContents.removeAllListeners('did-start-navigation');
browserWindow.webContents.on('did-start-navigation', (_, url) => {
electronSocket.emit('webContents-didStartNavigation' + id, url);
});
});

socket.on('register-webContents-didNavigate', (id) => {
const browserWindow = getWindowById(id);

browserWindow.webContents.removeAllListeners('did-navigate');
browserWindow.webContents.on('did-navigate', (_, url, httpResponseCode) => {
electronSocket.emit('webContents-didNavigate' + id, {url, httpResponseCode});
});
});

socket.on('register-webContents-willRedirect', (id) => {
const browserWindow = getWindowById(id);

browserWindow.webContents.removeAllListeners('will-redirect');
browserWindow.webContents.on('will-redirect', (_, url) => {
electronSocket.emit('webContents-willRedirect' + id, url);
});
});

socket.on('register-webContents-didFailLoad', (id) => {
const browserWindow = getWindowById(id);

browserWindow.webContents.removeAllListeners('did-fail-load');
browserWindow.webContents.on('did-fail-load', (_, errorCode, validatedUrl) => {
electronSocket.emit('webContents-didFailLoad' + id, {errorCode, validatedUrl});
});
});

socket.on('register-webContents-didRedirectNavigation', (id) => {
const browserWindow = getWindowById(id);

browserWindow.webContents.removeAllListeners('did-redirect-navigation');
browserWindow.webContents.on('did-redirect-navigation', (_, url) => {
electronSocket.emit('webContents-didRedirectNavigation' + id, url);
});
});

socket.on('register-webContents-input-event', (id) => {
const browserWindow = getWindowById(id);

Expand Down