From 24c9cbcc0a6feaf45de70233a75fb7c2bdc69149 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 8 Aug 2023 12:35:59 +0200 Subject: [PATCH] docs: handle opening links in the default browser in main.js (#39379) docs: handle opening links in the default browser in the main process --- docs/fiddles/menus/customize-menus/main.js | 6 ++++++ docs/fiddles/menus/customize-menus/renderer.js | 14 +------------- docs/fiddles/menus/shortcuts/index.html | 4 ---- docs/fiddles/menus/shortcuts/main.js | 8 +++++++- docs/fiddles/menus/shortcuts/renderer.js | 13 ------------- .../fiddles/native-ui/dialogs/error-dialog/main.js | 8 +++++++- .../native-ui/dialogs/error-dialog/renderer.js | 13 +------------ .../native-ui/dialogs/information-dialog/main.js | 8 +++++++- .../dialogs/information-dialog/renderer.js | 13 +------------ .../dialogs/open-file-or-directory/main.js | 8 +++++++- .../dialogs/open-file-or-directory/renderer.js | 13 +------------ docs/fiddles/native-ui/dialogs/save-dialog/main.js | 8 +++++++- .../native-ui/dialogs/save-dialog/renderer.js | 13 +------------ docs/fiddles/native-ui/drag-and-drop/main.js | 8 +++++++- docs/fiddles/native-ui/drag-and-drop/renderer.js | 13 ------------- .../native-ui/external-links-file-manager/main.js | 8 +++++++- .../external-links-file-manager/renderer.js | 12 ------------ docs/fiddles/native-ui/notifications/index.html | 2 +- docs/fiddles/native-ui/notifications/main.js | 8 +++++++- docs/fiddles/native-ui/notifications/renderer.js | 14 -------------- .../get-version-information/main.js | 8 +++++++- .../get-version-information/renderer.js | 14 -------------- .../manage-windows/frameless-window/main.js | 8 +++++++- .../manage-windows/frameless-window/renderer.js | 14 +------------- .../manage-windows/manage-window-state/main.js | 8 +++++++- .../manage-windows/manage-window-state/renderer.js | 14 +------------- .../windows/manage-windows/window-events/main.js | 8 +++++++- .../manage-windows/window-events/renderer.js | 14 +------------- 28 files changed, 99 insertions(+), 183 deletions(-) delete mode 100644 docs/fiddles/menus/shortcuts/renderer.js diff --git a/docs/fiddles/menus/customize-menus/main.js b/docs/fiddles/menus/customize-menus/main.js index b55adcc283ec..74a700895481 100644 --- a/docs/fiddles/menus/customize-menus/main.js +++ b/docs/fiddles/menus/customize-menus/main.js @@ -312,6 +312,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/menus/customize-menus/renderer.js b/docs/fiddles/menus/customize-menus/renderer.js index 24df360514c9..5527e1f20008 100644 --- a/docs/fiddles/menus/customize-menus/renderer.js +++ b/docs/fiddles/menus/customize-menus/renderer.js @@ -1,4 +1,4 @@ -const { ipcRenderer, shell } = require('electron') +const { ipcRenderer } = require('electron') // Tell main process to show the menu when demo button is clicked const contextMenuBtn = document.getElementById('context-menu') @@ -6,15 +6,3 @@ const contextMenuBtn = document.getElementById('context-menu') contextMenuBtn.addEventListener('click', () => { ipcRenderer.send('show-context-menu') }) - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/menus/shortcuts/index.html b/docs/fiddles/menus/shortcuts/index.html index 3f65322c75ec..6ab24f828bdf 100644 --- a/docs/fiddles/menus/shortcuts/index.html +++ b/docs/fiddles/menus/shortcuts/index.html @@ -68,10 +68,6 @@ - diff --git a/docs/fiddles/menus/shortcuts/main.js b/docs/fiddles/menus/shortcuts/main.js index f17dced4f656..ff51f59a9ada 100644 --- a/docs/fiddles/menus/shortcuts/main.js +++ b/docs/fiddles/menus/shortcuts/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, globalShortcut, dialog } = require('electron') +const { app, BrowserWindow, globalShortcut, dialog, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -38,6 +38,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/menus/shortcuts/renderer.js b/docs/fiddles/menus/shortcuts/renderer.js deleted file mode 100644 index b403b34e38c2..000000000000 --- a/docs/fiddles/menus/shortcuts/renderer.js +++ /dev/null @@ -1,13 +0,0 @@ -const { shell } = require('electron') - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/native-ui/dialogs/error-dialog/main.js b/docs/fiddles/native-ui/dialogs/error-dialog/main.js index cf33afc3dca3..1e26daacd8d8 100644 --- a/docs/fiddles/native-ui/dialogs/error-dialog/main.js +++ b/docs/fiddles/native-ui/dialogs/error-dialog/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain, dialog } = require('electron') +const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -29,6 +29,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/dialogs/error-dialog/renderer.js b/docs/fiddles/native-ui/dialogs/error-dialog/renderer.js index 94e360ea9a94..e355e7ee9a90 100644 --- a/docs/fiddles/native-ui/dialogs/error-dialog/renderer.js +++ b/docs/fiddles/native-ui/dialogs/error-dialog/renderer.js @@ -1,18 +1,7 @@ -const { ipcRenderer, shell } = require('electron') +const { ipcRenderer } = require('electron') -const links = document.querySelectorAll('a[href]') const errorBtn = document.getElementById('error-dialog') errorBtn.addEventListener('click', event => { ipcRenderer.send('open-error-dialog') }) - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/native-ui/dialogs/information-dialog/main.js b/docs/fiddles/native-ui/dialogs/information-dialog/main.js index 11b23c08acb8..187abcfce7b1 100644 --- a/docs/fiddles/native-ui/dialogs/information-dialog/main.js +++ b/docs/fiddles/native-ui/dialogs/information-dialog/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain, dialog } = require('electron') +const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -29,6 +29,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/dialogs/information-dialog/renderer.js b/docs/fiddles/native-ui/dialogs/information-dialog/renderer.js index 1f437c8556f4..108d8e324102 100644 --- a/docs/fiddles/native-ui/dialogs/information-dialog/renderer.js +++ b/docs/fiddles/native-ui/dialogs/information-dialog/renderer.js @@ -1,7 +1,6 @@ -const { ipcRenderer, shell } = require('electron') +const { ipcRenderer } = require('electron') const informationBtn = document.getElementById('information-dialog') -const links = document.querySelectorAll('a[href]') informationBtn.addEventListener('click', event => { ipcRenderer.send('open-information-dialog') @@ -13,13 +12,3 @@ ipcRenderer.on('information-dialog-selection', (event, index) => { else message += 'no.' document.getElementById('info-selection').innerHTML = message }) - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/native-ui/dialogs/open-file-or-directory/main.js b/docs/fiddles/native-ui/dialogs/open-file-or-directory/main.js index 85bee267b374..b3f87183a7f8 100644 --- a/docs/fiddles/native-ui/dialogs/open-file-or-directory/main.js +++ b/docs/fiddles/native-ui/dialogs/open-file-or-directory/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain, dialog } = require('electron') +const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -29,6 +29,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/dialogs/open-file-or-directory/renderer.js b/docs/fiddles/native-ui/dialogs/open-file-or-directory/renderer.js index 2de0b1d64744..08333444dca3 100644 --- a/docs/fiddles/native-ui/dialogs/open-file-or-directory/renderer.js +++ b/docs/fiddles/native-ui/dialogs/open-file-or-directory/renderer.js @@ -1,7 +1,6 @@ -const { ipcRenderer, shell } = require('electron') +const { ipcRenderer } = require('electron') const selectDirBtn = document.getElementById('select-directory') -const links = document.querySelectorAll('a[href]') selectDirBtn.addEventListener('click', event => { ipcRenderer.send('open-file-dialog') @@ -10,13 +9,3 @@ selectDirBtn.addEventListener('click', event => { ipcRenderer.on('selected-directory', (event, path) => { document.getElementById('selected-file').innerHTML = `You selected: ${path}` }) - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/native-ui/dialogs/save-dialog/main.js b/docs/fiddles/native-ui/dialogs/save-dialog/main.js index cf98f9e9c5c2..b522f27ed6ba 100644 --- a/docs/fiddles/native-ui/dialogs/save-dialog/main.js +++ b/docs/fiddles/native-ui/dialogs/save-dialog/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain, dialog } = require('electron') +const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -29,6 +29,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/dialogs/save-dialog/renderer.js b/docs/fiddles/native-ui/dialogs/save-dialog/renderer.js index c51f9a8da9ba..075b02e17b59 100644 --- a/docs/fiddles/native-ui/dialogs/save-dialog/renderer.js +++ b/docs/fiddles/native-ui/dialogs/save-dialog/renderer.js @@ -1,7 +1,6 @@ -const { ipcRenderer, shell } = require('electron') +const { ipcRenderer } = require('electron') const saveBtn = document.getElementById('save-dialog') -const links = document.querySelectorAll('a[href]') saveBtn.addEventListener('click', event => { ipcRenderer.send('save-dialog') @@ -11,13 +10,3 @@ ipcRenderer.on('saved-file', (event, path) => { if (!path) path = 'No path' document.getElementById('file-saved').innerHTML = `Path selected: ${path}` }) - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/native-ui/drag-and-drop/main.js b/docs/fiddles/native-ui/drag-and-drop/main.js index 7767495abbd9..1137ef176a96 100644 --- a/docs/fiddles/native-ui/drag-and-drop/main.js +++ b/docs/fiddles/native-ui/drag-and-drop/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain, nativeImage } = require('electron') +const { app, BrowserWindow, ipcMain, nativeImage, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow @@ -28,6 +28,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/drag-and-drop/renderer.js b/docs/fiddles/native-ui/drag-and-drop/renderer.js index 621eea3a6bb2..859348551e76 100644 --- a/docs/fiddles/native-ui/drag-and-drop/renderer.js +++ b/docs/fiddles/native-ui/drag-and-drop/renderer.js @@ -1,17 +1,4 @@ const { ipcRenderer } = require('electron') -const shell = require('electron').shell - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} const dragFileLink = document.getElementById('drag-file-link') diff --git a/docs/fiddles/native-ui/external-links-file-manager/main.js b/docs/fiddles/native-ui/external-links-file-manager/main.js index a9a5c558ea59..a2de1b97c897 100644 --- a/docs/fiddles/native-ui/external-links-file-manager/main.js +++ b/docs/fiddles/native-ui/external-links-file-manager/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -29,6 +29,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/external-links-file-manager/renderer.js b/docs/fiddles/native-ui/external-links-file-manager/renderer.js index e557679162bb..2a059092c98f 100644 --- a/docs/fiddles/native-ui/external-links-file-manager/renderer.js +++ b/docs/fiddles/native-ui/external-links-file-manager/renderer.js @@ -11,15 +11,3 @@ fileManagerBtn.addEventListener('click', (event) => { exLinksBtn.addEventListener('click', (event) => { shell.openExternal('https://electronjs.org') }) - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/native-ui/notifications/index.html b/docs/fiddles/native-ui/notifications/index.html index 60d43d194853..b7d86f23aa89 100644 --- a/docs/fiddles/native-ui/notifications/index.html +++ b/docs/fiddles/native-ui/notifications/index.html @@ -26,7 +26,7 @@

Open the - + full API documentation(opens in new window) in your browser. diff --git a/docs/fiddles/native-ui/notifications/main.js b/docs/fiddles/native-ui/notifications/main.js index a9a5c558ea59..a2de1b97c897 100644 --- a/docs/fiddles/native-ui/notifications/main.js +++ b/docs/fiddles/native-ui/notifications/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow, shell } = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -29,6 +29,12 @@ function createWindow () { // when you should delete the corresponding element. mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/native-ui/notifications/renderer.js b/docs/fiddles/native-ui/notifications/renderer.js index e7b2ca60bf3f..9a97f7a869e0 100644 --- a/docs/fiddles/native-ui/notifications/renderer.js +++ b/docs/fiddles/native-ui/notifications/renderer.js @@ -1,5 +1,3 @@ -const { shell } = require('electron') - const basicNotification = { title: 'Basic Notification', body: 'Short message part' @@ -29,15 +27,3 @@ basicNotificationButton.addEventListener('click', () => { console.log('Notification clicked') } }) - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/system/system-information/get-version-information/main.js b/docs/fiddles/system/system-information/get-version-information/main.js index 8d9751b4ef96..34bdd9e32b3b 100644 --- a/docs/fiddles/system/system-information/get-version-information/main.js +++ b/docs/fiddles/system/system-information/get-version-information/main.js @@ -1,4 +1,4 @@ -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow, shell } = require('electron') let mainWindow = null @@ -19,6 +19,12 @@ function createWindow () { mainWindow.on('closed', () => { mainWindow = null }) + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } app.whenReady().then(() => { diff --git a/docs/fiddles/system/system-information/get-version-information/renderer.js b/docs/fiddles/system/system-information/get-version-information/renderer.js index 714d52f172fa..40f7f2cf2cf6 100644 --- a/docs/fiddles/system/system-information/get-version-information/renderer.js +++ b/docs/fiddles/system/system-information/get-version-information/renderer.js @@ -1,5 +1,3 @@ -const { shell } = require('electron') - const versionInfoBtn = document.getElementById('version-info') const electronVersion = process.versions.electron @@ -8,15 +6,3 @@ versionInfoBtn.addEventListener('click', () => { const message = `This app is using Electron version: ${electronVersion}` document.getElementById('got-version-info').innerHTML = message }) - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/windows/manage-windows/frameless-window/main.js b/docs/fiddles/windows/manage-windows/frameless-window/main.js index eb9d8d7b5559..b60f99d22490 100644 --- a/docs/fiddles/windows/manage-windows/frameless-window/main.js +++ b/docs/fiddles/windows/manage-windows/frameless-window/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain } = require('electron') +const { app, BrowserWindow, ipcMain, shell } = require('electron') ipcMain.on('create-frameless-window', (event, { url }) => { const win = new BrowserWindow({ frame: false }) @@ -19,6 +19,12 @@ function createWindow () { // and load the index.html of the app. mainWindow.loadFile('index.html') + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/windows/manage-windows/frameless-window/renderer.js b/docs/fiddles/windows/manage-windows/frameless-window/renderer.js index d00294d01011..21f91ad561b3 100644 --- a/docs/fiddles/windows/manage-windows/frameless-window/renderer.js +++ b/docs/fiddles/windows/manage-windows/frameless-window/renderer.js @@ -1,4 +1,4 @@ -const { ipcRenderer, shell } = require('electron') +const { ipcRenderer } = require('electron') const newWindowBtn = document.getElementById('frameless-window') @@ -6,15 +6,3 @@ newWindowBtn.addEventListener('click', () => { const url = 'data:text/html,

Hello World!

Close this Window' ipcRenderer.send('create-frameless-window', { url }) }) - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/windows/manage-windows/manage-window-state/main.js b/docs/fiddles/windows/manage-windows/manage-window-state/main.js index caef426acbb2..05fcdd704e16 100644 --- a/docs/fiddles/windows/manage-windows/manage-window-state/main.js +++ b/docs/fiddles/windows/manage-windows/manage-window-state/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain } = require('electron') +const { app, BrowserWindow, ipcMain, shell } = require('electron') ipcMain.on('create-demo-window', (event) => { const win = new BrowserWindow({ width: 400, height: 275 }) @@ -29,6 +29,12 @@ function createWindow () { // and load the index.html of the app. mainWindow.loadFile('index.html') + + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) } // This method will be called when Electron has finished diff --git a/docs/fiddles/windows/manage-windows/manage-window-state/renderer.js b/docs/fiddles/windows/manage-windows/manage-window-state/renderer.js index 7071f50f2d4a..bdf6a54c1795 100644 --- a/docs/fiddles/windows/manage-windows/manage-window-state/renderer.js +++ b/docs/fiddles/windows/manage-windows/manage-window-state/renderer.js @@ -1,9 +1,7 @@ -const { shell, ipcRenderer } = require('electron') +const { ipcRenderer } = require('electron') const manageWindowBtn = document.getElementById('manage-window') -const links = document.querySelectorAll('a[href]') - ipcRenderer.on('bounds-changed', (event, bounds) => { const manageWindowReply = document.getElementById('manage-window-reply') const message = `Size: ${bounds.size} Position: ${bounds.position}` @@ -13,13 +11,3 @@ ipcRenderer.on('bounds-changed', (event, bounds) => { manageWindowBtn.addEventListener('click', (event) => { ipcRenderer.send('create-demo-window') }) - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -} diff --git a/docs/fiddles/windows/manage-windows/window-events/main.js b/docs/fiddles/windows/manage-windows/window-events/main.js index d9eac7471442..5abf2cc257cc 100644 --- a/docs/fiddles/windows/manage-windows/window-events/main.js +++ b/docs/fiddles/windows/manage-windows/window-events/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow, ipcMain } = require('electron') +const { app, BrowserWindow, ipcMain, shell } = require('electron') function createWindow () { // Create the browser window. @@ -15,6 +15,12 @@ function createWindow () { // and load the index.html of the app. mainWindow.loadFile('index.html') + // Open external links in the default browser + mainWindow.webContents.on('will-navigate', (event, url) => { + event.preventDefault() + shell.openExternal(url) + }) + let demoWindow ipcMain.on('show-demo-window', () => { if (demoWindow) { diff --git a/docs/fiddles/windows/manage-windows/window-events/renderer.js b/docs/fiddles/windows/manage-windows/window-events/renderer.js index 70218b7df22a..85460df9373b 100644 --- a/docs/fiddles/windows/manage-windows/window-events/renderer.js +++ b/docs/fiddles/windows/manage-windows/window-events/renderer.js @@ -1,4 +1,4 @@ -const { shell, ipcRenderer } = require('electron') +const { ipcRenderer } = require('electron') const listenToWindowBtn = document.getElementById('listen-to-window') const focusModalBtn = document.getElementById('focus-on-modal-window') @@ -25,15 +25,3 @@ ipcRenderer.on('window-blur', showFocusBtn) listenToWindowBtn.addEventListener('click', () => { ipcRenderer.send('show-demo-window') }) - -const links = document.querySelectorAll('a[href]') - -for (const link of links) { - const url = link.getAttribute('href') - if (url.indexOf('http') === 0) { - link.addEventListener('click', (e) => { - e.preventDefault() - shell.openExternal(url) - }) - } -}