JS 客戶端應用系統運行環境采集

JS 獲取瀏覽器運行環境,系統軟件信息,產品信息,設備信息

參考:
https://blog.csdn.net/l2345432l/article/details/109735338
https://blog.csdn.net/weixin_30588729/article/details/100093460
https://www.jb51.net/article/164278.htm

代碼

// 獲取系統信息
    getSystem () {
        const ua = navigator.userAgent.toLowerCase();
        const testUa = regexp => regexp.test(ua);
        const testVs = regexp => (ua.match(regexp) + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, ".");

        // 系統
        let system = "unknown";
        if (testUa(/windows|win32|win64|wow32|wow64/ig)) {
            system = "Windows"; // window系統
        } else if (testUa(/macintosh|macintel/ig)) {
            system = "MAC OS"; // MAC OS系統
        } else if (testUa(/x11/ig)) {
            system = "Linux"; // Linux系統
        } else if (testUa(/android|adr/ig)) {
            system = "Android"; // Android系統
        } else if (testUa(/ios|iphone|ipad|ipod|iwatch/ig)) {
            system = "IOS"; // IOS系統
        }

        // 系統版本
        let systemVs = "unknown";
        if (system === "Windows") {
            if (testUa(/windows nt 5.0|windows 2000/ig)) {
                systemVs = "2000";
            } else if (testUa(/windows nt 5.1|windows xp/ig)) {
                systemVs = "xp";
            } else if (testUa(/windows nt 5.2|windows 2003/ig)) {
                systemVs = "2003";
            } else if (testUa(/windows nt 6.0|windows vista/ig)) {
                systemVs = "vista";
            } else if (testUa(/windows nt 6.1|windows 7/ig)) {
                systemVs = "7";
            } else if (testUa(/windows nt 6.2|windows 8/ig)) {
                systemVs = "8";
            } else if (testUa(/windows nt 6.3|windows 8.1/ig)) {
                systemVs = "8.1";
            } else if (testUa(/windows nt 10.0|windows 10/ig)) {
                systemVs = "10";
            }
        } else if (system === "MAC OS") {
            systemVs = testVs(/os x [\d._]+/ig);
        } else if (system === "Android") {
            systemVs = testVs(/android [\d._]+/ig);
        } else if (system === "IOS") {
            systemVs = testVs(/os [\d._]+/ig);
        }

        // 內核和載體
        let engine = "unknow";
        let supporter = "unknow";
        if (testUa(/applewebkit/ig) && testUa(/safari/ig)) {
            engine = "webkit"; // webkit內核
            if (testUa(/edge/ig)) {
                supporter = "edge"; // edge瀏覽器
            } else if (testUa(/opr/ig)) {
                supporter = "opera"; // opera瀏覽器
            } else if (testUa(/chrome/ig)) {
                supporter = "Chrome"; // Chrome瀏覽器
            } else {
                supporter = "Safari"; // Safari瀏覽器
            }
        } else if (testUa(/gecko/ig) && testUa(/firefox/ig)) {
            engine = "gecko"; // gecko內核
            supporter = "firefox"; // firefox瀏覽器
        } else if (testUa(/presto/ig)) {
            engine = "presto"; // presto內核
            supporter = "opera"; // opera瀏覽器
        } else if (testUa(/trident|compatible|msie/ig)) {
            engine = "trident"; // trident內核
            supporter = "iexplore"; // iexplore瀏覽器
        }

        // 內核版本
        let engineVs = "unknow";
        if (engine === "webkit") {
            engineVs = testVs(/applewebkit\/[\d.]+/ig);
        } else if (engine === "gecko") {
            engineVs = testVs(/gecko\/[\d.]+/ig);
        } else if (engine === "presto") {
            engineVs = testVs(/presto\/[\d.]+/ig);
        } else if (engine === "trident") {
            engineVs = testVs(/trident\/[\d.]+/ig);
        }

        // 載體版本
        let supporterVs = "unknow";
        if (supporter === "Chrome") {
            supporterVs = testVs(/chrome\/[\d.]+/ig);
        } else if (supporter === "Safari") {
            supporterVs = testVs(/version\/[\d.]+/ig);
        } else if (supporter === "firefox") {
            supporterVs = testVs(/firefox\/[\d.]+/ig);
        } else if (supporter === "opera") {
            supporterVs = testVs(/opr\/[\d.]+/ig);
        } else if (supporter === "iexplore") {
            supporterVs = testVs(/(msie [\d.]+)|(rv:[\d.]+)/ig);
        } else if (supporter === "edge") {
            supporterVs = testVs(/edge\/[\d.]+/ig);
        }

        // 外殼和外殼版本
        let shell = "none";
        let shellVs = "unknow";
        if ( (ua.match(/MicroMessenger/i) == 'micromessenger') && (ua.match(/wxwork/i) == 'wxwork') ){
            shell = "企業微信";
            shellVs = ua.match(/wxwork\/([\d.]+)/)[1]
        } else if( ua.match(/micromessenger/i) == 'micromessenger' ){
            shell = "微信";
            shellVs = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i)[1]
        // }
        // if (testUa(/micromessenger/ig)) {
            // shell = "wechat"; // 微信瀏覽器     
            // shellVs = testVs(/micromessenger\/[\d.]+/ig);
        } else if (testUa(/qqbrowser/ig)) {
            shell = "qq"; // QQ瀏覽器
            shellVs = testVs(/qqbrowser\/[\d.]+/ig);
        } else if (testUa(/ubrowser/ig)) {
            shell = "uc"; // UC瀏覽器
            shellVs = testVs(/ubrowser\/[\d.]+/ig);
        } else if (testUa(/2345explorer/ig)) {
            shell = "2345"; // 2345瀏覽器
            shellVs = testVs(/2345explorer\/[\d.]+/ig);
        } else if (testUa(/metasr/ig)) {
            shell = "sougou"; // 搜狗瀏覽器
        } else if (testUa(/lbbrowser/ig)) {
            shell = "liebao"; // 獵豹瀏覽器
        } else if (testUa(/maxthon/ig)) {
            shell = "maxthon"; // 遨游瀏覽器
            shellVs = testVs(/maxthon\/[\d.]+/ig);
        } else if (testUa(/bidubrowser/ig)) {
            shell = "baidu"; // 百度瀏覽器
            shellVs = testVs(/bidubrowser [\d.]+/ig);
        } 

        // 獲取到system、systemVs、platform、engine、engineVs、supporter、supporterVs、shell、shellVs
        return Object.assign({
            engine, // webkit gecko presto trident
            engineVs,
            supporter, // Chrome Safari firefox opera iexplore edge
            supporterVs,
            system, // Windows MAC OS Linux Android IOS
            systemVs
            }, shell === "none" ? {} : {
            shell, // wechat qq uc 2345 sougou liebao maxthon baidu
            shellVs
        });
    }

