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 коммит произвёл GitHub
Родитель eecfaec8c9
Коммит 24c9cbcc0a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
28 изменённых файлов: 99 добавлений и 183 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -68,10 +68,6 @@
</div>
</div>
</div>
<script>
// You can also require other files to run in this process
require("./renderer.js");
</script>
</body>
</html>

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -26,7 +26,7 @@
<p>
Open the
<a href="https://electronjs.org/docs/all/#notifications-windows-linux-macos">
<a href="https://electronjs.org/docs/latest/tutorial/notifications">
full API documentation<span>(opens in new window)</span>
</a>
in your browser.

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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