[Javascript]加入信任網域

開發web應用程式有些時候挺麻煩的,多數的功能可不需要取得client端的資源,處理起來沒什麼問題,但如果今天要跟Outlook互動又或者要作Excel的資料匯入時,我們可能都會需要client端將網站加入信任網域,但如果要教導每個user如何將站台加入信任網域似乎又是件麻煩的事情

開發web應用程式有些時候挺麻煩的,多數的功能可不需要取得client端的資源,處理起來沒什麼問題,但如果今天要跟Outlook互動又或者要作Excel的資料匯入時,我們可能都會需要client端將網站加入信任網域,但如果要教導每個user如何將站台加入信任網域似乎又是件麻煩的事情,所以後來就請人寫了一段將目前網站加入信任網域的script,這段script是透過ActiveX的方式來進行,如果使用者端的IE安全性設定的太高就完全跳不出來囉,

01         var WshShell=new ActiveXObject("WScript.Shell");    
02
03         //將抓到網址列的位址 加入信任網站
04         var objnav =navigator;
05         var OsVersion = objnav.appVersion;
06         var url =window.location.hostname;  //抓網址列上的值
07         var urllength =url.length;
08         var domain1; //存放網址第一個.的前段
09         var domain2; //存放網址第一個.的後段
10         var num =url.indexOf("."); //找第一個"."
11         var rule= /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/; //驗證是否為ip格式的正規表示式
12         OsVersion= OsVersion.substring(38,41); //抓OS的版本
13         //判斷系統版本
14         if (OsVersion =="5.0")  //win2000
15         {
16             //判斷url為ip或domain
17             if(rule.test(url))
18             {              
19                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\","");
20                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\http","2","REG_DWORD");
21                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\:Range",url);
22             }

23             else
24             {
25                  domain1=url.substring(0,num);
26                  domain2=url.substring((num+1),urllength);              
27                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2,"");
28                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2+"\\"+domain1,"");
29                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2+"\\"+domain1+"\\http","2","REG_DWORD");
30             }
  
31         }

32         if (OsVersion =="5.1")  //winXP
33         {
34             //判斷url為ip或domain
35             if(rule.test(url))
36             {              
37                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\","");
38                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\http","2","REG_DWORD");
39                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\:Range",url);
40             }

41             else
42             {
43                  domain1=url.substring(0,num);
44                  domain2=url.substring((num+1),urllength);              
45                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2,"");
46                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2+"\\"+domain1,"");
47                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2+"\\"+domain1+"\\http","2","REG_DWORD");
48             }

49         }

50         if (OsVersion =="5.2")  //Win2003  
51         {
52             //判斷url為ip或domain
53             if(rule.test(url))
54             {  
55                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\EscRanges\\Range100\\","");
56                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\EscRanges\\Range100\\http","2","REG_DWORD");
57                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\EscRanges\\Range100\\:Range",url);
58             }

59             else
60             {
61                  domain1=url.substring(0,num);
62                  domain2=url.substring((num+1),urllength);        
63                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\EscDomains\\"+domain2,"");
64                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\EscDomains\\"+domain2+"\\"+domain1,"");
65                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\EscDomains\\"+domain2+"\\"+domain1+"\\http","2","REG_DWORD");
66             }

67         }
  
68         if (OsVersion =="6.0")  //vista
69         {
70             //判斷url為ip或domain
71             if(rule.test(url))
72             {              
73                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\","");
74                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\http","2","REG_DWORD");
75                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges\\Range100\\:Range",url);
76             }

77             else
78             {
79                  domain1=url.substring(0,num);
80                  domain2=url.substring((num+1),urllength);              
81                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2,"");
82                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2+"\\"+domain1,"");
83                  WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\"+domain2+"\\"+domain1+"\\http","2","REG_DWORD");
84             }
  
85         }

這段script主要是透過讀取client端的registry來判斷是否有將目前的網址加入信任網域,後來又追加了一段讓新視窗啟動時不要new一個tab,而是以dialog的方式呈現:

01 //先取得IE的版本
02 var tIEVersion = getClientIEVersion();
03 //如果是IE7.0以上的話,把tab的設定改成1:在新視窗中啟動
04 if(tIEVersion>6)
05 {
06       WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\TabbedBrowsing\\PopupsUseNewWindow", 1, "REG_DWORD");
07 }

08
09 function getClientIEVersion()
10 {
11     var version = 999;  
12     if (navigator.appVersion.indexOf("MSIE") != -1)    
13     {  
14         version = parseFloat(navigator.appVersion.split("MSIE")[1]);  
15     }
  
16     return version;
17 }

這算是一個方便的小功能,避免還要寫user guide去教導使用者,請他按下某個按鈕讓它自動設定就對了。

游舒帆 (gipi)

探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。