8.13.2014

[JS] How to detect the OS by JavaScript??

這一次的主題:如何用JavaScript來判斷使用者的作業系統??

解決這個問題的技巧,其實很簡單,就跟判斷使用者的瀏灠器一樣;我們判斷的依據,是HTTP Request表頭裡的某一項欄位:User-Agent

User-Agent主要呈現的是:
1. 瀏灠器名稱和版本號
2. 作業系統名稱和版本編號
3. 預設語言

JavaScript裡面也有提供類似的方法:window.navigator.userAgent;假如要判斷是32-Bit還是64-Bit則是用:window.navigator.platform

接下來,我們只需要寫一個很簡單的JavaScript alert(),當使用者的瀏灠器載入時,就呈現上述2項資訊,然後把它們記下來,做為判別的依據。

所以,自己這邊整理出了8種範例:

1. Microsoft Windows XP (Service Pack 3), Mozilla FireFox:
platform: Win32
userAgent: Mozilla/5.0 (Windows NT 5.1;rv:31.0) Gecko/20100101 Firefox/31.0

2. Microsoft Windows 7 (Service Pack 1), Google Chrome:
platform: Win32
userAgent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36

3. Apple MacOS X (10.9.3), Safari:
platform: MacIntel
userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML, like Gecko) Version/7.0.4 Safari/537.76.4

4. Linux Ubuntu 12.04, Mozilla FireFox:
platform: Linux i686
userAgent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0

5. Red Hat Enterprise Linux 6, Mozilla FireFox:
platform: Linux i686
userAgent: Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.9.2.9) Gecko/20100827 Red Hat/3.6.9-2.el6 Firefox/3.6.9

6. Linux Red Hat 9 WorkStation, Mozilla FireFox:
platform: Linux i686
userAgent: Mozilla/5.0 (X11; U; Linux i686; rv:1.4.2) Gecko/20040308

7. Linux Fedore Core 9, Mozilla FireFox:
platform: Linux i686
userAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008043010 Fedora/3.0-0.60.beta5.fc9 Firefox/3.0b5

8. Linux CentOS 5.8, Mozilla FireFox:
platform: Linux i686
userAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.26) Gecko/20120131 CentOS/3.6-1.el5.centos Firefox/3.6.26

有了這些資訊之後,就可以撰寫出我們想要的程式:
var platform = function() {
    var tmp = window.navigator.platform;
    var bit = 0;

    if (tmp == "Win32")
        bit = 32;
    else if (tmp == "Linux i686")
        bit = 32;
    else if (tmp == "MacIntel")
        bit = 64;
    else // other operation system keyword....
  //bit = ;

    return bit;
};

var OS = (function() {
    var retval = null;
    var strUserAgent = window.navigator.userAgent.toLowerCase();

    if (strUserAgent.match("windows nt 5.0") != null)
        retval = "Microsoft Windows 2000";
    else if (strUserAgent.match("windows nt 5.1") != null)
        retval = "Microsoft Windows XP";
    else if (strUserAgent.match("windows nt 6.0") != null)
        retval = "Microsoft Windows Vista";
    else if (strUserAgent.match("windows nt 6.1") != null)
        retval = "Microsoft Windows 7";
    else if (strUserAgent.match("windows nt 6.2") != null)
        retval = "Microsoft Windows 8";
    else if (strUserAgent.match("windows nt 6.3") != null)
        retval = "Microsoft Windows 8.1";
    else if (strUserAgent.match("ubuntu") != null)
        retval = "Ubuntu Linux";
    else if (strUserAgent.match("red hat") != null)
        retval = "Red Hat Enterprise Linux";
    else if (strUserAgent.match("x11; u; linux i686; zh-tw; rv:1.4.2") != null)
        retval = "Red Hat WorkStation 9";
    else if (strUserAgent.match("fedora") != null)
        retval = "Fedora Core Linux";
    else if (strUserAgent.match("centos") != null)
        retval = "Cent OS Linux";
    else // if (strUserAgent.match("Intel Mac OS X") != null)
        retval = "Apple Mac OS X";

    return retval;
}());
其實這份程式碼寫得非常籠統,僅能分辯出不一樣的作業系統;倘若還要再細分出該系統的版本,例如:

a). Microsoft Windows 2000, SP1, SP2, SP3, SP4, Server, Professional
b). Microsoft Windows XP SP1, SP2, SP3
c). Red Hat Enterprise Linux 3, 4, 5, 6
d). Apple MacOS X 10.9.3; 10.9.4

要解決這樣的問題,我的作法會是:
0. 先Google看看,是否有相關的資料;

假如沒有:
1. 先安裝好虛擬機器,如Virtual Machine, Virtual Box, Virtual PC
2. 準備好要辨認的作業系統;且把該系統安裝在虛擬機器上;
3. 寫一個能夠呈現User Agent的JavaScript程式碼;
4. 把JavaScript的程式碼,放進虛擬機器裡的作業系統;
5. 在虛擬機裡的OS,用Browser來執行該段JavaScript;且把該User Agent記錄下來。

假如是自己在實務上真的必須這麼做,除了navigator.platform和navigator.userAgent之外,也會順便把navigator.appNamenavigator.appVersion也記下來,以備不時之需。

沒有留言:

張貼留言