浏览器通过JS 获取手机型号和指纹

2024-4-29 16:08:09 [大杂烩 - PC与手机]

在浏览器上通过JS在客户端判断浏览器的版本、设备的型号会经常用到,如有针对性的设计界面、兼容性处理等,还有时候需要通过获取设备信息来识别用户。当然现在的浏览器从安全考虑,提供的信息是越来越严了。具体不作解释,直接上代码。

一、获取浏览器版本

function getBrowser() {
if (Browser) return Browser;
userAgent = userAgent?userAgent:navigator.userAgent;
//name,keyword,vesion
let BrowserEnum= {
IE: ["Internet Explorer", "MSIETrident", "(MSIE \\d+\\.\\d+)(rv:\\d+\\.\\d+)"],
OPERA: ["Opera", "OPR", "OPR\\/\\d+\\.\\d+\\.\\d+\\.\\d+"],
SOGOU: ["搜狗浏览器", "SogouMobileBrowserMetaSr", "SogouMobileBrowser\\/\\d+\\.\\d+\\.\\d+"], // 手机搜狗浏览器
WX: ["微信浏览器", "MicroMessenger","MicroMessenger\\/[\\d\\.]+"],
BAIDU: ["百度浏览器", "baiduboxapp", "baiduboxapp\\/\\d+\\.\\d+\\.\\d+\\.\\d+"],
QQ: ["QQ浏览器", "QQBrowser", "QQBrowser\\/\\d+\\.\\d+(\\.\\d+\\.\\d+)?"],
_360: ["360极速浏览器", "360 Alitephone Browser", "360 Alitephone Browser \\(\\d+\\.\\d+\\.\\d+\\.\\d+\\/\\d+\\.\\d+\\.\\d+\\.\\d+"], // 手机360极速浏览器
UC: ["UC浏览器", "UC?Browser", "UC?Browser\\/\\d+\\.\\d+\\.\\d+\\.\\d+"],
MAXTHON: ["遨游浏览器", "Maxthon", "Maxthon\\/\\d+"], // 手机遨游浏览器
LIEBAO: ["猎豹浏览器", "LieBao", "LieBaoFast\\/\\d+\\.\\d+\\.\\d+"],
THEWORLD: ["世界之窗浏览器", "TheWorld", "TheWorld \\d+"],
QUARK: ["夸克", "Quark", "Quark\\/\\d+\\.\\d+\\.\\d+\\.\\d+"],
HUAWEI: ["华为浏览器", "HuaweiBrowser", "HuaweiBrowser\\/\\d+\\.\\d+\\.\\d+\\.\\d+"],
LE: ["绿茶浏览器", "LeBrowser", "LeBrowser\\/\\d+\\.\\d+\\.\\d+"], // 联想手机浏览器
MIUI: ["Miui浏览器", "MiuiBrowser", "MiuiBrowser\\/\\d+\\.\\d+\\.\\d+"], // 小米手机浏览器
FIREFOX: ["Firefox", "Firefox", "Firefox\\/\\d+\\.\\d+"],
EDGE: ["Edge", "Edg", "Edg\\/\\d+\\.\\d+\\.\\d+\\.\\d+"],
CHROME: ["Chrome", "Chrome", "Chrome\\/\\d+\\.\\d+\\.\\d+\\.\\d+"], // 也可能是360,PC搜狗,PC遨游浏览器
SAFARI: ["Safari", "Safari", "Safari\\/\\d+\\.\\d+(\\.\\d+)?"],
};

let be,version;
for(let x in BrowserEnum) {
let re=new RegExp(BrowserEnum[x][1],"gi");
if (re.test(userAgent)) {
be=x;
break;
}
}
if (be) {
if (BrowserEnum[be][2])
{
version = userAgent.match(new RegExp(BrowserEnum[be][2],"i"));
version=version?version[0].split(/[\s\:\/]/)[1]:"";
}
Browser = {name: be,desc: BrowserEnum[be][0]+" "+(version?version:""),version: version?version:"" };
} else {
Browser = {name:"UNKNOWN",desc:"未知",version:""};
}
return Browser;
}


二、获得手机型号

(苹果在版本11以后,就不在userAgent里放CPU型号了。)

