Skip to content

Commit

Permalink
Refactor web contents will-navigate handling
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Jun 22, 2024
1 parent 5096caa commit 1084de2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ app.on('session-created', (session) => {
});

app.on('web-contents-created', (event, webContents) => {
webContents.on('will-navigate', (event, url) => {
// Only allow windows to refresh, not navigate anywhere.
const window = AbstractWindow.getWindowByWebContents(webContents);
if (!window || url !== window.initialURL) {
event.preventDefault();
openExternal(url);
}
});

// Overwritten by AbstractWindow. We just set this here as a safety measure.
webContents.setWindowOpenHandler((details) => ({
action: 'deny'
Expand Down
13 changes: 13 additions & 0 deletions src-main/windows/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AbstractWindow {
this.window = options.existingWindow || new BrowserWindow(this.getWindowOptions());
this.window.webContents.setWindowOpenHandler(this.handleWindowOpen.bind(this));
this.window.webContents.on('before-input-event', this.handleInput.bind(this));
this.window.webContents.on('will-navigate', this.handleWillNavigate.bind(this));
this.applySettings();

if (!options.existingWindow) {
Expand Down Expand Up @@ -286,6 +287,18 @@ class AbstractWindow {
}
}

/**
* @param {Electron.WillNavigateEvent} event
* @param {string} url
*/
handleWillNavigate (event, url) {
// Only allow windows to refresh, not navigate anywhere.
if (url !== this.initialURL) {
event.preventDefault();
openExternal(url);
}
}

reload () {
// Don't use webContents.reload() because it allows the page to navigate by using
// history.pushState() then location.reload()
Expand Down

0 comments on commit 1084de2

Please sign in to comment.