在 Electron 中打开浏览器
Open a browser in Electron
目前,当我在 React 组件中单击 link 时,它会在我不想要的 Electron 应用程序中打开 URL。我尝试使用 used target _blank 打开浏览器,但没有成功。单击 link 时如何从 Electron 应用程序打开浏览器?
这是我所做的:
来自 React 组件
<div className="form-group">
<label htmlFor="uname1">Open Google from <a href="https://www.google.com" target="_blank">here</a>)</label>
</div>
Electronmain.js中的
const {app, BrowserWindow,Menu, shell} = require('electron')
const isDev = require('electron-is-dev')
process.env.NODE_ENV = "development"
const isMac = process.platform === "darwin" ? true : false
let mainWindow
function createMainWindow (){
mainWindow = new BrowserWindow({
width:1024,
height:768,
show: false,
backgroundColor:"#263238"
})
mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`)
mainWindow.once('ready-to-show', () => {
mainWindow.show();
mainWindow.focus();
});
}
document.body.addEventListener('click', event => {
if (event.target.tagName.toLowerCase() === 'a' && event.target.protocol != 'file:') {
event.preventDefault();
shell.openExternal(event.target.href);
}
});
非常感谢,非常感谢任何帮助。再次感谢
添加此侦听器:
mainWindow.webContents.on('new-window', function(event, url){
event.preventDefault();
electron.shell.openExternal(url);
}
目前,当我在 React 组件中单击 link 时,它会在我不想要的 Electron 应用程序中打开 URL。我尝试使用 used target _blank 打开浏览器,但没有成功。单击 link 时如何从 Electron 应用程序打开浏览器?
这是我所做的:
来自 React 组件
<div className="form-group">
<label htmlFor="uname1">Open Google from <a href="https://www.google.com" target="_blank">here</a>)</label>
</div>
Electronmain.js中的
const {app, BrowserWindow,Menu, shell} = require('electron')
const isDev = require('electron-is-dev')
process.env.NODE_ENV = "development"
const isMac = process.platform === "darwin" ? true : false
let mainWindow
function createMainWindow (){
mainWindow = new BrowserWindow({
width:1024,
height:768,
show: false,
backgroundColor:"#263238"
})
mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`)
mainWindow.once('ready-to-show', () => {
mainWindow.show();
mainWindow.focus();
});
}
document.body.addEventListener('click', event => {
if (event.target.tagName.toLowerCase() === 'a' && event.target.protocol != 'file:') {
event.preventDefault();
shell.openExternal(event.target.href);
}
});
非常感谢,非常感谢任何帮助。再次感谢
添加此侦听器:
mainWindow.webContents.on('new-window', function(event, url){
event.preventDefault();
electron.shell.openExternal(url);
}