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浏览器、搜狗浏览器等。
https://www.jianshu.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
});
}