Electron 实现通过协议打开应用

前提:

        项目需要,通过在浏览器地址栏输入对应的软件协议来唤起应用。

实现原理:

1.使用electron的setAsDefaultProtocolClient方法(扩展阅读:app | Electron

app.setAsDefaultProtocolClient(protocol[, path, args])

  • protocol string - The name of your protocol, without ://. For example, if you want your app to handle electron:// links, call this method with electron as the parameter.
  • path string (optional) Windows - The path to the Electron executable. Defaults to process.execPath
  • args string[] (optional) Windows - Arguments passed to the executable. Defaults to an empty array

Returns boolean - Whether the call succeeded.

Sets the current executable as the default handler for a protocol (aka URI scheme). It allows you to integrate your app deeper into the operating system. Once registered, all links with your-protocol:// will be opened with the current executable. The whole link, including protocol, will be passed to your application as a parameter.

Note: On macOS, you can only register protocols that have been added to your app's info.plist, which cannot be modified at runtime. However, you can change the file during build time via Electron ForgeElectron Packager, or by editing info.plist with a text editor. Please refer to Apple's documentation for details.

Note: In a Windows Store environment (when packaged as an appx) this API will return true for all calls but the registry key it sets won't be accessible by other applications. In order to register your Windows Store application as a default protocol handler you must declare the protocol in your manifest.

// 应用主文件

public constructor(){
    // ******

    this.setAsDefaultProtocolClient();

    // ******
}

  
private setAsDefaultProtocolClient () {
    app.setAsDefaultProtocolClient(${protocol_string});
}

 

2.在应用安装模版中添加协议注册方法

  ; 注册 URL 协议
  WriteRegStr HKCR "${PROTOCOL_STRING}" "" "URL:${PROTOCOL_STRING}"
  WriteRegStr HKCR "${PROTOCOL_STRING}" "URL Protocol" ""
  WriteRegStr HKCR "${PROTOCOL_STRING}\DefaultIcon" "" "$appExe,0"
  WriteRegStr HKCR "${PROTOCOL_STRING}\shell" "" ""
  WriteRegStr HKCR "${PROTOCOL_STRING}\shell\open" "" ""
  WriteRegStr HKCR "${PROTOCOL_STRING}\shell\open\command" "" '"$appExe" "%1"'

 

各位朋友做协议时应该见过如下url: tencent://message/?uin=88888Site=bbs.125.laMenu=yes 复制代码 在浏览器里打开这个连接,会唤起qq的聊天窗口,并且根据传递的参数88888,打开了与88888的强制聊天窗口,如下图: 又比如打开浏览器,输入 steam://install/943700 复制代码 居然会唤起steam的安装界面,安装某个游戏。 是否想知道他们是如何实现的呢? 通过注册表中的探索,发现了秘密: 导出注册表查看: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\steam] @="URL:steam protocol" "URL Protocol"="" [HKEY_CLASSES_ROOT\steam\DefaultIcon] @="Steam.exe" [HKEY_CLASSES_ROOT\steam\Shell] [HKEY_CLASSES_ROOT\steam\Shell\Open] [HKEY_CLASSES_ROOT\steam\Shell\Open\Command] @="\"C:\\Program Files (x86)\\Steam\\Steam.exe\" -- \"%1\"" 原来是在这里的定义了私有协议,我们依葫芦画瓢,来定义一个自己的私有协议“Mofei”吧。 构造一个注册表: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Mofei] @="URL:Mofei Protocol Handler" "URL Protocol"="" [HKEY_CLASSES_ROOT\Mofei\shell] [HKEY_CLASSES_ROOT\Mofei\shell\open] [HKEY_CLASSES_ROOT\Mofei\shell\open\command] @="C:\\Users\\Administrator\\Desktop\\test.exe \"%1\"" 以上代码中"Mofei"为需要注册的协议名,例如Tencent/steam。 下面的 HKEY_CLASSES_ROOT\Mofei\shell\open\command的键值“ C:\\Users\\Administrator\\Desktop\\test.exe ”为要处理的程序的路径。 可以将以上代码通过记事本保存为xx.reg,双击此注册表文件导入。也可以通过精易模块 自行操作注册表来创建表项和键值。 此一步的目的是告诉windows,假如计算机请求的url是以"mofei://"为开头,就交给注册表中用户自定义的程序来处理,且整个请求的url内容作为启动参数,传递给应用程序。 例如在你的浏览器中打开 "mofei://hahaha",此时windows将会启动我们设定的test.exe程序,并且将"mofei://hahaha"作为启动参数传递给test.exe。 现在我们启动易语言 ,新建一个空白无窗口程序,写下如下代码: 窗口程序集名 保 留 保 留 备 注 程序集1 子程序名 返回值类型 公开 备 注 _启动子程序 整数型 请在本子程序中放置动态链接库初始化代码 变量名 类 型 静态 数组 备 注 aa 文本型 0 取命令行 ( aa) 信息框 ( aa[ 1] , 0, ,) 返回 ( 0) ' 返回值被忽略。 将程序编译为test.exe,放置到桌面上(我们事先定义的路径)。 现在,我们打开任意浏览器(大部分浏览器都支持私有协议),输入我们注册的"mofei://hahaha",处于安全考虑,首次使用新注册的私有协议时浏览器程序会先询问是否允许,选择允许即可。 于是,你看到了如下界面,此时我们自己注册的私有协议就成功的拉起了我们的程序。 firefox和chrome都可以哦: 现在,我们可以取出参数供我们的程序使用。 窗口程序集名 保 留 保 留 备 注 程序集1 子程序名 返回值类型 公开 备 注 _启动子程序 整数型 请在本子程序中放置动态链接库初始化代码 变量名 类 型 静态 数组 备 注 aa 文本型 0 取命令行 ( aa) 信息框 ( 取文本中间 ( aa[ 1] , 9,取文本长度 ( aa[ 1] ) - 9) , 0, ,) 返回 ( 0) ' 返回值被忽略。 用firefox来看一下执行结果。 个别浏览器会对传入的中文参数进行url编码,例如chrome,如果你的程序中未判断参数编码,就会造成如下状况,如下演示: 不止是浏览器可以唤起,连windows的资源管理器也可以唤起哦。 甚至连易语言也可以唤起哦。 方
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值