Skip to content

Commit e4e96bb

Browse files
authored
Merge pull request #819 from NickRimmer/features/webContent-additional-events
WebContent additional events
2 parents 23f4d39 + bf0bdc8 commit e4e96bb

File tree

6 files changed

+264
-1
lines changed

6 files changed

+264
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ElectronNET.API.Entities;
2+
3+
/// <summary>
4+
/// 'OnDidFailLoad' event details.
5+
/// </summary>
6+
public class OnDidFailLoadInfo
7+
{
8+
/// <summary>
9+
/// The full list of error codes and their meaning is available here
10+
/// https://source.chromium.org/chromium/chromium/src/+/main:net/base/net_error_list.h
11+
/// </summary>
12+
public int ErrorCode { get; set; }
13+
14+
/// <summary>
15+
/// Validated URL.
16+
/// </summary>
17+
public string ValidatedUrl { get; set; }
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace ElectronNET.API.Entities;
2+
3+
/// <summary>
4+
/// 'OnDidNavigate' event details.
5+
/// </summary>
6+
public class OnDidNavigateInfo
7+
{
8+
/// <summary>
9+
/// Navigated URL.
10+
/// </summary>
11+
public string Url { get; set; }
12+
13+
/// <summary>
14+
/// HTTP response code.
15+
/// </summary>
16+
public int HttpResponseCode { get; set; }
17+
}

src/ElectronNET.API/WebContents.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,154 @@ public event Action OnDidFinishLoad
8484

8585
private event Action _didFinishLoad;
8686

87+
/// <summary>
88+
/// Emitted when any frame (including main) starts navigating.
89+
/// </summary>
90+
public event Action<string> OnDidStartNavigation
91+
{
92+
add
93+
{
94+
if (_didStartNavigation == null)
95+
{
96+
BridgeConnector.Socket.On<string>("webContents-didStartNavigation" + Id, (url) =>
97+
{
98+
_didStartNavigation(url);
99+
});
100+
101+
BridgeConnector.Socket.Emit("register-webContents-didStartNavigation", Id);
102+
}
103+
_didStartNavigation += value;
104+
}
105+
remove
106+
{
107+
_didStartNavigation -= value;
108+
109+
if (_didStartNavigation == null)
110+
BridgeConnector.Socket.Off("webContents-didStartNavigation" + Id);
111+
}
112+
}
113+
114+
private event Action<string> _didStartNavigation;
115+
116+
/// <summary>
117+
/// Emitted when a main frame navigation is done.
118+
/// 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.
119+
/// </summary>
120+
public event Action<OnDidNavigateInfo> OnDidNavigate
121+
{
122+
add
123+
{
124+
if (_didNavigate == null)
125+
{
126+
BridgeConnector.Socket.On<OnDidNavigateInfo>("webContents-didNavigate" + Id, (data) =>
127+
{
128+
_didNavigate(data);
129+
});
130+
131+
BridgeConnector.Socket.Emit("register-webContents-didNavigate", Id);
132+
}
133+
_didNavigate += value;
134+
}
135+
remove
136+
{
137+
_didNavigate -= value;
138+
139+
if (_didNavigate == null)
140+
BridgeConnector.Socket.Off("webContents-didNavigate" + Id);
141+
}
142+
}
143+
144+
private event Action<OnDidNavigateInfo> _didNavigate;
145+
146+
/// <summary>
147+
/// Emitted when a server side redirect occurs during navigation. For example a 302 redirect.
148+
/// This event will be emitted after OnDidStartNavigation and always before the OnDidRedirectNavigation event for the same navigation.
149+
/// </summary>
150+
public event Action<string> OnWillRedirect
151+
{
152+
add
153+
{
154+
if (_willRedirect == null)
155+
{
156+
BridgeConnector.Socket.On<string>("webContents-willRedirect" + Id, (url) =>
157+
{
158+
_willRedirect(url);
159+
});
160+
161+
BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
162+
}
163+
_willRedirect += value;
164+
}
165+
remove
166+
{
167+
_willRedirect -= value;
168+
169+
if (_willRedirect == null)
170+
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
171+
}
172+
}
173+
174+
private event Action<string> _willRedirect;
175+
176+
/// <summary>
177+
/// Emitted after a server side redirect occurs during navigation. For example a 302 redirect.
178+
/// </summary>
179+
public event Action<string> OnDidRedirectNavigation
180+
{
181+
add
182+
{
183+
if (_didRedirectNavigation == null)
184+
{
185+
BridgeConnector.Socket.On("webContents-didRedirectNavigation" + Id, (url) =>
186+
{
187+
_didRedirectNavigation(url?.ToString());
188+
});
189+
190+
BridgeConnector.Socket.Emit("register-webContents-didRedirectNavigation", Id);
191+
}
192+
_didRedirectNavigation += value;
193+
}
194+
remove
195+
{
196+
_didRedirectNavigation -= value;
197+
198+
if (_didRedirectNavigation == null)
199+
BridgeConnector.Socket.Off("webContents-didRedirectNavigation" + Id);
200+
}
201+
}
202+
203+
private event Action<string> _didRedirectNavigation;
204+
205+
206+
/// <summary>
207+
/// This event is like OnDidFinishLoad but emitted when the load failed.
208+
/// </summary>
209+
public event Action<OnDidFailLoadInfo> OnDidFailLoad
210+
{
211+
add
212+
{
213+
if (_didFailLoad == null)
214+
{
215+
BridgeConnector.Socket.On("webContents-willRedirect" + Id, (data) =>
216+
{
217+
_didFailLoad(((JObject) data).ToObject<OnDidFailLoadInfo>());
218+
});
219+
220+
BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
221+
}
222+
_didFailLoad += value;
223+
}
224+
remove
225+
{
226+
_didFailLoad -= value;
227+
228+
if (_didFailLoad == null)
229+
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
230+
}
231+
}
232+
233+
private event Action<OnDidFailLoadInfo> _didFailLoad;
234+
87235
/// <summary>
88236
/// Emitted when an input event is sent to the WebContents.
89237
/// </summary>

src/ElectronNET.Host/api/webContents.js

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/webContents.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/webContents.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,51 @@ export = (socket: Socket) => {
2424
});
2525
});
2626