function getModel() {
if (Model) return Model;
userAgent = userAgent?userAgent:navigator.userAgent;
//name,keyword Hw=HUAWEI Ss=SAMSUNG,Hn=HONOR,Rm=REALME
let ModelEnum = {
IPad:["iPad", "iPad"],
IPod:["iPod", "iPod"],
IPhone:["iPhone", "iPhone"],
Hn7:["荣耀7", "PLK-AL10"],
Hn8:["荣耀8", "FRD-AL10"],
Hn10QCB:["荣耀10 青春版", "HRY-AL00"],
Hn30Pro:["荣耀30 Pro", "EBG-AN00"],
Hn30Prop:["荣耀30 Pro+", "EBG-AN10"],
Hn50:["荣耀50", "NTH-AN00"], 
Hn50Pro:["荣耀50 Pro", "RNA-AN00"],
Hn50SE:["荣耀50 SE", "JLH-AN00"],
Hn60:["荣耀60", "LSA-AN00"],
Hn60Pro:["荣耀60 Pro", "TNA-AN00"],
Hn60SE:["荣耀60 SE", "GIA-AN00"],
Hn70:["荣耀70", "FNE-AN00"],
Hn70Pro:["荣耀70 Pro", "SDY-AN00"],
HnMAGIC3:["荣耀 Magic3", "ELZ-AN00"],
HnMAGIC3Pro:["荣耀 Magic3 Pro", "ELZ-AN10"],
HnMAGIC3ZZB:["荣耀 Magic3 至臻版", "ELZ-AN20"],
HnMAGICV:["荣耀 Magic V", "MGI-AN00"],
HnMAGIC4:["荣耀 Magic4", "LGE-AN00"],
HnMAGIC4Pro:["荣耀 Magic4 Pro", "LGE-AN10"],
HnX10:["荣耀 X10", "TEL-AN00a"],
HnX20SE:["荣耀 X20 SE", "CHL-AN00"],
HnX20:["荣耀 X20", "NTN-AN20"],
HnX30I:["荣耀 X30i", "TFY-AN00"],
HnX30MAX:["荣耀 X30 Max", "KKG-AN70"],
HnX30:["荣耀 X30", "ADY-AN00"],
HnCW20:["荣耀 畅玩20", "KOZ-AL00"],
HnPLAY5THLB:["荣耀 Play5T 活力版", "NZA-AL00"],
HnPLAY5:["荣耀 Play5", "HJC-AN90"],
HnPLAY5T:["荣耀 Play5T", "KOZ-AL40"],
HnPLAY5TPro:["荣耀 Play5T Pro", "CHL-AL00"],
HnPLAY5HLB:["荣耀 Play5 活力版", "NEW-AN90"],
HnCW30p:["荣耀 畅玩30 Plus", "CMA-AN00"],
HnPLAY6TPro:["荣耀 Play6T Pro", "TFY-AN40"],
HnPLAY6T:["荣耀 Play6T", "CMA-AN40"],
HnV10:["荣耀 V10", "BKL-AL20"],
HnV30Pro:["荣耀 V30 Pro", "OXF-AN10"],
HnV40QSB:["荣耀 V40 轻奢版", "ALA-AN70"],
HwP20:["华为 P20", "EML-AL00"],
HwP20Pro:["华为 P20 Pro", "CLT-AL01"],
HwP30:["华为 P30", "ELE-AL00"],
HwP30Pro:["华为 P30 Pro", "VOG-AL00"],
HwP30Proz:["华为 P30 Pro 尊享版", "VOG-AL10"],
HwP40:["华为 P40", "ANA-AL00"],
HwP40Pro:["华为 P40 Pro", "ELS-TN00"],
HwP40Prop:["华为 P40 Pro+", "ELS-AN10"],
HwP50Pro:["华为 P50 Pro", "JAD-AL50"],
HwP50:["华为 P50", "ABR-AL00"],
HwP50POCKET:["华为 P50 Pocket", "BAL-AL00"],
HwP50E:["华为 P50E", "ABR-AL60"],
HwPOCKETS:["华为 POCKET S", "BAL-AL60"],
HwP70ULT:["华为 Pura 70 Ultra", "HBP-AL00"],
HwP70Prop:["华为 Pura 70 Pro+", "HBN-AL10"],
HwP70Pro:["华为 Pura 70 Pro", "HBN-AL00"],
HwP70:["华为 Pura 70", "ADY-AL00"],
HwP60Pro:["华为 P60 Pro", "MNA-AL00"],
HwP60:["华为 P60", "LNA-AL00"],

HwMATE20:["华为 Mate 20", "HMA-AL00"],
HwMATE30:["华为 Mate 30", "TAS-AL00"],
HwMATE30Pro:["华为 Mate 30 Pro", "LIO-AN00"],
HwMATE30EPro:["华为 Mate 30E Pro", "LIO-AN00m"],
HwMATE40Pro:["华为 Mate 40 Pro", "NOH-AL00"],
HwMATE40RS:["华为 Mate 40 RS 保时捷设计 典藏版", "NOP-AN00"],
HwMATE40:["华为 Mate 40", "OCE-AN10"],
HwMATEX2:["华为 Mate X2", "TET-AN00"],
HwMATE40E:["华为 Mate 40E", "OCE-AL50"],
HwMATEXS2:["华为 Mate Xs 2", "PAL-AL00"],
// HwMATE50:["华为 Mate 50", "EVA-AL10"],
HwMATE50:["华为 Mate 50", "CET-AL00"],
HwMATE50RPO:["华为 Mate 50 Pro", "DCO-AL00"],
HwMATE50RS:["华为 Mate 50RS", "EVA-AL00"],
HwMATEX5:["华为 Mate X5", "ALT-AL10"],
HwMATE60RS:["华为 Mate 60 RS", "ALN-AL10"],
HwMATE60Prop:["华为 Mate 60 Pro+", "ALN-AL10"],
HwMATE60:["华为 Mate 60", "BRA-AL00"],
HwMATE60Pro:["华为 Mate 60 Pro", "ALN-AL00"],
HwMATEX3:["华为 Mate X3", "ALT-AL00"],
HwMATE50E:["华为 Mate 50E", "CET-AL60"],
HwMATEXS2:["华为 Mate Xs 2", "PAL-AL00"],

HwNOVA2S:["华为 nova 2S", "HWI-AL00"],
HwNOVA4:["华为 nova 4", "VCE-AL00"],
HwNOVA5Pro:["华为 nova 5 Pro", "SEA-AL10"],
HwNOVA5Z:["华为 nova 5z", "SPN-AL00"],
HwNOVA7:["华为 nova 7", "JEF-AN00"],
HwNOVA7Pro:["华为 nova 7 Pro", "JER-AN10"],
HwNOVA8SE:["华为 nova 8 SE", "JSC-AL50"],
HwNOVA8:["华为 nova 8", "ANG-AN00"],
HwNOVA8Pro:["华为 nova 8 Pro", "BRQ-AN00"],
HwNOVA8SEh:["华为 nova 8 SE 活力版", "CHL-AL60"],
HwNOVA9:["华为 nova 9", "NAM-AL00"],
HwNOVA9Pro:["华为 nova 9 Pro", "RTE-AL00"],
HwNOVA9SE:["华为 nova 9 SE", "JLN-AL00"],
HwCX9:["华为 畅享9", "DUB-AL00"],
HwCX10p:["华为 畅享10 Plus", "STK-AL00"],
HwCX10:["华为 畅享10", "ART-AL00x"],
HwCX10E:["华为 畅享10e", "MED-AL00"],
HwCX20Pro:["华为 畅享20 Pro", "DVC-AN20"],
HwCX20:["华为 畅享20", "WKG-AN00"],
HwCX20p:["华为 畅享20 Plus", "FRL-AN00a"],
HwCX20E:["华为 畅享20e", "MLD-AL10"],
HwCX50:["华为 畅享50", "MGA-AL00"],
HwMM8:["华为 麦芒8", "POT-AL10"],
VIVOX27:["vivo X27", "V1829A"],
VIVOX27Pro:["vivo X27 Pro", "V1836A"],
VIVOX50:["vivo X50", "V2001A"],
VIVOX50Prop:["vivo X50 Pro+", "V2011A"],
VIVOX60:["vivo X60", "V2046A"],
VIVOX60Pro:["vivo X60 Pro", "V2047A"],
VIVOX60Prop:["vivo X60 Pro+/X60t Pro+", "V2056A"],
VIVOX60qB:["vivo X60 曲屏版", "V2059A"],
VIVOX70T:["vivo X70t", "V2132A"],
VIVOX70:["vivo X70", "V2133A"],
VIVOX70Pro:["vivo X70 Pro", "V2134A"],
VIVOX70Prop:["vivo X70 Pro+", "V2145A"],
VIVOX80:["vivo X80", "V2183A"],
VIVOX80Pro:["vivo X80 Pro", "V2185A"],
VIVOS5:["vivo S5", "V1932A"],
VIVOS7E:["vivo S7e/S7e 活力版", "V2031EA"],
VIVOS9E:["vivo S9e", "V2048A"],
VIVOS9:["vivo S9", "V2072A"],
VIVOS10:["vivo S10/S10 Pro", "V2121A"],
VIVOS10E:["vivo S10e", "V2130A"],
VIVOS12:["vivo S12", "V2162A"],
VIVOS12Pro:["vivo S12 Pro", "V2163A"],
VIVOS15E:["vivo S15e", "V2190A"],
VIVOS15:["vivo S15", "V2203A"],
VIVOS15Pro:["vivo S15 Pro", "V2207A"],
VIVOT1:["vivo T1", "V2115A"],
VIVOT1X:["vivo T1x/Y53s(t2版)", "V2123A"],
VIVOY3:["vivo Y3/Y3s", "V1901A"],
VIVOY3BZB:["vivo Y3 标准版", "V1930A"],
VIVOY5S:["vivo Y5s", "V1934A"],
VIVOY9S:["vivo Y9s", "V1945A"],
VIVOY50:["vivo Y50", "V1965A"],
VIVOY70S:["vivo Y70s/Y70t", "V2002A"],
VIVOY50T:["vivo Y50t", "V2023EA"],
VIVOY30:["vivo Y30", "V2034A"],
VIVOY30BZB:["vivo Y30 标准版", "V2036A"],
VIVOY31S:["vivo Y31s/Y52s(t1版)", "V2054A"],
VIVOY52S:["vivo Y52s", "V2057A"],
VIVOY30G:["vivo Y30g", "V2066BA"],
VIVOY31SBZB:["vivo Y31s 标准版/Y31s(t1版)", "V2068A"],
VIVOY71T:["vivo Y71t", "V2102A"],
VIVOY53S:["vivo Y53s", "V2111A"],
VIVOY76S:["vivo Y76s", "V2156A"],
VIVOY32:["vivo Y32", "V2158A"],
VIVOY55S:["vivo Y55s", "V2164A"],
VIVOY33S:["vivo Y33s", "V2166A"],
VIVOY10:["vivo Y10(t1版)", "V2168A"],
VIVOZ5X:["vivo Z5x", "V1911A"],
VIVOZ5I:["vivo Z5i", "V1941A"],
VIVOXFOLD:["vivo X Fold", "V2178A"],
VIVOXNOTE:["vivo X Note", "V2170A"],
SsGZFLIP:["三星 Galaxy Z Flip", "SM-F7070"],
SsGZFLIP3:["三星 Galaxy Z Flip3", "SM-F7110"],
SsGZFOLD2:["三星 Galaxy Z Fold2", "SM-F9160"],
SsGZFOLD3:["三星 Galaxy Z Fold3", "SM-F9260"],
SsGS20FE:["三星 Galaxy S20 FE", "SM-G7810"],
SsGS5:["三星 Galaxy S5", "SM-G900P"],
SsGS8p:["三星 Galaxy S8+", "SM-G955U"],
SsGS20:["三星 Galaxy S20", "SM-G981B"],
SsGS20U:["三星 Galaxy S20 Ultra", "SM-G9880"],
SsGS21FE:["三星 Galaxy S21 FE", "SM-G9900"],
SsGS21:["三星 Galaxy S21", "SM-G9910"],
SsGS21p:["三星 Galaxy S21+", "SM-G9960"],
SsGS21U:["三星 Galaxy S21 Ultra", "SM-G9980"],
SsGS22:["三星 Galaxy S22", "SM-S9010"],
SsGS22p:["三星 Galaxy S22+", "SM-S9060"],
SsGS22U:["三星 Galaxy S22 Ultra", "SM-S9080"],
SsGN2:["三星 Galaxy Note2", "GT-N7100"],
SsGN20:["三星 Galaxy Note20", "SM-N9810"],
SsGN20U:["三星 Galaxy Note20 Ultra", "SM-N9860"],
SsGA5:["三星 Galaxy A5", "SM-A5000"],
SsGA51:["三星 Galaxy A51", "SM-A5160"],
SsGA52:["三星 Galaxy A52", "SM-A5260"],
SsGA53:["三星 Galaxy A53", "SM-A5360"],
SsGA71:["三星 Galaxy A71", "SM-A7160"],
SsGA90:["三星 Galaxy A90", "SM-A9080"],
SsGF52:["三星 Galaxy F52", "SM-E5260"],
SsW21:["三星 W21", "SM-W2021"],
SsW22:["三星 W22", "SM-W2022"],
RmGT:["真我 GT", "RMX2202"],
RmGT2Pro:["真我 GT2 Pro", "RMX3300"],
RmGT2:["真我 GT2", "RMX3310"],
RmGTd:["真我 GT 大师版", "RMX3361"],
RmGTt:["真我 GT 大师探索版", "RMX3366"],
RmGTNEO:["真我 GT Neo", "RMX3031"],
RmGTNeos:["真我 GT Neo 闪速版", "RMX3350"],
RmGTNeo2T:["真我 GT Neo2T", "RMX3357"],
RmGTNEO2:["真我 GT Neo2", "RMX3370"],
RmGTNEO3:["真我 GT Neo3", "RMX3560"],
RmGTNEO3h:["真我 GT Neo3 火影限定版", "RMX3562"],
RmQ5Pro:["真我 Q5 Pro", "RMX3372"],
RmQ3S:["真我 Q3s", "RMX3461"],
RmQ5:["真我 Q5", "RMX3478"],
RmQ5I:["真我 Q5i", "RMX3574"],
RmV15:["真我 V15", "RMX3092"],
RmV25:["真我 V25", "RMX3475"],
RmV23:["真我 V23", "RMX3571"],
MINOTE2:["小米 Note 2", "Mi Note 2"],
MIPAD2:["小米PAD2", "Tablet"],
MI6:["小米6", "MI 6"],
MI8:["小米8", "MI 8"],
MI10:["小米10", "Mi 10"],
MI10z:["小米10 至尊纪念版", "M2007J1SC"],
REDMIK20Pro:["Redmi K20 Pro", "Redmi K20 Pro Build"],
REDMIK20Proz:["Redmi K20 Pro 尊享版", "Redmi K20 Pro Premium Edition"],
LENOVOA850p:["联想 A850+", "Lenovo A850+"],
LENOVOZ6Pro:["联想 Z6 Pro", "Lenovo L78051"],
IPLAY40:["酷比魔方 iPlay40", "iPlay40"],
_360N6:["360 N6", "1707-A01"],
UROVOI9000S:["优博讯 i9000S", "i9000S"],
BBPLAYBOOK:["黑莓PlayBook", "PlayBook"],
BB10:["黑莓手机", "BB10"]
};


let moEnum;
for(let x in ModelEnum) {
let re=new RegExp(ModelEnum[x][1],"i");
if (re.test(userAgent)) {
moEnum=x;
break;
}
}
if (userAgent.search(/(iPhoneiPodiPad)/i)>=0) {
Model= getAppleModel(moEnum);
} else {
if (moEnum) {
Model= {name:moEnum,desc:ModelEnum[moEnum][0],model:ModelEnum[moEnum][1] };
} else {
Model= {name:getOS().name,desc:getOS().desc,model:getOS().type};
}
}
return Model;

function getAppleModel(name) {
let iPhoneDev = [
['iPh5','a7', '640x1136', ['iPhone 5', 'iPhone 5s']],
['iPadAir','a7', '1536x2048', ['iPad Air', 'iPad Mini 2', 'iPad Mini 3']],
['iPodtouch','a8', '640x1136', ['iPod touch (6th gen)']],
['iPh6','a8', '750x1334', ['iPhone 6']],
['iPh5p','a8', '1242x2208', ['iPhone 6 Plus']],
['iPadAir2','a8', '1536x2048', ['iPad Air 2', 'iPad Mini 4']],
['iPhSE','a9', '640x1136', ['iPhone SE']],
['iPh6s','a9', '750x1334', ['iPhone 6s']],
['iPh6sp','a9', '1242x2208', ['iPhone 6s Plus']],
['iPadPro9','a9x', '1536x2048', ['iPad Pro (1st gen 9.7-inch)']],
['iPadPro12','a9x', '2048x2732', ['iPad Pro (1st gen 12.9-inch)']],
['iPh7','a10', '750x1334', ['iPhone 7']],
['iPh7p','a10', '1242x2208', ['iPhone 7 Plus']],
['iPadPro210','a10x', '1668x2224', ['iPad Pro (2th gen 10.5-inch)']],
['iPadPro212','a10x', '2048x2732', ['iPad Pro (2th gen 12.9-inch)']],
['iPh8','a11', '750x1334', ['iPhone 8']],
['iPh8p','a11', '1242x2208', ['iPhone 8 Plus']],
['iPhX','a11', '1125x2436', ['iPhone X']],
['iPhXr','a12', '828x1792', ['iPhone Xr']],
['iPhXs','a12', '1125x2436', ['iPhone Xs']],
['iPhXsM','a12', '1242x2688', ['iPhone Xs Max']],
['iPh11','a13', '828x1792', ['iPhone 11']],
['iPh12','a14', '1170x2532', ['iPhone 12']],//内存2G
['iPh13','a15', '1170×2532', ['iPhone 13']],//内存4G
['iPh14','a15', '1170×2532', ['iPhone 14']],//内存6G
['iPh15','a16', '1179x2556', ['iPhone 15']],
['iPadPro311','a12x', '1668x2388', ['iPad Pro (3rd gen 11-inch)']],
['iPadPro312', '2048x2732', ['iPad Pro (3rd gen 12.9-inch)']]
];
var gpu = getGPU(),
matches = gpu.match(/^apple\s+([_a-z0-9-]+)\s+gpu$/i),
res = getResolution(),
desc= name,
model="";
if (matches) {
for (var i = 0; i < iPhoneDev.length; i ++) {
var device = iPhoneDev[i];
if (matches[1].toLowerCase() == device[1] && res == device[2]) {
desc = device[3].join(","),
model = device[1]+"_"+device[2],
name=device[0];
break;
}
}
} else {
name="iPh13",
desc="iPhone 13",
model="13_"+res;

}
return {name:name,desc:desc,model:model};

function getResolution() {
var ratio = window.devicePixelRatio 1;
return (Math.min(screen.width, screen.height) * ratio)
+ 'x' + (Math.max(screen.width, screen.height) * ratio);
}
}
}

