js通过浏览器打开本地exe文件

180 阅读1分钟

通过注册表来实现

1.新建txt文件进行如下配置:

Windows Registry Editor Version 5.00
 
[HKEY_CLASSES_ROOT\zidingyi]
@="zidingyi Protocol"
"URL Protocol"= ""
 
[HKEY_CLASSES_ROOT\zidingyi\DefaultIcon]
@="D:\\mqttx\\MQTTX.exe"
 
[HKEY_CLASSES_ROOT\zidingyi\shell]
@= ""
 
[HKEY_CLASSES_ROOT\zidingyi\shell\open]
@= ""
 
[HKEY_CLASSES_ROOT\zidingyi\shell\open\command]
@="\"D:\\mqttx\\MQTTX.exe\""

将上面内容中两处exe的路径修改为要启动的exe的路径(修改zidingyi可以配置多个注册表)

2.配置好后,保存文件,并修改文件后缀为.reg,修改完成,双击运行

3.在页面中封装对应方法

// 一键打开应用程序
const open_miniapp = str => {
  // 检测用户的设备是否是Windows系统
  if (/windows|win32/i.test(navigator.userAgent)) {
    // 检测用户的浏览器是否能使用zidingyi
    let openApp = false
    let userAgent = navigator.userAgent
    //判断是否 Firefox浏览器
    if (userAgent.indexOf('Firefox') > -1) {
      openApp = true
    }
    //判断是否 Chrome浏览器
    if (userAgent.indexOf('Chrome') > -1) {
      openApp = true
    }
    //判断是否 Edge浏览器
    if (userAgent.indexOf('Edg') > -1) {
      openApp = true
    }
    if (openApp) {
      try {
        window.location.href = str // window.open(str)或者a标签的href属性
      } catch (err) {
        console.log(err)
        this.$message.warning('找不到该程序,请手动打开!')
      }
    } else {
      this.$message.warning('您的浏览器不支持自动打开程序,请手动打开程序!')
      this.$notify.warning({
        title: '提示',
        content: '推荐使用Chrome浏览器!',
        placement: 'bottom-right',
        duration: 3000
      })
    }
  } else {
    this.$message.warning('您的设备暂不支持运行程序,请在Windows电脑上进行操作!')
  }
}