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 });
View Code
复制代码

 

posted @   ziff123  阅读(1667)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示