docs: handle opening links in the default browser in main.js (#39379)

docs: handle opening links in the default browser in the main process
This commit is contained in:
Milan Burda 2023-08-08 12:35:59 +02:00 committed by GitHub
parent eecfaec8c9
commit 24c9cbcc0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 99 additions and 183 deletions

View file

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

View file

@ -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,<h2>Hello World!</h2><a id="close" href="javascript:window.close()">Close this Window</a>'
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)
})
}
}

View file

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

View file

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

View file

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

View file

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