ie 浏览器打开exe 文件

通过 IE 的 ActiveX 控件实现。利用 ActiveXObject 对象,通过脚本的方式调用 cmd,并执行打开本地应用程序的命令。

下面是通过 IE 打开火狐浏览器的示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>通过IE打开火狐浏览器</title>
</head>
<body>
<button onclick="test()">测试</button>

<script>
function test() {
// 判断是否为 IE 浏览器
if (typeof ActiveXObject !== 'undefined') {
// IE 浏览器
try {
var objShell = new ActiveXObject('WScript.Shell');
var result = objShell.Run('cmd.exe /c start firefox.exe http://www.baidu.com', 0, true);
if (result !== 0) {
// 未安装火狐
}
} catch (e) {
// Automation 服务器不能创建对象——未启用 ActiveX 控件
}
} else {
// 非 IE 浏览器处理
}
}
</script>
</body>
</html>

  

注意:使用该方式,首先需要启用 IE 的 ActiveX 控件。

第一步:打开IE浏览器,选择工具栏中的工具 -> Internet选项
image.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第二步:选择【安全】标签 –> Internet -> 单击自定义级别;


image.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第三步:在单击自定义级别打开的面板中,找到ActiveX控件和插件,启用下图红框圈出的相应选项; 

 

image.png image.png

第四步:设置完成后,单击确定退出设置区域;重启IE浏览器,即可生效。


通用方法(支持IE/火狐/谷歌)

通过注册表方式启动本地 exe 程序。

第一步:新建 .reg 注册表文件

image.png

 

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\myWebshell]
@="URL:myWebshell Protocol Handler"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\myWebshell\DefaultIcon]
@="D:\\Soft\\Navicat Premium\\navicat.exe"
[HKEY_CLASSES_ROOT\myWebshell\shell]
[HKEY_CLASSES_ROOT\myWebshell\shell\open]
[HKEY_CLASSES_ROOT\myWebshell\shell\open\command]
@="\"D:\\Soft\\Navicat Premium\\navicat.exe\" \"%1\""

  

 

第二步:双击执行 .reg 文件,添加到注册表。

 image.png

第三步:在网页上创建调用链接:协议名://+自定义参数

<a href="myWebshell://hello">打开 Navicat </a>

判断协议是否存在

使用 protocolcheck.js 库实现。

protocolCheck(`myWebshell://hello`, function () {
       alert('当前电脑未检测到 myWebshell 程序。');
});

1.新建protocolcheck.js文件(内容是github里一个大神写的):

