did-start-navigation, did-navigate, will-redirect, did-fail-load, did-redirect-navigation events added for WebContents

This commit is contained in:
Nick Rimmer
2023-11-03 00:23:37 +01:00
parent 23f4d39a30
commit bf0bdc8386
6 changed files with 264 additions and 1 deletions

View File

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

View File

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

View File

@@ -84,6 +84,154 @@ public class WebContents
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>

View File

@@ -19,6 +19,41 @@ module.exports = (socket) => {
electronSocket.emit('webContents-didFinishLoad' + id);
});
});
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);
browserWindow.webContents.removeAllListeners('input-event');

File diff suppressed because one or more lines are too long

View File

@@ -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);