评论

收藏

[JavaScript] Node.js:解析浏览器ua-UserAgent

开发技术 开发技术 发布于:2021-07-13 18:05 | 阅读数:349 | 评论:0

  文档:https://www.npmjs.com/package/ua-parser-js

Node.js
  安装
$ npm install ua-parser-js
  示例
const UAParser = require("ua-parser-js");
const ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36';
// 如果在浏览器中使用,可以不传参数
var uaParser = new UAParser(ua);
console.log(uaParser.getResult());

CDN
<script src="https://cdn.jsdelivr.net/npm/ua-parser-js@0/dist/ua-parser.min.js"></script>
<html lang="en">
  <head>
  <title>Demo</title>
 
  </head>
  <body>
  <script>
    var uaParser = new UAParser();
    console.log(JSON.stringify(uaParser.getResult()));
  </script>
  </body>
</html>
  解析结果
{
  "ua":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
  "browser":{
    "name":"Chrome",
    "version":"88.0.4324.150",
    "major":"88"
  },
  "engine":{
    "name":"Blink",
    "version":"88.0.4324.150"
  },
  "os":{
    "name":"Mac OS",
    "version":"10.14.4"
  },
  "device":{
  },
  "cpu":{
  }
}
  
关注下面的标签,发现更多相似文章