前端判断本地是否安装exe文件,有则打开,没有提醒下载

文章介绍了一个在Vue项目中使用protocolcheck.js来检查和启动程序的方法。当检测到特定协议如myr://时,如果未安装相应程序,会提示用户下载;已安装则自动唤醒。同时提到了通过注册表设置程序关联,以及如何编写reg文件手动创建注册表项。现在需求是实现自动写入注册表功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目需要:老板给了我一个免安装的程序,提供给客户,首次下载,后续有则打开这个程序,没有就提醒客户下载。

1.protocolcheck.js

protocolcheck.js 这个是 github上面大神写的方法

(function (f) { if (typeof exports === "object" && typeof module !== "undefined") { module.exports = f() } else if (typeof define === "function" && define.amd) { define([], f) } else { var g; if (typeof window !== "undefined") { g = window } else if (typeof global !== "undefined") { g = global } else if (typeof self !== "undefined") { g = self } else { g = this } g.protocolCheck = f() } })(function () {
  var define, module, exports; return (function e(t, n, r) { function s(o, u) { if (!n[o]) { if (!t[o]) { var a = typeof require == "function" && require; if (!u && a) return a(o, !0); if (i) return i(o, !0); var f = new Error("Cannot find module '" + o + "'"); throw f.code = "MODULE_NOT_FOUND", f } var l = n[o] = { exports: {} }; t[o][0].call(l.exports, function (e) { var n = t[o][1][e]; return s(n ? n : e) }, l, l.exports, e, t, n, r) } return n[o].exports } var i = typeof require == "function" && require; for (var o = 0; o < r.length; o++)s(r[o]); return s })({
    1: [function (require, module, exports) {
      function _registerEvent(target, eventType, cb) {
        if (target.addEventListener) {
          target.addEventListener(eventType, cb);
          return {
            remove: function () {
              target.removeEventListener(eventType, cb);
            }
          };
        } else {
          target.attachEvent(eventType, cb);
          return {
            remove: function () {
              target.detachEvent(eventType, cb);
            }
          };
        }
      }

      function _createHiddenIframe(target, uri) {
        var iframe = document.createElement("iframe");
        iframe.src = uri;
        iframe.id = "hiddenIframe";
        iframe.style.display = "none";
        target.appendChild(iframe);

        return iframe;
      }

      function openUriWithHiddenFrame(uri, failCb, successCb) {

        var timeout = setTimeout(function () {
          failCb();
          handler.remove();
        }, 1000);

        var iframe = document.querySelector("#hiddenIframe");
        if (!iframe) {
          iframe = _createHiddenIframe(document.body, "about:blank");
        }

        var handler = _registerEvent(window, "blur", onBlur);

        function onBlur() {
          clearTimeout(timeout);
          handler.remove();
          successCb();
        }

        iframe.contentWindow.location.href = uri;
      }

      function openUriWithTimeoutHack(uri, failCb, successCb) {

        var timeout = setTimeout(function () {
          failCb();
          handler.remove();
        }, 1000);

        //handle page running in an iframe (blur must be registered with top level window)
        var target = window;
        while (target != target.parent) {
          target = target.parent;
        }

        var handler = _registerEvent(target, "blur", onBlur);

        function onBlur() {
          clearTimeout(timeout);
          handler.remove();
          successCb();
        }

        window.location = uri;
      }

      function openUriUsingFirefox(uri, failCb, successCb) {
        var iframe = document.querySelector("#hiddenIframe");

        if (!iframe) {
          iframe = _createHiddenIframe(document.body, "about:blank");
        }

        try {
          iframe.contentWindow.location.href = uri;
          successCb();
        } catch (e) {
          if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
            failCb();
          }
        }
      }

      function openUriUsingIEInOlderWindows(uri, failCb, successCb) {
        if (getInternetExplorerVersion() === 10) {
          openUriUsingIE10InWindows7(uri, failCb, successCb);
        } else if (getInternetExplorerVersion() === 9 || getInternetExplorerVersion() === 11) {
          openUriWithHiddenFrame(uri, failCb, successCb);
        } else {
          openUriInNewWindowHack(uri, failCb, successCb);
        }
      }

      function openUriUsingIE10InWindows7(uri, failCb, successCb) {
        var timeout = setTimeout(failCb, 1000);
        window.addEventListener("blur", function () {
          clearTimeout(timeout);
          successCb();
        });

        var iframe = document.querySelector("#hiddenIframe");
        if (!iframe) {
          iframe = _createHiddenIframe(document.body, "about:blank");
        }
        try {
          iframe.contentWindow.location.href = uri;
        } catch (e) {
          failCb();
          clearTimeout(timeout);
        }
      }

      function openUriInNewWindowHack(uri, failCb, successCb) {
        var myWindow = window.open('', '', 'width=0,height=0');

        myWindow.document.write("<iframe src='" + uri + "'></iframe>");

        setTimeout(function () {
          try {
            myWindow.location.href;
            myWindow.setTimeout("window.close()", 1000);
            successCb();
          } catch (e) {
            myWindow.close();
            failCb();
          }
        }, 1000);
      }

      function openUriWithMsLaunchUri(uri, failCb, successCb) {
        navigator.msLaunchUri(uri,
          successCb,
          failCb
        );
      }

      function checkBrowser() {
        var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
        var ua = navigator.userAgent.toLowerCase();
        return {
          isOpera: isOpera,
          isFirefox: typeof InstallTrigger !== 'undefined',
          isSafari: (~ua.indexOf('safari') && !~ua.indexOf('chrome')) || Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0,
          isIOS: /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream,
          isChrome: !!window.chrome && !isOpera,
          isIE: /*@cc_on!@*/false || !!document.documentMode // At least IE6
        }
      }

      function getInternetExplorerVersion() {
        var rv = -1;
        if (navigator.appName === "Microsoft Internet Explorer") {
          var ua = navigator.userAgent;
          var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
          if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
        }
        else if (navigator.appName === "Netscape") {
          var ua = navigator.userAgent;
          var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
          if (re.exec(ua) != null) {
            rv = parseFloat(RegExp.$1);
          }
        }
        return rv;
      }

      module.exports = function (uri, failCb, successCb, unsupportedCb) {
        function failCallback() {
          failCb && failCb();
        }

        function successCallback() {
          successCb && successCb();
        }

        if (navigator.msLaunchUri) { //for IE and Edge in Win 8 and Win 10
          openUriWithMsLaunchUri(uri, failCb, successCb);
        } else {
          var browser = checkBrowser();

          if (browser.isFirefox) {
            openUriUsingFirefox(uri, failCallback, successCallback);
          } else if (browser.isChrome || browser.isIOS) {
            openUriWithTimeoutHack(uri, failCallback, successCallback);
          } else if (browser.isIE) {
            openUriUsingIEInOlderWindows(uri, failCallback, successCallback);
          } else if (browser.isSafari) {
            openUriWithHiddenFrame(uri, failCallback, successCallback);
          } else {
            unsupportedCb();
            //not supported, implement please
          }
        }
      }

    }, {}]
  }, {}, [1])(1)
});

