JavaScript设置受信任站点 允许未签名Activex运行
1 /// <summary>注册表操作类</summary> 2 RegInstance = { 3 Instance: null, 4 CreateInstance: function () { 5 if (RegInstance.Instance === null) { 6 var obj = new ActiveXObject("WScript.Shell"); 7 if (obj) { 8 RegInstance.Instance = obj; 9 return true; 10 } 11 else { 12 return false; 13 } 14 } 15 else { 16 return true; 17 } 18 }, 19 RegRead: function (reg) { 20 if (RegInstance.CreateInstance()) { 21 return RegInstance.Instance.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges"); 22 } 23 }, 24 RegWrite: function (regKeyName, regKeyValue, regDataType, regData) { 25 if (RegInstance.CreateInstance()) { 26 if (regKeyName == "" || typeof regKeyName == "undefined") { 27 throw "arguments regKeyName cannot be null"; 28 } 29 if (typeof regKeyValue == "undefined") { 30 RegInstance.Instance.RegWrite(regKeyName); 31 } 32 else { 33 if (typeof regDataType == "undefined") { 34 RegInstance.Instance.RegWrite(regKeyName, regKeyValue); 35 } 36 else { 37 if (typeof regData == "undefined") { 38 RegInstance.Instance.RegWrite(regKeyName, regKeyValue, regDataType); 39 } 40 else { 41 RegInstance.Instance.RegWrite(regKeyName, regKeyValue, regDataType, regData); 42 } 43 } 44 } 45 } 46 } 47 }; 48 49 RegDataType = { 50 REG_BINARY: "REG_BINARY", 51 REG_SZ: "REG_SZ", 52 REG_MULTI_SZ: "REG_MULTI_SZ", 53 REG_EXPAND_SZ: "REG_EXPAND_SZ ", 54 REG_QWORD: "REG_QWORD", 55 REG_DWORD: "REG_DWORD" 56 } 57 58 /// <summary>IE注册表设置</summary> 59 InternetInstance = { 60 IESafeSetting: "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\",//IE安全路径设置 61 IEAdvanceSetting: "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main\\",//IE高级设置 62 IETrustedSitesSetting: "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\",//受信任域名站点 63 IETrustedIPSetting: "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\RANGES\\",//受信任的IP站点 64 //下载已签名Activex 65 SetHadSignedActivex: function (b) { 66 RegInstance.RegWrite(InternetInstance.IESafeSetting, 0x00000000, "REG_DWORD"); 67 }, 68 SaveTrustedSites: function (url) { 69 var analyze = UrlAnalyze.Analyze(url); 70 //IP和domain受信任地址不一致,需要添加到不同的注册表节点 71 if (analyze.isIP == true) { 72 var rangeIndex = analyze.host; 73 var regKey = InternetInstance.IETrustedIPSetting + "Range" + rangeIndex; 74 RegInstance.RegWrite(regKey + "\\:Range", analyze.host); 75 RegInstance.RegWrite(regKey + "\\" + analyze.sechme, "2", RegDataType.REG_DWORD); 76 //RegInstance.RegWrite(regKey, ""); 77 } 78 else { 79 //判断是否含有www. 80 var regKey = ""; 81 var isW = false; 82 if (("s" + analyze.host).toLowerCase().indexOf("www") == 1) { 83 isW = true; 84 regKey = InternetInstance.IETrustedSitesSetting + analyze.host.substring(4, analyze.host.length); 85 } 86 else { 87 regKey = InternetInstance.IETrustedSitesSetting + analyze.host; 88 } 89 90 if (isW) { 91 RegInstance.RegWrite(regKey + "\\www\\" + analyze.sechme, "2", RegDataType.REG_DWORD); 92 } 93 else { 94 RegInstance.RegWrite(regKey + "\\" + analyze.sechme, "2", RegDataType.REG_DWORD); 95 } 96 } 97 }, 98 SaveThisSiteTrusted: function () { 99 var thisUrl = document.location.href; 100 InternetInstance.SaveTrustedSites(thisUrl); 101 }, 102 SetActivexTrusted: function () { 103 var activexReg = InternetInstance.IEAdvanceSetting + "3\\"; 104 RegInstance.RegWrite(activexReg + "1001", "0", RegDataType.REG_DWORD); 105 RegInstance.RegWrite(activexReg + "1004", "0", RegDataType.REG_DWORD); 106 RegInstance.RegWrite(activexReg + "1200", "0", RegDataType.REG_DWORD); 107 RegInstance.RegWrite(activexReg + "1201", "0", RegDataType.REG_DWORD); 108 RegInstance.RegWrite(activexReg + "1405", "0", RegDataType.REG_DWORD); 109 RegInstance.RegWrite(activexReg + "2201", "0", RegDataType.REG_DWORD); 110 } 111 } 112 113 /// <summary>Url解析</summary> 114 UrlAnalyze = { 115 Analyze: function (url) { 116 if (typeof url != 'undefined') { 117 var segment = url.match(/^(\w+\:\/\/)?([\w\d]+(?:\.[\w]+)*)?(?:\:(\d+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/); 118 if (!segment[3]) { 119 segment[3] = '80'; 120 } 121 var param = {}; 122 if (segment[5]) { 123 var pse = segment[5].match(/([^=&]+)=([^&]+)/g); 124 if (pse) { 125 for (var i = 0; i < pse.length; i++) { 126 param[pse[i].split('=')[0]] = pse[i].split('=')[1]; 127 } 128 } 129 } 130 //判断是IP还是域名 131 var isIP = false; 132 var str = segment[2].match(/(\d+)\.(\d+)\.(\d+)\.(\d+)/g); 133 if (!(str == null || RegExp.$1 > 255 || RegExp.$2 > 255 || RegExp.$3 > 255 || RegExp.$4 > 255)) { 134 isIP = true; 135 } 136 137 return { 138 url: segment[0], 139 sechme: segment[1].replace("://", "") == "" ? "http" : segment[1].replace("://", ""), 140 host: segment[2], 141 port: segment[3], 142 path: segment[4], 143 queryString: segment[5], 144 fregment: segment[6], 145 isIP: isIP, 146 param: param 147 }; 148 } 149 else { 150 return null; 151 } 152 } 153 }
调用方式如下:
if (RegInstance.CreateInstance()) { InternetInstance.SetActivexTrusted(); InternetInstance.SaveThisSiteTrusted(); }