分析及了解

一、瀏覽器運行環境(瀏覽器+版本)
套殼瀏覽器 => 外殼,外殼版本,如:“企業微信”
原生瀏覽器 => 載體,載體版本,如:“Chrome”“Sarari”,“91.0.4472.106”
區分企業微信和微信:https://blog.csdn.net/hrf368246980/article/details/83055671
企業微信版本:https://www.cnblogs.com/websmile/p/13730118.html

  • 原生瀏覽器與套殼瀏覽器
    原生瀏覽器是指火狐、IE、谷歌(Chrome)、Safari、Opera等這一類擁有完整獨立內核的瀏覽器。
    所謂套殼瀏覽器,是指在某瀏覽器內核之上增加相應的輔助功能,并改變其名稱與外觀的瀏覽器。如360瀏覽器、QQ瀏覽器、搜狗瀏覽器等。
    http://www.lxweimin.com/p/67d790a8f221

二、設備信息(做不到 X)

三、系統軟件信息:
類型:如“Android”“EMUI”“MAC OS”“IOS”“Windows”
https://blog.csdn.net/l2345432l/article/details/109735338
版本:如“11.0.0”“10”
windows版本:https://blog.csdn.net/marke_huang/article/details/83094762
企業微信windows版本:https://blog.csdn.net/u012708193/article/details/110881738

四、產品信息:
類型:PC / H5 / 小程序(固定)
產品名稱:(固定)
版本:(固定,不能接口獲取)

調試代碼(部分代碼不可用,僅用于調試)

  • 引入文件
import MobileDetect from '../../static/js/mobile-detect.js';
import MobileDevice from '../../static/js/iphone-device.js';
  • 使用:
this.systemDetail = this.getSystem()
  • 方法