三、获取操作系统类型

function getOS() {
if (OS) return OS;
userAgent = userAgent?userAgent:navigator.userAgent;
let OSEnum = {
WINNT:["Windows NT", "PC"],
WIN2000:["Windows 2000", "PC"],
WINXP:["Windows XP", "PC"],
WIN2003:["Windows Server 2003", "PC"],
WINVISTA:["Windows Vista", "PC"],
WIN7:["Windows 7", "PC"],
WIN8:["Windows 8", "PC"],
WIN8_1:["Windows 8.1", "PC"],
WIN10:["Windows 10", "PC"],
WINME:["Windows ME", "PC"],
LINUX:["Linux", "PC"],
WIN:["Windows", "PC", "Win(?:dows )?([^do]{2})\\s?(\\d+\\.\\d+)?"],
MAC:["Mac OS X", "PC", "X [\\d_\\.]+"], //Mac OS X
WINCE:["Windows CE", "MOB"],
WINPHONE:["Windows Phone", "MOB", "OS [\\d\\.]+"], //Windows Phone OS
IOS:["iOS", "MOB", "OS \\d+_[\\d_\\.]+"], //CPU IPhone OS
ANDROID:["Android", "MOB", "Android [\\d\\.]+"],
BB10:["BlackBerry 10 OS", "MOB", "Version\\/[\\d\\.]+"],
BBT:["BlackBerry Tablet OS", "MOB", "OS [\\d\\.]+"], //RIM Tablet OS
UNKNOWN:["UNKNOWN",""]
};
 
let name,desc,version,type;

if (userAgent.search(/(iPhoneiPodiPad)/i)>=0) {
name = "IOS";
version = getVersion(userAgent, OSEnum[name]);
version = version?version.replace(/_/g, "."):"2";
desc = OSEnum[name][0]+" "+version;
} else if (userAgent.search(/Android/i)>=0) {
name = "ANDROID";
version = getVersion(userAgent, OSEnum[name]);
let vs=version?version.split("."):null;
desc = OSEnum[name][0]+(version?" " + vs[0] + (vs.length > 1 ?"." + vs[1]:""):"");
} else if (userAgent.search(/Mac OS X/i)>=0) {
name = "MAC";
version = getVersion(userAgent,OSEnum[name]);
version = version?version.replace(/_/g, "."):"2";
desc = OSEnum[name][0]+" "+version;
} else if (userAgent.search(/(WindowsWin)/i)>=0) {
name = "WIN";
version = userAgent.match(new RegExp(OSEnum[name][2],"i"));
version=version?version[0].split(/[\s\:\/]/):"";
if (version) {
if (version.length == 1) {
version = version[0].substring(3);
desc = OSEnum[name][0]  + " " + version;
} else {
if ("NT"==version[1]) {
version = version[2];
switch (version) {
case "5.0":
name = "WIN2000";
break;
case "5.1":
name = "WINXP";
break;
case "5.2":
name = "WIN2003";
break;
case "6.0":
name = "WINVISTA";
break;
case "6.1":
name = "WIN7";
break;
case "6.2":
name = "WIN8";
break;
case "6.3":
name = "WIN8_1";
break;
case "10.0":
name = "WIN10";
break;
default:
name = "WINNT";
break;
}
desc = OSEnum[name][0] ;
} else if ("9x"==version[1]) {
name = "WINME";
version = "9x";
desc = OSEnum[name][0] ;
} else if ("CE"==version[1]) {
name = "WINCE";
desc = OSEnum[name][0] ;
} else if ("Ph"==version[1]) {
name = "WINPHONE";
version = getVersion(userAgent, OSEnum[name]);
desc= OSEnum[name][0] + (version?" " + version:"");

} else {
desc= OSEnum[name][0]+ " " + version[1];
}
}
}
} else if (userAgent.search(/RIM Tablet OS/i)>=0) {
name = "BBT";
version = getVersion(userAgent, ose);
desc = OSEnum[name][0] + " " + version;
} else if (userAgent.search(/BB10/i)>=0) {
name = "BB10";
version = getVersion(userAgent, ose);
desc = OSEnum[name][0] + " " + version;
} else if (userAgent.search(/Linux/i)>=0) {
name = "LINUX";
if (userAgent.search(/Ubuntu/i)>=0) {
version = "Ubuntu";
} else if (userAgent.search(/UOS/i)>=0) {
version = "UOS";
}
desc = OSEnum[name][0] + " " + version;
} else {
name = "UNKNOWN";
desc = "未知";

}

desc=desc?desc:name+" "+version;
OS={name:name,desc:desc,version:version,type:OSEnum[name][1]};
return OS;

function getVersion(userAgent, ose) {
version = userAgent.match(new RegExp(ose[2]));
version=version?version[0].split(/[\s\:\/]/)[1]:"";//length-1
return version;
}
}


四、通过canvas取得GPU型号

let canvas = document.createElement('canvas');
let gl = canvas.getContext('experimental-webgl');
let debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
let GPU = debugInfo == null ? 'unknown' : gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);



当然,从指纹考虑,还有一些浏览器提供了部分设备信息,但不同浏览器及不同的版本,这些信息有的可用、有的不可用。

如:

CPU线程:navigator.hardwareConcurrency navigator.cpuClass "x",
Memory内存大小 :navigator.deviceMemory?navigator.deviceMemory:"x",
Platform平台:navigator.platform,
Resolution分辨率: [screen.width, screen.height,window.devicePixelRatio ]

小荷作文网原创,有问题请联系 xhzww@126.com