npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ua-format-js

v0.0.3

Published

Our name is UAFormat.js, standing on the shoulders of UAParser.js

Downloads

5

Readme

ua-format-js

重复造轮子,一个userAgent解析器UAFormat.js(参考UAParser.js)

Another wheel,an userAgent parser UAFormat.js (refer to the UAParser.js)

致力于更符合中国特色的浏览器userAgent字符串识别与探测!欢迎添砖加瓦

方法

  1. setUA()
  2. getUA()
  3. getOS()
  4. getBrowser()
  5. getDevice()
  6. getEngine()
  7. getResult()
  8. ~~getCPU()~~

注解

调用getOS()getBrowser()getEngine() 返回 { name:'', version:'' }

调用getDevice() 返回 { type:'', model:'', vendor:'' }

  • 'device.type'可能值,支持自定义: desktop、mobile、tablet、smarttv、wearable、embedded、console

  • 'device.vendor'可能值,支持自定义: Meizu、Samsung、Lenovo、Acer、Alcatel、Amazon、HP、 HTC、Huawei、Jolla、LG、Microsoft、Motorola、Nexian、 Apple、Nintendo、Nokia、Nvidia、Ouya、Palm、Panasonic、 Archos、Asus、BenQ、BlackBerry、Dell、GeeksPhone、Google、 Polytron、RIM、Sharp、Siemens、Sony-Ericsson、Sprint、Xbox、 ZTE

  • 'device.model' 动态解析,支持自定义

规则

src/ua-format.jsuaRules对象中添加相应的解析规则

getOS方法中的os的相应信息,在osRules中添加如下一个规则对象

{
  patterns:[/microsoft\s(windows)\s(vista|xp)/i],                  // Windows (iTunes)
  defaults:[[NAME,'Windows'],[VERSION]]
}
  • patterns是正则表达式数组(允许多个)
  • defaults是默认值数组(二维表示自定义值,自定义值优先使用)

例子

  • HTML引入示例
<!doctype html>
<html>
<head>
<script type="text/javascript" src="ua-format.min.js"></script>
<script type="text/javascript">

	var formater = new UAFormat();

    // 默认使用当前浏览器: window.navigator.userAgent
    console.log(formater.getResult());
    /*
    /// 输出结果:
    {
        ua: "",
        browser: {
            name: "",
            version: ""
        },
        engine: {
            name: "",
            version: ""
        },
        os: {
            name: "",
            version: ""
        },
        device: {
            model: "",
            type: "",
            vendor: ""
        }
    }
    */

    // 自定义ua
    var uaString = "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";
    formater.setUA(uaString);

    var result = formater.getResult();

    console.log(result.browser);        // {name: "Chromium", version: "15.0.874.106"}
    console.log(result.device);         // {model: "", type: "", vendor: ""}
    console.log(result.os);             // {name: "Ubuntu", version: "11.10"}
    console.log(result.os.version);     // "11.10"
    console.log(result.engine.name);    // "WebKit"

    var uaString2 = '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(formater.setUA(uaString2).getDevice().model); // "PlayBook"
    console.log(formater.getOS());                            // {name: "RIM Tablet OS", version: "1.0.0"}
    console.log(formater.getBrowser().name);                  // "Safari"

</script>
</head>
<body>
</body>
</html>
  • node使用
npm install ua-format-js --save

js文件示例

var formater = require('ua-format-js').UAFormat();
var uaString = 'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; ONE A2001 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/6.0 Mobile Safari/537.36';

formater.setUA(uaString);
console.log(formater.getResult());

// result
// {
//     ua: 'Mozilla/5.0(Linux;U;Android6.0.1;zh-cn;ONEA2001Build/MMB29M)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/37.0.0.0MQQBrowser/6.0MobileSafari/537.36',
//     os: {
//         name: 'Android',
//         version: '6.0.1'
//     },
//     browser: {
//         name: 'QQBrowser',
//         version: '6.0'
//     },
//     device: {
//         model: 'ONEA2001',
//         vendor: 'OnePlus',
//         type: 'mobile'
//     },
//     engine: {
//         name: 'WebKit',
//         version: '537.36'
//     }
// }

更新

  • v0.0.3 -2018/9/17
    1. 添加一些常见浏览器qq、2345、line、MiPlus...
    2. 修改几处变量命名
    3. 归类正则减少初始化Object
    4. 开发模式依赖插件因安全问题升级
  • v0.0.2 -2017/3/10
    1. 修复一个bug
    2. 兼容nodejs使用
    3. 增加小米、红米、魅族、努比亚、vivo、oppo、1+、乐视、金立手机识别
    4. 增加百度浏览器baiduboxapp识别
    5. 调整部分注释
    6. 该版本以后,如果只增加正则识别则只更新版本号尾数如0.0.xx,功能性增加则中间位数依次迭代
  • v0.0.1 - 2017/3/8
    1. 实现同原理的UAParser.js
    2. 复制UAParser.js的所有正则内容
    3. 无结果时默认为unknown

To Do

  1. 持续增加国内常见手机端ua
  2. 增加 desktop 设备识别
  3. 精简不常用ua
  4. 增加自己的演示地址

鸣谢

打赏

请我喝杯咖啡