7、electron通过浏览器打开链接
electron默认打开链接是在当前程序里面打开,可以通过系统的浏览器默认打开链接
1、新建渲染进程页“通过浏览器打开链接.html”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h3> <a id="baidu" href="https://baidu.com/">百度</a> </h3> <script> let {shell} = require('electron') let baidu = document.querySelector("#baidu"); baidu.onclick= function(e){ e.preventDefault(); let href = this.getAttribute('href'); shell.openExternal(href); //通过浏览器打开链接 } </script> </body> </html>
2、主进程index.js
1 var electron = require('electron') 2 3 var app = electron.app //引用APP 4 var BrowserWindow = electron.BrowserWindow; //窗口引用 5 var mainWindow = null; //声明要打开的主窗口 6 7 app.on('ready',()=>{ 8 mainWindow = new BrowserWindow({ 9 width:800, 10 height:800, 11 webPreferences: 12 { 13 nodeIntegration:true, 14 contextIsolation:false, 15 enableRemoteModule: true, //允许使用remote模块 16 } 17 }); 18 19 require("@electron/remote/main").initialize(); //初始化remote模块 20 require("@electron/remote/main").enable(mainWindow.webContents); // 21 22 require('./menu.js') 23 //mainWindow.loadFile('index.html'); 24 //mainWindow.loadFile('打开新窗口.html'); 25 mainWindow.loadFile('通过浏览器打开链接.html'); 26 //mainWindow.openDevTools(); 27 mainWindow.on('close',()=>{ 28 mainWindow = null; //关闭窗口释放资源 29 }) 30 });