vue项目,我使用的是下面这个(百度来的,有导出,上面的没成功)

function _registerEvent(target, eventType, cb) {
  if (target.addEventListener) {
    target.addEventListener(eventType, cb);
    return {
      remove: function () {
        target.removeEventListener(eventType, cb);
      }
    };
  } else {
    target.attachEvent(eventType, cb);
    return {
      remove: function () {
        target.detachEvent(eventType, cb);
      }
    };
  }
}

export function openUriWithTimeoutHack(uri, failCb, successCb) {

  var timeout = setTimeout(function () {
    failCb();
    handler.remove();
  }, 1000);

  //handle page running in an iframe (blur must be registered with top level window)
  var target = window;
  while (target != target.parent) {
    target = target.parent;
  }

  var handler = _registerEvent(target, "blur", onBlur);

  function onBlur() {
    clearTimeout(timeout);
    handler.remove();
    successCb();
  }

  window.location = uri;

}

2.使用
import { openUriWithTimeoutHack } from "./protocolcheck.js";

 transfer3dtiles() {
      openUriWithTimeoutHack(
        "myr://",
        () => {
          console.log("未安装");
          this.$confirm(
            "未安装,下载...",
            "提示",
            {
              confirmButtonText: "下载",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
            .then(() => {
              window.open("下载地址");
            })
            .catch(() => {
              console.log("取消下载");
            });
        },
        () => {
          console.log("已安装,自动唤起");
        }
      );
    },

3.写入注册表

创建reg文件,双击执行

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\myr]
"URL Protocol"="D:\\osg2cesiumApp_V1.10\\osg2cesiumApp\\osg2cesiumApp.exe"
@="osg2cesiumApp"
[HKEY_CLASSES_ROOT\myr\DefaultIcon]
@="D:\\osg2cesiumApp_V1.10\\osg2cesiumApp\\osg2cesiumApp.exe"
[HKEY_CLASSES_ROOT\myr\shell]
[HKEY_CLASSES_ROOT\myr\shell\open]
[HKEY_CLASSES_ROOT\myr\shell\open\command]
@="\"D:\\osg2cesiumApp_V1.10\\osg2cesiumApp\\osg2cesiumApp.exe\""

"D:\\osg2cesiumApp_V1.10\\osg2cesiumApp\\osg2cesiumApp.exe"

exe文件路径

myr

注册表项,与openUriWithTimeoutHack第一个参数对应

老板要求我实现自动写入注册表,大佬们,这个要怎么实现,跪求!!!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值