27+
socket.on('register-webContents-didStartNavigation', (id) => {
28+
const browserWindow = getWindowById(id);
29+
30+
browserWindow.webContents.removeAllListeners('did-start-navigation');
31+
browserWindow.webContents.on('did-start-navigation', (_, url) => {
32+
electronSocket.emit('webContents-didStartNavigation' + id, url);
33+
});
34+
});
35+
36+
socket.on('register-webContents-didNavigate', (id) => {
37+
const browserWindow = getWindowById(id);
38+
39+
browserWindow.webContents.removeAllListeners('did-navigate');
40+
browserWindow.webContents.on('did-navigate', (_, url, httpResponseCode) => {
41+
electronSocket.emit('webContents-didNavigate' + id, {url, httpResponseCode});
42+
});
43+
});
44+
45+
socket.on('register-webContents-willRedirect', (id) => {
46+
const browserWindow = getWindowById(id);
47+
48+
browserWindow.webContents.removeAllListeners('will-redirect');
49+
browserWindow.webContents.on('will-redirect', (_, url) => {
50+
electronSocket.emit('webContents-willRedirect' + id, url);
51+
});
52+
});
53+
54+
socket.on('register-webContents-didFailLoad', (id) => {
55+
const browserWindow = getWindowById(id);
56+
57+
browserWindow.webContents.removeAllListeners('did-fail-load');
58+
browserWindow.webContents.on('did-fail-load', (_, errorCode, validatedUrl) => {
59+
electronSocket.emit('webContents-didFailLoad' + id, {errorCode, validatedUrl});
60+
});
61+
});
62+
63+
socket.on('register-webContents-didRedirectNavigation', (id) => {
64+
const browserWindow = getWindowById(id);
65+
66+
browserWindow.webContents.removeAllListeners('did-redirect-navigation');
67+
browserWindow.webContents.on('did-redirect-navigation', (_, url) => {
68+
electronSocket.emit('webContents-didRedirectNavigation' + id, url);
69+
});
70+
});
71+
2772
socket.on('register-webContents-input-event', (id) => {
2873
const browserWindow = getWindowById(id);
2974

0 commit comments

Comments
 (0)