getSystem () {
        const ua = navigator.userAgent.toLowerCase();
        const testUa = regexp => regexp.test(ua);
        const testVs = regexp => (ua.match(regexp) + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, ".");

        // 系統
        let system = "unknown";
        if (testUa(/windows|win32|win64|wow32|wow64/ig)) {
            system = "Windows"; // window系統
        } else if (testUa(/macintosh|macintel/ig)) {
            system = "MAC OS"; // MAC OS系統
        } else if (testUa(/x11/ig)) {
            system = "Linux"; // Linux系統
        } else if (testUa(/android|adr/ig)) {
            system = "Android"; // Android系統
        } else if (testUa(/ios|iphone|ipad|ipod|iwatch/ig)) {
            system = "IOS"; // IOS系統
        }

        // 系統版本
        let systemVs = "unknown";
        if (system === "Windows") {
            // if (testUa(/windows nt 5.0|windows 2000/ig)) {
            //     systemVs = "2000";
            // } else if (testUa(/windows nt 5.1|windows xp/ig)) {
            //     systemVs = "xp";
            // } else if (testUa(/windows nt 5.2|windows 2003/ig)) {
            //     systemVs = "2003";
            // } else if (testUa(/windows nt 6.0|windows vista/ig)) {
            //     systemVs = "vista";
            // } else if (testUa(/windows nt 6.1|windows 7/ig)) {
            //     systemVs = "7";
            // } else if (testUa(/windows nt 6.2|windows 8/ig)) {
            //     systemVs = "8";
            // } else if (testUa(/windows nt 6.3|windows 8.1/ig)) {
            //     systemVs = "8.1";
            // } else if (testUa(/windows nt 10.0|windows 10/ig)) {
            //     systemVs = "10";
            // }
            systemVs = ua.substr(ua.indexOf('windows nt ') + 11,4)
        } else if (system === "MAC OS") {
            systemVs = testVs(/os x [\d._]+/ig);
        } else if (system === "Android") {
            systemVs = testVs(/android [\d._]+/ig);
        } else if (system === "IOS") {
            systemVs = testVs(/os [\d._]+/ig);
        }

        // 內核和載體
        let engine = "unknow";
        let supporter = "unknow";
        if (testUa(/applewebkit/ig) && testUa(/safari/ig)) {
            engine = "webkit"; // webkit內核
            if (testUa(/edge/ig)) {
                supporter = "edge"; // edge瀏覽器
            } else if (testUa(/opr/ig)) {
                supporter = "opera"; // opera瀏覽器
            } else if (testUa(/chrome/ig)) {
                supporter = "Chrome"; // Chrome瀏覽器
            } else {
                supporter = "Safari"; // Safari瀏覽器
            }
        } else if (testUa(/gecko/ig) && testUa(/firefox/ig)) {
            engine = "gecko"; // gecko內核
            supporter = "firefox"; // firefox瀏覽器
        } else if (testUa(/presto/ig)) {
            engine = "presto"; // presto內核
            supporter = "opera"; // opera瀏覽器
        } else if (testUa(/trident|compatible|msie/ig)) {
            engine = "trident"; // trident內核
            supporter = "iexplore"; // iexplore瀏覽器
        }

        // 內核版本
        let engineVs = "unknow";
        if (engine === "webkit") {
            engineVs = testVs(/applewebkit\/[\d.]+/ig);
        } else if (engine === "gecko") {
            engineVs = testVs(/gecko\/[\d.]+/ig);
        } else if (engine === "presto") {
            engineVs = testVs(/presto\/[\d.]+/ig);
        } else if (engine === "trident") {
            engineVs = testVs(/trident\/[\d.]+/ig);
        }

        // 載體版本
        let supporterVs = "unknow";
        if (supporter === "Chrome") {
            supporterVs = testVs(/chrome\/[\d.]+/ig);
        } else if (supporter === "Safari") {
            supporterVs = testVs(/version\/[\d.]+/ig);
        } else if (supporter === "firefox") {
            supporterVs = testVs(/firefox\/[\d.]+/ig);
        } else if (supporter === "opera") {
            supporterVs = testVs(/opr\/[\d.]+/ig);
        } else if (supporter === "iexplore") {
            supporterVs = testVs(/(msie [\d.]+)|(rv:[\d.]+)/ig);
        } else if (supporter === "edge") {
            supporterVs = testVs(/edge\/[\d.]+/ig);
        }

        // 外殼和外殼版本
        let shell = "none";
        let shellVs = "unknow";
        if ( (ua.match(/MicroMessenger/i) == 'micromessenger') && (ua.match(/wxwork/i) == 'wxwork') ){
            shell = "企業微信";
            shellVs = ua.match(/wxwork\/([\d.]+)/)[1]
        } else if( ua.match(/micromessenger/i) == 'micromessenger' ){
            shell = "微信";
            shellVs = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i)[1]
        // }
        // if (testUa(/micromessenger/ig)) {
            // shell = "wechat"; // 微信瀏覽器     
            // shellVs = testVs(/micromessenger\/[\d.]+/ig);
        } else if (testUa(/qqbrowser/ig)) {
            shell = "qq"; // QQ瀏覽器
            shellVs = testVs(/qqbrowser\/[\d.]+/ig);
        } else if (testUa(/ubrowser/ig)) {
            shell = "uc"; // UC瀏覽器
            shellVs = testVs(/ubrowser\/[\d.]+/ig);
        } else if (testUa(/2345explorer/ig)) {
            shell = "2345"; // 2345瀏覽器
            shellVs = testVs(/2345explorer\/[\d.]+/ig);
        } else if (testUa(/metasr/ig)) {
            shell = "sougou"; // 搜狗瀏覽器
        } else if (testUa(/lbbrowser/ig)) {
            shell = "liebao"; // 獵豹瀏覽器
        } else if (testUa(/maxthon/ig)) {
            shell = "maxthon"; // 遨游瀏覽器
            shellVs = testVs(/maxthon\/[\d.]+/ig);
        } else if (testUa(/bidubrowser/ig)) {
            shell = "baidu"; // 百度瀏覽器
            shellVs = testVs(/bidubrowser [\d.]+/ig);
        }

        let locator,service
        if(window.ActiveXObject){
            //支持-通過ActiveXObject的一個新實例來創建XMLHttpRequest對象            
            locator = new ActiveXObject("WbemScripting.SWbemLocator"); 
            service = locator.ConnectServer("."); 
            //CPU信息 
            var cpu = new Enumerator (service.ExecQuery("SELECT * FROM Win32_Processor")).item(); 
            var cpuType=cpu.Name,hostName=cpu.SystemName; 
            //內存信息 
            var memory = new Enumerator (service.ExecQuery("SELECT * FROM Win32_PhysicalMemory")); 
            for (var mem=[],i=0;!memory.atEnd();memory.moveNext()) mem[i++]={cap:memory.item().Capacity/1024/1024,speed:memory.item().Speed} 
            //系統信息 
            var system2=new Enumerator (service.ExecQuery("SELECT * FROM Win32_ComputerSystem")).item(); 
            var physicMenCap=Math.ceil(system2.TotalPhysicalMemory/1024/1024),curUser=system2.UserName,cpuCount=system2.NumberOfProcessors 
            console.log({cpuType:cpuType,cpuCount:cpuCount,hostName:hostName,curUser:curUser,memCap:physicMenCap,mem:mem}) 
        }            
        //不支持        
        else if(window.XMLHttpRequest){
            locator = new XMLHttpRequest()        
        }
        console.log(locator)
        console.log(service)

         // 獲取手機型號
         Array.prototype.contains = function(needle) { // 判斷數組中是否包含某字符串
            for (i in this) {
                if (this[i].indexOf(needle) > 0)
                    return i;
            }
            return -1;
        }
        var device_type = navigator.userAgent; //獲取userAgent信息
        console.log(device_type)
        var md = new MobileDetect(device_type); //初始化mobile-detect
        var os = md.os(); // 獲取系統版本
        var model = "";
        if (os == "iOS") { //ios系統的處理
            os = md.os() + md.version("iPhone");
            //再通過iphone-device.js獲取具體的蘋果手機型號
            model = MobileDevice.getModels().join(' or ');
            if(model == 'unknown'){
                model = md.mobile(); // iPhone
            }
        } else if (os == "AndroidOS") { //Android系統的處理
            os = md.os() + md.version("Android");
            var sss = device_type.split(";");
            var i = sss.contains("Build/");
            if (i > -1) {
                model = sss[i].substring(0, sss[i].indexOf("Build/"));
            }
        }
        console.log(os, model)


        // 獲取到system、systemVs、platform、engine、engineVs、supporter、supporterVs、shell、shellVs
        return Object.assign({
            engine, // webkit gecko presto trident
            engineVs,
            supporter, // Chrome Safari firefox opera iexplore edge
            supporterVs,
            system, // Windows MAC OS Linux Android IOS
            systemVs,
            model,
            device_type
            }, shell === "none" ? {} : {
            shell, // wechat qq uc 2345 sougou liebao maxthon baidu
            shellVs
        });
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,345評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,494評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,283評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,953評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,714評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,186評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,410評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,940評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,776評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,976評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,210評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,642評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,878評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,654評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,958評論 2 373

推薦閱讀更多精彩內容