Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit 08bbff5

Browse files
authored
docs: handle opening links in the default browser in main.js (part #2) (electron#39473)
docs: handle opening links in the default browser in main.js
1 parent 117d531 commit 08bbff5

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1-
const { app, ipcMain } = require('electron')
1+
const { app, BrowserWindow, ipcMain, shell } = require('electron')
22

3-
ipcMain.on('get-app-path', (event) => {
4-
event.sender.send('got-app-path', app.getAppPath())
3+
let mainWindow = null
4+
5+
ipcMain.handle('get-app-path', (event) => app.getAppPath())
6+
7+
function createWindow () {
8+
const windowOptions = {
9+
width: 600,
10+
height: 400,
11+
title: 'Get app information',
12+
webPreferences: {
13+
contextIsolation: false,
14+
nodeIntegration: true
15+
}
16+
}
17+
18+
mainWindow = new BrowserWindow(windowOptions)
19+
mainWindow.loadFile('index.html')
20+
21+
mainWindow.on('closed', () => {
22+
mainWindow = null
23+
})
24+
25+
// Open external links in the default browser
26+
mainWindow.webContents.on('will-navigate', (event, url) => {
27+
event.preventDefault()
28+
shell.openExternal(url)
29+
})
30+
}
31+
32+
app.whenReady().then(() => {
33+
createWindow()
534
})
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
const { ipcRenderer, shell } = require('electron')
1+
const { ipcRenderer } = require('electron')
22

33
const appInfoBtn = document.getElementById('app-info')
4-
const electronDocLink = document.querySelectorAll('a[href]')
54

6-
appInfoBtn.addEventListener('click', () => {
7-
ipcRenderer.send('get-app-path')
8-
})
9-
10-
ipcRenderer.on('got-app-path', (event, path) => {
5+
appInfoBtn.addEventListener('click', async () => {
6+
const path = await ipcRenderer.invoke('get-app-path')
117
const message = `This app is located at: ${path}`
128
document.getElementById('got-app-info').innerHTML = message
139
})
14-
15-
electronDocLink.addEventListener('click', (e) => {
16-
e.preventDefault()
17-
const url = e.target.getAttribute('href')
18-
shell.openExternal(url)
19-
})

docs/fiddles/windows/manage-windows/new-window/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h2>Create a new window</h2>
88
<h3>Supports: Win, macOS, Linux <span>|</span> Process: Main</h3>
99
<button id="new-window">View Demo</button>
1010
<p>The <code>BrowserWindow</code> module gives you the ability to create new windows in your app.</p>
11-
<p>There are a lot of options when creating a new window. A few are in this demo, but visit the <a id="browser-window-link" href="">documentation<span>(opens in new window)</span></a>
11+
<p>There are a lot of options when creating a new window. A few are in this demo, but visit the <a href="https://www.electronjs.org/docs/latest/api/browser-window">documentation<span>(opens in new window)</span></a>
1212
<div>
1313
<h2>ProTip</h2>
1414
<strong>Use an invisible browser window to run background tasks.</strong>

docs/fiddles/windows/manage-windows/new-window/main.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Modules to control application life and create native browser window
2-
const { app, BrowserWindow, ipcMain } = require('electron')
2+
const { app, BrowserWindow, ipcMain, shell } = require('electron')
33

44
ipcMain.on('new-window', (event, { url, width, height }) => {
55
const win = new BrowserWindow({ width, height })
@@ -19,6 +19,12 @@ function createWindow () {
1919

2020
// and load the index.html of the app.
2121
mainWindow.loadFile('index.html')
22+
23+
// Open external links in the default browser
24+
mainWindow.webContents.on('will-navigate', (event, url) => {
25+
event.preventDefault()
26+
shell.openExternal(url)
27+
})
2228
}
2329

2430
// This method will be called when Electron has finished
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
const { shell, ipcRenderer } = require('electron')
1+
const { ipcRenderer } = require('electron')
22

33
const newWindowBtn = document.getElementById('new-window')
4-
const link = document.getElementById('browser-window-link')
54

65
newWindowBtn.addEventListener('click', (event) => {
76
const url = 'https://electronjs.org'
87
ipcRenderer.send('new-window', { url, width: 400, height: 320 })
98
})
10-
11-
link.addEventListener('click', (e) => {
12-
e.preventDefault()
13-
shell.openExternal('https://www.electronjs.org/docs/latest/api/browser-window')
14-
})

0 commit comments

Comments
 (0)