通过特定协议拉起 electron 应用

在 Android 通过 sheme 协议可以拉起其他应用。
electron 应用也可以通过类似特定协议被拉起。
在同时有 web、客户端的应用里,可以通过这种方式在 web 拉起客户端。

支持拉起客户端

  const PROTOCOL = 'xxx'
  if (process.defaultApp) {
    // 这里是开发环境,有启动参数
    if (process.argv.length >= 2) {
      app.setAsDefaultProtocolClient(PROTOCOL, process.execPath, [path.resolve(process.argv[1])]);
    }
  } else {
    app.setAsDefaultProtocolClient(PROTOCOL);
  }

这里注册启动协议得程序运行一次才生效,体验不好。有没有在安装时就生效的实现?同时在卸载时就取消注册?

mac

在electron-builder.yml加配置,xxx是协议名

# 注册客户端启动协议,适用于mac
protocols:
  - name: ' xxx'
    schemes: 
    - 'xxx'

windows

在electron-builder.yml的 build 目录增加installer.nsh文件,比如electron-builder.yml是

directories:
  buildResources: build

就增加build/installer.nsh

!define PROTOCOL_NAME "xxx"
!macro customInstall
  DeleteRegKey HKCU "${PROTOCOL_NAME}"
  WriteRegStr HKCU "${PROTOCOL_NAME}" "" "URL:${PROTOCOL_NAME}"
  WriteRegStr HKCU "${PROTOCOL_NAME}" "URL Protocol" ""
  WriteRegStr HKCU "${PROTOCOL_NAME}\shell" "" ""
  WriteRegStr HKCU "${PROTOCOL_NAME}\shell\Open" "" ""
  WriteRegStr HKCU "${PROTOCOL_NAME}\shell\Open\command" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME} %1"
!macroend

!macro customUnInstall
  DeleteRegKey HKCU "${PROTOCOL_NAME}"
!macroend

除此之外

还要记得本地开发时仍需要注册,不然调试不生效,但程序退出后记得取消注册

app.removeAsDefaultProtocolClient(PROTOCOL, process.execPath, [path.resolve(process.argv[1])]);

处理二次启动时的参数

拉起后希望能处理传参,比如在浏览器通过 xxx://open?a=1拉起,希望能获取到 open,a=1这些参数。由于 mac默认单例和electron实现的原因,在 mac 和 windows 下有些不一样。

mac

  // 处理请求参数
  app.on('open-url', (_, url) => {
    dialog.showErrorBox('Welcome Back from open-url', `You arrived from: ${url}`);
    handleUrl(url);
  });

windows

  app.on('second-instance', (_, commandLine) => {
    const url = commandLine[commandLine.length - 1]
    dialog.showErrorBox('Welcome Back 111', `You arrived from: ${url}`);
    if(url) handleUrl(url);
  });

处理首次启动时的参数

  if (process.argv.length > 1) {
    const url = process.argv.find(arg => arg.startsWith(`${PROTOCOL}://`));
    if (url) handleUrl(url, {fromInit: true});
  }

参考链接

https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app
https://xuxin123.com/electron/url-scheme/

Electron是一个开源框架,用于开发桌面应用程序,它基于Node.js和Chromium/Blink渲染引擎。如果你想通过协议(例如HTTP、HTTPS、文件系统协议等)在Electron应用中打开外部资源,可以利用其事件监听机制和`webContents`对象。 以下是基本步骤: 1. **创建Webview标签页**: 首先,在HTML模板中添加一个WebView标签,并设置允许所有协议访问: ```html <webview id="my-webview" src="about:blank" allow-file-access-from-files allow-scripts allow-popups></webview> ``` 2. **处理协议请求**: 在主进程的JavaScript文件中,你可以监听`new-window`事件,当用户试图通过协议打开新的窗口时触发。例如: ```javascript const { webContents } = require('electron').remote; webContents.on('new-window', (event, url) => { event.preventDefault(); // 取消默认的行为,然后按照你需要的方式打开 let myProtocolHandler = handleCustomProtocol(url); if (myProtocolHandler) { myProtocolHandler(); } }); function handleCustomProtocol(url) { // 这里检查url是否匹配你的自定义协议 if (url.startsWith('your-custom-protocol')) { // 打开应用或其他操作 return openYourApp(url); } return null; // 如果不是你的协议,返回null表示使用默认浏览器打开 } function openYourApp(url) { // 使用Electron APIs(如shell或webContents.openURL)打开应用 shell.openExternal(url); } ``` 3. **自定义协议注册**: 为了让Electron能识别你的自定义协议(如`your-custom-protocol://`),你需要在main进程中注册这个协议。这通常在`package.json`的`protocol`字段中配置: ```json "main": "main.js", "protocol": { "your-custom-protocol": { "schemes": ["your-custom-protocol"], "handler": "your-custom-protocol-handler" } }, ``` 然后在`your-custom-protocol-handler.js`中编写处理程序,根据实际需求解析并打开对应的应用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值