Comparing version 1.0.0 to 1.2.0
{ | ||
"name": "detector", | ||
"version": "1.0.0", | ||
"version": "1.2.0", | ||
"description": "User Agent Detector.", | ||
"main": "src/detector.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha -R spec test/detector-spec.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
# detector | ||
根据 UserAgent 字符串识别客户端信息的模块。识别的信息包括: | ||
1. 硬件设备。 | ||
2. 操作系统。 | ||
3. 浏览器。 | ||
4. 浏览器渲染引擎。 | ||
识别到的信息结构如下: | ||
```js | ||
detector = { | ||
device: { | ||
name: "iphone", | ||
version: -1, | ||
fullVersion: "-1", | ||
[iphone]: -1 | ||
}, | ||
os: { | ||
name: "ios", | ||
version: 6.1, | ||
fullVersion: "6.1", | ||
[ios]: 6.1 | ||
}, | ||
browser: { | ||
name: "chrome": | ||
version: 26.0, | ||
fullVersion: "26.0.1410.50", | ||
mode: 26.0, | ||
fullMode: "26.0.1410.50", | ||
compatible: false, | ||
[chrome]: 26.0 | ||
}, | ||
engine: { | ||
name: "webkit", | ||
version: 536.26, | ||
fullVersion: "536.26", | ||
mode: 523.26, | ||
fullMode: "523.26", | ||
compatible: false, | ||
[webkit]: 536.26 | ||
} | ||
} | ||
``` | ||
备注:上面的 [iphone], [ios], [chrome], [webkit] 是动态的,根据实际识别 到的信息不同而有所不同。 | ||
detector 是根据 UserAgent 信息识别客户端的模块,可以用于在服务端处理 UserAgent | ||
识别客户端。 | ||
另外有一个更适合在客户端浏览器运行的版本的 [detector](https://github.com/aralejs/detector) | ||
该版本适合运行在 Node 环境中处理服务端收集到的 userAgent 信息。 | ||
浏览器端运行的版本推荐 [detector](https://github.com/aralejs/detector)。 | ||
## 使用说明 | ||
@@ -13,4 +62,4 @@ | ||
var detector = require("detector"); | ||
var ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"; | ||
console.log(detector.detect(ua)); | ||
var ua = req.headers['user-agent']; | ||
console.log(detector.parse(ua)); | ||
``` | ||
@@ -20,7 +69,4 @@ | ||
### {Object} detector.detect(String userAgent) | ||
### {Object} detector.parse(String userAgent) | ||
根据指定 userAgent 识别客户端信息。 | ||
这个 API 延续了基于浏览器运行的版本,但似乎基于 node 的版本无需这么累赘, | ||
请[参与讨论](https://github.com/hotoo/node-detector/issues/1) 。 |
var detector = {}; | ||
var NA_VERSION = "-1"; | ||
var re_msie = /\b(?:msie|ie) ([0-9.]+)/; | ||
var re_msie = /\b(?:msie |ie |trident\/[0-9].*rv[ :])([0-9.]+)/; | ||
@@ -16,3 +17,3 @@ function toString(object){ | ||
function each(object, factory, argument){ | ||
for(var i=0,l=object.length; i<l; i++){ | ||
for(var i=0,b,l=object.length; i<l; i++){ | ||
if(factory.call(object, object[i], i) === false){break;} | ||
@@ -26,10 +27,20 @@ } | ||
["nokia", function(ua){ | ||
// 不能将两个表达式合并,因为可能出现 "nokia; nokia 960" | ||
// 这种情况下会优先识别出 nokia/-1 | ||
if(ua.indexOf("nokia ") !== -1){ | ||
return /\bnokia ([0-9]+)?/; | ||
}else if(/\bnokia[\d]/.test(ua)){ | ||
return /\bnokia(\d+)/; | ||
}else if(ua.indexOf("noain") !== -1){ | ||
return /\bnoain ([a-z0-9]+)/; | ||
}else{ | ||
return "nokia"; | ||
return /\bnokia([a-z0-9]+)?/; | ||
} | ||
}], | ||
// 三星有 Android 和 WP 设备。 | ||
["samsung", function(ua){ | ||
if(ua.indexOf("samsung") !== -1){ | ||
return /\bsamsung(?:\-gt)?[ \-]([a-z0-9\-]+)/; | ||
}else{ | ||
return /\b(?:gt|sch)[ \-]([a-z0-9\-]+)/; | ||
} | ||
}], | ||
["wp", function(ua){ | ||
@@ -43,17 +54,46 @@ return ua.indexOf("windows phone ") !== -1 || | ||
["ipad", "ipad"], | ||
// ipod 规则应置于 iphone 之前。 | ||
["ipod", "ipod"], | ||
["iphone", "iphone"], | ||
["iphone", /\biphone\b|\biph(\d)/], | ||
["mac", "macintosh"], | ||
["mi", function(ua){ | ||
if(ua.indexOf("mi-one plus") !== -1){ | ||
return { | ||
version: "1s" | ||
}; | ||
["mi", /\bmi[ \-]?([a-z0-9 ]+(?= build))/], | ||
["aliyun", /\baliyunos\b(?:[\-](\d+))?/], | ||
["meizu", /\b(?:meizu\/|m)([0-9]+)\b/], | ||
["nexus", /\bnexus ([0-9s.]+)/], | ||
["huawei", function(ua){ | ||
if(ua.indexOf("huawei-huawei") !== -1){ | ||
return /\bhuawei\-huawei\-([a-z0-9\-]+)/; | ||
}else{ | ||
return /\bmi ([0-9.as]+)/; | ||
return /\bhuawei[ _\-]?([a-z0-9]+)/; | ||
} | ||
}], | ||
["aliyun", "aliyunos"], | ||
["meizu", /\bm([0-9]+)\b/], | ||
["nexus", /\bnexus ([0-9.]+)/], | ||
["lenovo", function(ua){ | ||
if(ua.indexOf("lenovo-lenovo") !== -1){ | ||
return /\blenovo\-lenovo[ \-]([a-z0-9]+)/; | ||
}else{ | ||
return /\blenovo[ \-]?([a-z0-9]+)/; | ||
} | ||
}], | ||
// 中兴 | ||
["zte", function(ua){ | ||
if(/\bzte\-[tu]/.test(ua)){ | ||
return /\bzte-[tu][ _\-]?([a-su-z0-9\+]+)/; | ||
}else{ | ||
return /\bzte[ _\-]?([a-su-z0-9\+]+)/; | ||
} | ||
}], | ||
// 步步高 | ||
["vivo", /\bvivo ([a-z0-9]+)/], | ||
["htc", function(ua){ | ||
if(/\bhtc[a-z0-9 _\-]+(?= build\b)/.test(ua)){ | ||
return /\bhtc[ _\-]?([a-z0-9 ]+(?= build))/; | ||
}else{ | ||
return /\bhtc[ _\-]?([a-z0-9 ]+)/; | ||
} | ||
}], | ||
["oppo", /\boppo[_]([a-z0-9]+)/], | ||
["konka", /\bkonka[_\-]([a-z0-9]+)/], | ||
["sonyericsson", /\bmt([a-z0-9]+)/], | ||
["coolpad", /\bcoolpad[_ ]?([a-z0-9]+)/], | ||
["lg", /\blg[\-]([a-z0-9]+)/], | ||
["android", "android"], | ||
@@ -76,9 +116,18 @@ ["blackberry", "blackberry"] | ||
["macosx", /\bmac os x ([0-9._]+)/], | ||
["ios", /\bcpu(?: iphone)? os ([0-9._]+)/], | ||
["ios", function(ua){ | ||
if(/\bcpu(?: iphone)? os /.test(ua)){ | ||
return /\bcpu(?: iphone)? os ([0-9._]+)/; | ||
}else if(ua.indexOf("iph os ") !== -1){ | ||
return /\biph os ([0-9_]+)/; | ||
}else{ | ||
return /\bios\b/; | ||
} | ||
}], | ||
["yunos", /\baliyunos ([0-9.]+)/], | ||
["android", /\bandroid[ -]([0-9.]+)/], | ||
["android", /\bandroid[\/\- ]?([0-9.x]+)/], | ||
["chromeos", /\bcros i686 ([0-9.]+)/], | ||
["linux", "linux"], | ||
["windowsce", /\bwindows ce(?: ([0-9.]+))?/], | ||
["symbian", /\bsymbianos\/([0-9.]+)/], | ||
["symbian", /\bsymbian(?:os)?\/([0-9.]+)/], | ||
["meego", /\bmeego\b/], | ||
["blackberry", "blackberry"] | ||
@@ -97,3 +146,4 @@ ]; | ||
engineMode, engineVersion, | ||
browserMode, browserVersion; | ||
browserMode, browserVersion, | ||
compatible=false; | ||
@@ -133,29 +183,31 @@ // IE8 及其以上提供有 Trident 信息, | ||
} | ||
var ENGINE = [ | ||
["trident", re_msie], | ||
//["blink", /blink\/([0-9.+]+)/], | ||
["webkit", /\bapplewebkit\/([0-9.+]+)/], | ||
["webkit", /\bapplewebkit[\/]?([0-9.+]+)/], | ||
["gecko", /\bgecko\/(\d+)/], | ||
["presto", /\bpresto\/([0-9.]+)/] | ||
["presto", /\bpresto\/([0-9.]+)/], | ||
["androidwebkit", /\bandroidwebkit\/([0-9.]+)/], | ||
["coolpadwebkit", /\bcoolpadwebkit\/([0-9.]+)/] | ||
]; | ||
var BROWSER = [ | ||
/** | ||
* 360SE (360安全浏览器) | ||
**/ | ||
["360", /\b360(?:se|ee|chrome)/], | ||
/** | ||
* Maxthon (傲游) | ||
**/ | ||
["mx", /\bmaxthon(?:[ \/]([0-9.]+))?/], | ||
/** | ||
* [Sogou (搜狗浏览器)](http://ie.sogou.com/) | ||
**/ | ||
// Sogou. | ||
["sg", / se ([0-9.x]+)/], | ||
/** | ||
* TheWorld (世界之窗) | ||
* NOTE: 由于裙带关系,TW API 与 360 高度重合。若 TW 不提供标准信息,则可能会被识别为 360 | ||
**/ | ||
// TheWorld (世界之窗) | ||
// 由于裙带关系,TW API 与 360 高度重合。 | ||
// 只能通过 UA 和程序安装路径中的应用程序名来区分。 | ||
// TheWorld 的 UA 比 360 更靠谱,所有将 TheWorld 的规则放置到 360 之前。 | ||
["tw", "theworld"], | ||
// 360SE, 360EE. | ||
["360", function(ua){ | ||
if(ua.indexOf("360 aphone browser") !== -1){ | ||
return /\b360 aphone browser \(([^\)]+)\)/; | ||
} | ||
return /\b360(?:se|ee|chrome|browser)\b/; | ||
}], | ||
// Maxthon | ||
["mx", /\bmaxthon(?:[ \/]([0-9.]+))?/], | ||
["qq", /\bm?qqbrowser\/([0-9.]+)/], | ||
["green", "greenbrowser"], | ||
["qq", /\bqqbrowser\/([0-9.]+)/], | ||
["tt", /\btencenttraveler ([0-9.]+)/], | ||
@@ -166,7 +218,24 @@ ["lb", "lbbrowser"], | ||
["sy", "saayaa"], | ||
// 有基于 Chromniun 的急速模式和基于 IE 的兼容模式。必须在 IE 的规则之前。 | ||
["baidu", /\bbidubrowser[ \/]([0-9.x]+)/], | ||
["mi", /\bmiuibrowser\/([0-9.]+)/], | ||
// 后面会做修复版本号,这里只要能识别是 IE 即可。 | ||
["ie", re_msie], | ||
["mi", /\bmiuibrowser\/([0-9.]+)/], | ||
// Opera 15 之后开始使用 Chromniun 内核,需要放在 Chrome 的规则之前。 | ||
["opera", function(ua){ | ||
var re_opera_old = /\bopera.+version\/([0-9.ab]+)/; | ||
var re_opera_new = /\bopr\/([0-9.]+)/; | ||
return re_opera_old.test(ua) ? re_opera_old : re_opera_new; | ||
}], | ||
["chrome", / (?:chrome|crios|crmo)\/([0-9.]+)/], | ||
// UC 浏览器,可能会被识别为 Android 浏览器,规则需要前置。 | ||
["uc", function(ua){ | ||
if(ua.indexOf("ucbrowser") >= 0){ | ||
return /\bucbrowser\/([0-9.]+)/; | ||
}else if(ua.indexOf("ucweb") >= 0){ | ||
return /\bucweb[\/]?([0-9.]+)/; | ||
}else{ | ||
return /\buc\b/; | ||
} | ||
}], | ||
// Android 默认浏览器。该规则需要在 safari 之前。 | ||
@@ -178,7 +247,6 @@ ["android", function(ua){ | ||
["safari", /\bversion\/([0-9.]+(?: beta)?)(?: mobile(?:\/[a-z0-9]+)?)? safari\//], | ||
// 如果不能被识别为 Safari,则猜测是 WebView。 | ||
["webview", /\bcpu(?: iphone)? os (?:[0-9._]+).+\bapplewebkit\b/], | ||
["firefox", /\bfirefox\/([0-9.ab]+)/], | ||
["opera", /\bopera.+version\/([0-9.ab]+)/], | ||
["uc", function(ua){ | ||
return ua.indexOf("ucbrowser") !== -1 ? /\bucbrowser\/([0-9.]+)/ : /\bucweb([0-9.]+)/; | ||
}] | ||
["nokia", /\bnokiabrowser\/([0-9.]+)/] | ||
]; | ||
@@ -198,3 +266,3 @@ | ||
name: name, | ||
version: "-1", | ||
version: NA_VERSION, | ||
codename: "" | ||
@@ -220,3 +288,3 @@ }; | ||
}else{ | ||
info.version = "-1"; | ||
info.version = NA_VERSION; | ||
} | ||
@@ -228,3 +296,3 @@ return info; | ||
var na = {name:"na", version:"-1"}; | ||
var na = {name:"na", version:NA_VERSION}; | ||
// 初始化识别。 | ||
@@ -317,4 +385,4 @@ function init(ua, patterns, factory, detector){ | ||
detector.detect = parse; | ||
detector.parse = parse; | ||
module.exports = detector; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
45888
6
1132
1
71
1