diff --git a/src-main/index.js b/src-main/index.js index c33b70f7..c6adf2bb 100644 --- a/src-main/index.js +++ b/src-main/index.js @@ -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' diff --git a/src-main/windows/abstract.js b/src-main/windows/abstract.js index bee12f91..dd2c1fab 100644 --- a/src-main/windows/abstract.js +++ b/src-main/windows/abstract.js @@ -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) { @@ -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()