内容如下:
  1 (function(f){
  2     if(typeof exports==="object"&&typeof module!=="undefined"){
  3         module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)
  4         }else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){
  5             g=global
  6         }else if(typeof self!=="undefined"){
  7             g=self
  8         }else{
  9             g=this
 10         }
 11         g.protocolCheck = f()
 12     }
 13 }
 14 )
 15 (function(){
 16     var define,module,exports;
 17     return (function e(t,n,r){
 18         function s(o,u){
 19             if(!n[o]){if(!t[o]){
 20                 var a=typeof require=="function"&&require;
 21                 if(!u&&a)return a(o,!0);
 22                 if(i)return i(o,!0);
 23                 var f=new Error("Cannot find module '"+o+"'");
 24                 throw f.code="MODULE_NOT_FOUND",f
 25             }
 26             var l=n[o]={
 27                 exports:{}};
 28                 t[o][0].call(l.exports,function(e){
 29                     var n=t[o][1][e];
 30                     return s(n?n:e)},l,l.exports,e,t,n,r)
 31                 }
 32                 return n[o].exports
 33             }
 34             var i=typeof require=="function"&&require;
 35             for(var o=0;o<r.length;o++)s(r[o]);
 36             return s
 37         })
 38         ({1:[function(require,module,exports){
 39             
 40 function _registerEvent(target, eventType, cb) {
 41     if (target.addEventListener) {
 42         target.addEventListener(eventType, cb);
 43         return {
 44             remove: function () {
 45                 target.removeEventListener(eventType, cb);
 46             }
 47         };
 48     } else {
 49         target.attachEvent(eventType, cb);
 50         return {
 51             remove: function () {
 52                 target.detachEvent(eventType, cb);
 53             }
 54         };
 55     }
 56 }
 57 
 58 function _createHiddenIframe(target, uri) {
 59     var iframe = document.createElement("iframe");
 60     iframe.src = uri;
 61     iframe.id = "hiddenIframe";
 62     iframe.style.display = "none";
 63     target.appendChild(iframe);
 64 
 65     return iframe;
 66 }
 67 
 68 function openUriWithHiddenFrame(uri, failCb, successCb) {
 69 
 70     var timeout = setTimeout(function () {
 71         failCb();
 72         handler.remove();
 73     }, 1000);
 74 
 75     var iframe = document.querySelector("#hiddenIframe");
 76     if (!iframe) {
 77         iframe = _createHiddenIframe(document.body, "about:blank");
 78     }
 79 
 80     var handler = _registerEvent(window, "blur", onBlur);
 81 
 82     function onBlur() {
 83         clearTimeout(timeout);
 84         handler.remove();
 85         successCb();
 86     }
 87 
 88     iframe.contentWindow.location.href = uri;
 89 }
 90 
 91 function openUriWithTimeoutHack(uri, failCb, successCb) {
 92     
 93     var timeout = setTimeout(function () {
 94         failCb();
 95         handler.remove();
 96     }, 1000);
 97 
 98     //handle page running in an iframe (blur must be registered with top level window)
 99     var target = window;
100     while (target != target.parent) {
101         target = target.parent;
102     }
103 
104     var handler = _registerEvent(target, "blur", onBlur);
105 
106     function onBlur() {
107         clearTimeout(timeout);
108         handler.remove();
109         successCb();
110     }
111 
112     window.location = uri;
113 }
114 
115 function openUriUsingFirefox(uri, failCb, successCb) {
116     var iframe = document.querySelector("#hiddenIframe");
117 
118     if (!iframe) {
119         iframe = _createHiddenIframe(document.body, "about:blank");
120     }
121 
122     try {
123         iframe.contentWindow.location.href = uri;
124         successCb();
125     } catch (e) {
126         if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
127             failCb();
128         }
129     }
130 }
131 
132 function openUriUsingIEInOlderWindows(uri, failCb, successCb) {
133     if (getInternetExplorerVersion() === 10) {
134         openUriUsingIE10InWindows7(uri, failCb, successCb);
135     } else if (getInternetExplorerVersion() === 9 || getInternetExplorerVersion() === 11) {
136         openUriWithHiddenFrame(uri, failCb, successCb);
137     } else {
138         openUriInNewWindowHack(uri, failCb, successCb);
139     }
140 }
141 
142 function openUriUsingIE10InWindows7(uri, failCb, successCb) {
143     var timeout = setTimeout(failCb, 1000);
144     window.addEventListener("blur", function () {
145         clearTimeout(timeout);
146         successCb();
147     });
148 
149     var iframe = document.querySelector("#hiddenIframe");
150     if (!iframe) {
151         iframe = _createHiddenIframe(document.body, "about:blank");
152     }
153     try {
154         iframe.contentWindow.location.href = uri;
155     } catch (e) {
156         failCb();
157         clearTimeout(timeout);
158     }
159 }
160 
161 function openUriInNewWindowHack(uri, failCb, successCb) {
162     var myWindow = window.open('', '', 'width=0,height=0');
163 
164     myWindow.document.write("<iframe src='" + uri + "'></iframe>");
165 
166     setTimeout(function () {
167         try {
168             myWindow.location.href;
169             myWindow.setTimeout("window.close()", 1000);
170             successCb();
171         } catch (e) {
172             myWindow.close();
173             failCb();
174         }
175     }, 1000);
176 }
177 
178 function openUriWithMsLaunchUri(uri, failCb, successCb) {
179     navigator.msLaunchUri(uri,
180         successCb,
181         failCb
182     );
183 }
184 
185 function checkBrowser() {
186     var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
187     var ua = navigator.userAgent.toLowerCase();
188     return {
189         isOpera   : isOpera,
190         isFirefox : typeof InstallTrigger !== 'undefined',
191         isSafari  : (~ua.indexOf('safari') && !~ua.indexOf('chrome')) || Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0,
192         isIOS     : /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream,
193         isChrome  : !!window.chrome && !isOpera,
194         isIE      : /*@cc_on!@*/false || !!document.documentMode // At least IE6
195     }
196 }
197 
198 function getInternetExplorerVersion() {
199     var rv = -1;
200     if (navigator.appName === "Microsoft Internet Explorer") {
201         var ua = navigator.userAgent;
202         var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
203         if (re.exec(ua) != null)
204             rv = parseFloat(RegExp.$1);
205     }
206     else if (navigator.appName === "Netscape") {
207         var ua = navigator.userAgent;
208         var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
209         if (re.exec(ua) != null) {
210             rv = parseFloat(RegExp.$1);
211         }
212     }
213     return rv;
214 }
215 
216 module.exports = function(uri, failCb, successCb, unsupportedCb) {
217     function failCallback() {
218         failCb && failCb();
219     }
220 
221     function successCallback() {
222         successCb && successCb();
223     }
224 
225     if (navigator.msLaunchUri) { //for IE and Edge in Win 8 and Win 10
226         openUriWithMsLaunchUri(uri, failCb, successCb);
227     } else {
228         var browser = checkBrowser();
229 
230         if (browser.isFirefox) {
231             openUriUsingFirefox(uri, failCallback, successCallback);
232         } else if (browser.isChrome || browser.isIOS) {
233             openUriWithTimeoutHack(uri, failCallback, successCallback);
234         } else if (browser.isIE) {
235             openUriUsingIEInOlderWindows(uri, failCallback, successCallback);
236         } else if (browser.isSafari) {
237             openUriWithHiddenFrame(uri, failCallback, successCallback);
238         } else {
239             unsupportedCb();
240             //not supported, implement please
241         }
242     }
243 }
244 
245 },{}]},{},[1])(1)
246 });
View Code

 

 

操作之前需要对ie浏览器设置,选择internet 选项 ,安全,受信任的站点,改区域的安全级别设置为最低。

源地址

https://juejin.cn/post/6959443418355335199

posted @ 2021-12-16 16:49  完全hi  阅读(1300)  评论(0编辑  收藏  举报