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)
- 品牌及型號:
PC端:X
移動端:https://www.cnblogs.com/alisleepy/p/11200325.html(以下兩種結合)
1、手機型號:https://blog.csdn.net/xiawenyi1234/article/details/105990375/(安卓,IOS僅能精確到iphone)
2、獲取IOS型號:https://segmentfault.com/a/1190000010157682(IOS),僅支持以下型號(不全)
僅支持這些型號
- 處理器/芯片/CPU:X
PC:http://www.blogjava.net/redhatlinux/archive/2009/02/11/254254.html(僅IE支持)
ActiveXObject is not defined,解決辦法:https://www.cnblogs.com/webqiand/p/5948213.html(解決不了局限性)- 內存:沒看 ??
三、系統軟件信息:
類型:如“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
});
}