客户端开发(Electron)URL远程启动

简介: 客户端开发(Electron)URL远程启动

Electron是一个使用 JavaScript、HTML 和 CSS 构建桌面应用程序的框架。 嵌入 Chromium 和 Node.js 到 二进制的 Electron 允许您保持一个 JavaScript 代码代码库并创建 在Windows上运行的跨平台应用 macOS和Linux——不需要本地开发 经验。



本篇说明:


我们在使用一些客户端应用的时候,尤其是用的最多的微信,你在微信客户端打开一些页面的时一般都会默认在微信的浏览器打开,并且在右上角提供了按钮支持在你电脑上装的浏览器里打开。这样的功能是如何实现的呢?我们一起来走进Electron Url 远程启动。


环境说明:


  1. 设备环境Windows;
  2. 代码环境:快速入门


提示说明:


  1. 客户端开发在不同的平台有不同的特性;
  2. 不同的平台有不同的API。


客户端操作时:在浏览器打开:


第一步:在preload中定义打开浏览器的桥接函数

  1. 通过渲染进程发送open事件
  2. 桥接函数
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("shell", {
  open: () => ipcRenderer.send("shell:open"),
});
复制代码


第二步:绑定触发的事件

  1. 绑定函数
document.getElementById("open-in-browser").addEventListener("click", () => {
  shell.open();
});
复制代码
  1. 注:桥接函数会绑定到window对象上


第三步:主进程中监听shell:open事件

  1. 主进程做监听:
ipcMain.on("shell:open", () => {
  const pagePath = path.join("file://", __dirname, "index.html");
  shell.openExternal(pagePath);
});
复制代码


浏览器操作时:在客户端打开:


第一步:注册处理协议

if (process.defaultApp) {
  if (process.argv.length >= 2) {
    app.setAsDefaultProtocolClient("electron-fiddle", process.execPath, [
      path.resolve(process.argv[1]),
    ]);
  }
} else {
  app.setAsDefaultProtocolClient("electron-fiddle");
}
复制代码


第二步:因我们在windows平台使用,故需要做兼容处理

app.on("second-instance", (event, commandLine, workingDirectory) => {
    if (mainWindow) {
      if (mainWindow) {
          //如果被最小化就恢复,否则激活主窗口
        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.focus();
      }
    }
  });
复制代码


完整main代码:


const { app, BrowserWindow, ipcMain, shell, dialog } = require("electron");
const path = require("path");
// 注册协议处理器
if (process.defaultApp) {
  if (process.argv.length >= 2) {
    app.setAsDefaultProtocolClient("electron-fiddle", process.execPath, [
      path.resolve(process.argv[1]),
    ]);
  }
} else {
  app.setAsDefaultProtocolClient("electron-fiddle");
}
let mainWindow = null;
const createWindow = () => {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      nativeWindowOpen: true,
    },
  });
  mainWindow.loadFile("index.html");
};
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
  app.quit();
} else {
    // 当第二个实例被执行并且调用 app.requestSingleInstanceLock() 时,这个事件将在你的应用程序的首个实例中触发
  app.on("second-instance", (event, commandLine, workingDirectory) => {
    if (mainWindow) {
      if (mainWindow) {
          //如果被最小化就恢复,否则激活主窗口
        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.focus();
      }
    }
  });
  app.whenReady().then(() => {
    createWindow();
  });
}
app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});
ipcMain.on("shell:open", () => {
  const pagePath = path.join("file://", __dirname, "index.html");
  shell.openExternal(pagePath);
});
复制代码


总结:


本篇涉及到了以下2点:

  1. windows中第二个窗口实例处理;
  2. 注册处理协议。



相关文章
|
11月前
|
安全 测试技术 网络安全
软件测试|测试平台开发-Flask 入门:URL组成部分详解
软件测试|测试平台开发-Flask 入门:URL组成部分详解
59 0
|
18天前
|
数据采集 存储 前端开发
Java爬虫开发:Jsoup库在图片URL提取中的实战应用
Java爬虫开发:Jsoup库在图片URL提取中的实战应用
|
1月前
|
XML Android开发 UED
"掌握安卓开发新境界:深度解析AndroidManifest.xml中的Intent-filter配置,让你的App轻松响应scheme_url,开启无限交互可能!"
【8月更文挑战第2天】在安卓开发中,scheme_url 通过在`AndroidManifest.xml`中配置`Intent-filter`,使应用能响应特定URL启动或执行操作。基本配置下,应用可通过定义特定URL模式的`Intent-filter`响应相应链接。
79 12
|
1月前
|
JavaScript 开发工具
Electron 开发过程中主进程的无法看到 console.log 输出怎么办
Electron 开发过程中主进程的无法看到 console.log 输出怎么办
|
2月前
|
前端开发
PC端01,桌面端,electron的开发,electron的开发的系列课程,软件开发必备流程,electron的讲解,electron的开发,vitepress博主的gitee链接,PC端效率软件
PC端01,桌面端,electron的开发,electron的开发的系列课程,软件开发必备流程,electron的讲解,electron的开发,vitepress博主的gitee链接,PC端效率软件
PC端01,桌面端,electron的开发,electron的开发的系列课程,软件开发必备流程,electron的讲解,electron的开发,vitepress博主的gitee链接,PC端效率软件
|
4月前
|
移动开发 开发框架 JavaScript
Vue3 Vite electron 开发桌面程序
Vue3 Vite electron 开发桌面程序
295 0
|
4月前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
109 0
|
10月前
|
前端开发 算法 JavaScript
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(下)
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(下)
148 0
|
10月前
|
存储 Web App开发 JavaScript
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(上)
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(上)
198 0
|
12月前
orbeon form 通过 url 的方式同第三方应用集成的开发明细
orbeon form 通过 url 的方式同第三方应用集成的开发明细