首页 > 其他分享 >vue项目调用exe文件(JS方式)

vue项目调用exe文件(JS方式)

时间:2022-12-29 17:12:18浏览次数:37  
标签:function failCb vue target successCb uri JS exe var

方式一:

项目中引入下面JS

(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);
});

方式一调用:

 window.protocolCheck("hyvss://", () => {        alert('检测到当前电脑未安装,确认是否下载!')  });

方式二(Vue中使用):

export function openUrlWithInputTimeoutHack(url, failCb, successCb) {
    let target = document.createElement('input')
    target.style.width = '0'
    target.style.height = '0'
    target.style.position = 'fixed'
    target.style.top = '0'
    target.style.left = '0'
    document.body.appendChild(target)

    target.focus();
    var handler = _registerEvent(target, "blur", onBlur);
    console.log('focus')
    function onBlur() {
        console.log('blur')
        successCb && successCb()
        handler.remove()
        clearTimeout(timeout)
        document.body.removeChild(target)
    };

    //will trigger onblur
    location.href = url

    // Note: timeout could vary as per the browser version, have a higher value
    var timeout = setTimeout(function () {
        console.log('setTimeout')
        failCb && failCb()
        handler.remove()
        document.body.removeChild(target)
    }, 1000);
}

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);
            }
        };
    }
} 

引入文件:

import { openUrlWithInputTimeoutHack } from '@/utils/arouse'

方式二调用:

openUrlWithInputTimeoutHack('EaglEye:\\', () => {
  console.log('未安装');
  window.location.href = 'xxx软件下载路径'
}, () => {
  console.log('已安装,自动唤起');
})

 

标签:function,failCb,vue,target,successCb,uri,JS,exe,var
From: https://www.cnblogs.com/han024/p/17013026.html

相关文章

  • vue中的计算属性
    <!DOCTYPEhtml><html> <head>  <metacharset="UTF-8"/>  <title>列表过滤</title>  <scripttype="text/javascript"src="../js/vue.js"></script......
  • vue-pdf在线预览pdf文件
    1.安装vue-pdfnpminstall--savevue-pdf2.页面js注册组件importpdffrom'vue-pdf'components:{pdf}3.页面使用组件<pdfsrc=""></pdf>......
  • js里的类 class
    以前不知道为啥,总觉得这个类很高深莫测,然后自己在开发业务中也没有需要使用它的地方。所以就一直没去了解,今天有时间看了一下文档,在这稍微记录一下自己的总结。类的关键字......
  • js倒计时功能
    <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metaht......
  • main.js和 router文件夹里面的index.js引入的vue一定要一样
     当 main.js和router文件夹里面的index.js引入的vue不一样      错误:找不到<router-view>标签解决办法:将main.js和index.js的vue引入成为一样的......
  • vue3+TS 自定义指令:长按触发绑定的函数
    vue3+TS自定义指令:长按触发绑定的函数而然间看到一个在vue2中写的长按触发事件的自定义指定,想着能不能把他copy到我的vue3项目中呢。编写自定义指令时遇到的几个难点1.......
  • vue3 变量改变触发界面显示改变
    我们需要使用到 ref 和 reactive 来触发界面绑定变量的更新。reactive的参数对象一般是对象或者数组,能够将复杂数据类型变为响应式数据;它的响应式是深层次的,底层......
  • 记:后端对字符串进行gzip压缩,前端js进行gzip解压
    最近有个需求要求对长字符串进行gzip压缩,然后在js进行解压缩的操作:publicstaticvoidmain(String[]args){try{StringlongString="www.baidu.com";......
  • vue websockt 实现站内消息的发送和接收
    1.什么是 WebSocketwebsocket是HTML5开始提供的一种网络通信协议,它的目的是在浏览器之间建立一个不受限的双方通信的通道,比如说,服务器可以在任意时刻发送信息给浏览器。......
  • Vue中路由的配置问题
    Unknowncustomelement:<router-view>-didyouregisterthecomponentcorrectly?Forrecursivecomponents,makesuretoprovidethe"name"option.foundin不......