UAParser.js (英语)
JavaScript 库可以检测浏览器,引擎,OS,CPU,以及设备类型/型号,来自脚印相对较小的用户代理数据(~17KB minification,~6KB gzipped),可用于浏览器(客户端-侧)或节点.js(服务器-侧).
- 作者: Faisal Salman <[email protected] (英语)> 翻译:
- 发音: https://faisalman.github.io/ua-parser-js 页面存档备份,存于互联网档案馆
- 资料来源: https://github.com/faisalman/ua-parser-js (中文(简体) )
文档
UAParser([用户代理][,扩展])
类型 user-agent "弦"(弦声).
类型 extensions "阵列".
在浏览器环境中,您不需要将用户代理字符串传递给函数,您只需调用该游戏,它应该自动从该游戏中获取该字符串 window.navigator.userAgent但节点中的情况并非如此。 用户代理字符串必须在节点中传递,才能使函数工作.
通常您可以在 :
request.headers["user-agent"]。 。 。 .
构造器
当你打电话 UAParser 与 new 关键词 UAParser 将返回带有空结果对象的新实例,您必须调用可用的方法之一才能从用户代理字符串中获取信息.
像这样:
new UAParser([uastring][,extensions])
let parser = new UAParser("user-agent"); // you need to pass the user-agent for nodejs
console.log(parser); // {}
let parserResults = parser.getResult();
console.log(parserResults);
/** {
"ua": "",
"browser": {},
"engine": {},
"os": {},
"device": {},
"cpu": {}
} */
当您拨打 UAParser 没有 new 关键字,它会自动调用 getResult() 函数并返回解析结果.
UAParser([uastring][,extensions])- 返回结果对象
{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }
- 返回结果对象
方法
方法表
这些方法不言自明,下面是所有可用方法的小概述:
-
getResult()- 返回所有函数对象呼叫、用户代理字符串、浏览器信息、cpu、设备、引擎、 os:{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }。 。 。 . -
getBrowser()- 返回浏览器名称和版本. -
getDevice()- 返回设备模型,类型,供应商. -
getEngine()- 返回当前浏览器引擎名称和版本. -
getOS()- 返回运行中的操作系统名称和版本. -
getCPU()- 返回 CPU 建筑设计名称. -
getUA()- 返回用户代理字符串. -
setUA(user-agent)- 设置一个自定义的用户代理来解析.
-
getResult()- 返回时
{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }
- 返回时
-
getBrowser()- 返回时
{ name: '', version: '' }
- 返回时
# Possible 'browser.name':
2345Explorer, 360 Browser, Amaya, Android Browser, Arora, Avant, Avast, AVG,
BIDUBrowser, Baidu, Basilisk, Blazer, Bolt, Brave, Bowser, Camino, Chimera,
Chrome Headless, Chrome WebView, Chrome, Chromium, Comodo Dragon, Dillo,
Dolphin, Doris, DuckDuckGo, Edge, Electron, Epiphany, Facebook, Falkon, Fennec,
Firebird, Firefox [Focus/Reality], Flock, Flow, GSA, GoBrowser, Huawei Browser,
ICE Browser, IE, IEMobile, IceApe, IceCat, IceDragon, Iceweasel, Instagram,
Iridium, Iron, Jasmine, K-Meleon, Kindle, Klar, Konqueror, LBBROWSER, Line,
LinkedIn, Links, Lunascape, Lynx, MIUI Browser, Maemo Browser, Maemo, Maxthon,
MetaSr Midori, Minimo, Mobile Safari, Mosaic, Mozilla, NetFront, NetSurf, Netfront,
Netscape, NokiaBrowser, Obigo, Oculus Browser, OmniWeb, Opera Coast,
Opera [Mini/Mobi/Tablet], PaleMoon, PhantomJS, Phoenix, Polaris, Puffin, QQ,
QQBrowser, QQBrowserLite, Quark, QupZilla, RockMelt, Safari, Sailfish Browser,
Samsung Browser, SeaMonkey, Silk, Skyfire, Sleipnir, Slim, SlimBrowser, Swiftfox,
Tesla, Tizen Browser, UCBrowser, UP.Browser, Vivaldi, Waterfox, WeChat, Weibo,
Yandex, baidu, iCab, w3m, Whale Browser...
# 'browser.version' determined dynamically
getDevice()- 返回时
{ model: '', type: '', vendor: '' }
- 返回时
# Possible 'device.type':
console, mobile, tablet, smarttv, wearable, embedded
##########
# NOTE: 'desktop' is not a possible device type.
# UAParser only reports info directly available from the UA string, which is not the case for 'desktop' device type.
# If you wish to detect desktop devices, you must handle the needed logic yourself.
# You can read more about it in this issue: https://github.com/faisalman/ua-parser-js/issues/182
##########
# Possible 'device.vendor':
Acer, Alcatel, Amazon, Apple, Archos, ASUS, AT&T, BenQ, BlackBerry, Dell,
Essential, Fairphone, GeeksPhone, Google, HP, HTC, Huawei, Jolla, Lenovo, LG,
Meizu, Microsoft, Motorola, Nexian, Nintendo, Nokia, Nvidia, OnePlus, OPPO, Ouya,
Palm, Panasonic, Pebble, Polytron, Realme, RIM, Roku, Samsung, Sharp, Siemens,
Sony[Ericsson], Sprint, Tesla, Vivo, Vodafone, Xbox, Xiaomi, Zebra, ZTE, ...
# 'device.model' determined dynamically
getEngine()- 返回时
{ name: '', version: '' }
- 返回时
# Possible 'engine.name'
Amaya, Blink, EdgeHTML, Flow, Gecko, Goanna, iCab, KHTML, Links, Lynx, NetFront,
NetSurf, Presto, Tasman, Trident, w3m, WebKit
# 'engine.version' determined dynamically
getOS()- 返回时
{ name: '', version: '' }
- 返回时
# Possible 'os.name'
AIX, Amiga OS, Android[-x86], Arch, Bada, BeOS, BlackBerry, CentOS, Chromium OS,
Contiki, Fedora, Firefox OS, FreeBSD, Debian, Deepin, DragonFly, elementary OS,
Fuchsia, Gentoo, GhostBSD, GNU, Haiku, HarmonyOS, HP-UX, Hurd, iOS, Joli, KaiOS,
Linpus, Linspire,Linux, Mac OS, Maemo, Mageia, Mandriva, Manjaro, MeeGo, Minix,
Mint, Morph OS, NetBSD, Nintendo, OpenBSD, OpenVMS, OS/2, Palm, PC-BSD, PCLinuxOS,
Plan9, PlayStation, QNX, Raspbian, RedHat, RIM Tablet OS, RISC OS, Sabayon,
Sailfish, Series40, Slackware, Solaris, SUSE, Symbian, Tizen, Ubuntu, Unix,
VectorLinux, WebOS, Windows [Phone/Mobile], Zenwalk, ...
# 'os.version' determined dynamically
getCPU()- 返回时
{ architecture: '' }
- 返回时
# Possible 'cpu.architecture'
68k, amd64, arm[64/hf], avr, ia[32/64], irix[64], mips[64], pa-risc, ppc, sparc[64]
-
getUA()- 返回当前实例的 UA 字符串
-
setUA(uastring)- 设置要解析的 UA 字符串
- 返回当前实例
使用量
使用 HTML
<!doctype html>
<html>
<head>
<script src="ua-parser.min.js"></script>
<script>
var parser = new UAParser();
console.log(parser.getResult());
/*
/// This will print an object structured like this:
{
ua: "",
browser: {
name: "",
version: "",
major: "" //@deprecated
},
engine: {
name: "",
version: ""
},
os: {
name: "",
version: ""
},
device: {
model: "",
type: "",
vendor: ""
},
cpu: {
architecture: ""
}
}
*/
// Default result depends on current window.navigator.userAgent value
// Now let's try a custom user-agent string as an example
var uastring1 = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";
parser.setUA(uastring1);
var result = parser.getResult();
// You can also use UAParser constructor directly without having to create an instance:
// var result = UAParser(uastring1);
console.log(result.browser); // {name: "Chromium", version: "15.0.874.106"}
console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
console.log(result.os.version); // "11.10"
console.log(result.engine.name); // "WebKit"
console.log(result.cpu.architecture); // "amd64"
// Do some other tests
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
console.log(parser.setUA(uastring2).getBrowser().name); // "Konqueror"
console.log(parser.getOS()); // {name: "OpenBSD", version: undefined}
console.log(parser.getEngine()); // {name: "KHTML", version: "4.1.4"}
var uastring3 = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
console.log(parser.setUA(uastring3).getDevice().model); // "PlayBook"
console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
console.log(parser.getBrowser().name); // "Safari"
</script>
</head>
<body>
</body>
</html>
使用节点.js
注意:在节点JS环境中没有设备信息.
$ npm install ua-parser-js
var http = require('http');
var parser = require('ua-parser-js');
http.createServer(function (req, res) {
// get user-agent header
var ua = parser(req.headers['user-agent']);
// write the result as response
res.end(JSON.stringify(ua, null, ' '));
})
.listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
使用类型脚本
$ npm install --save @types/ua-parser-js
# Download TS type definition from DefinitelyTyped repository:
# https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/ua-parser-js
使用 jQuery/ Zepto (.ua)
虽然是用香草 js 写出, 此库会自动检测 jQuery/ Zepto 是否存在并创建 $.ua 对象(以其用户代理为基础的数值)以及 window.UAParser 建筑师。 要获取/ 设置用户代理, 您可以使用 : $.ua.get() 编号 : $.ua.set(uastring)。 。 。 .
// Say we are in a browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0':
// Get the details
console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
console.log($.ua.os); // {name: "Android", version: "2.3.4"}
console.log($.ua.os.name); // "Android"
console.log($.ua.get()); // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"
// Now lets try to reset to another custom user-agent
$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13');
// Test again
console.log($.ua.browser.name); // "Safari"
console.log($.ua.engine.name); // "Webkit"
console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
// Add class to <body> tag
// <body class="ua-browser-safari ua-devicetype-tablet">
$('body').addClass('ua-browser-' + $.ua.browser.name + ' ua-devicetype-' + $.ua.device.type);
使用扩展
UAParser([uastring,] extensions)
// Example:
var myOwnListOfBrowsers = [
[/(mybrowser)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]
];
var myParser = new UAParser({ browser: myOwnListOfBrowsers });
var myUA = 'Mozilla/5.0 MyBrowser/1.3';
console.log(myParser.setUA(myUA).getBrowser()); // {name: "MyBrowser", version: "1.3"}
发展
发起人
贡献者
制造的 贡献者-img。 。 。 .
如何作出贡献
- 叉并克隆此寄存器
- 视需要作出一些修改
- 写入单位测试以显示其功能
- 运行测试套房 以确保它没有打破任何东西
$ npm test - 提交拉动请求
develop分支
许可证
MIT 许可证
版权 (c) 2012-2021 Faisal Salman <[email protected] (英语)> 翻译:
特此免费准许任何人获得副本 ,用于处理 在软件中不受限制,包括不受限制的权利 使用、复制、修改、合并、出版、分发、分许可证和/或出售 软件的复制件,并允许该软件所针对的人 满足下列条件:
上述版权通知和这项许可通知应载入所有 软件的复制件或大部分.
软件是提供的"AS IS", 没有任何的恐惧, EXPRESS OR ,包括但不限制于雇佣军的保障, 特别目的和非目的的证明。 绝对不会 B. 任何索赔、索赔或其他索赔的索赔人或 责任,在契约、契约或其他行为中,产生于, 财产、财产、财产、财产、财产或财产 软件.