盛夏的果实 发表于 2021-7-13 18:05:02

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

  文档: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":{

    }
}

  
文档来源:51CTO技术博客https://blog.51cto.com/u_13567403/3039806
页: [1]
查看完整版本: Node.js:解析浏览器ua-UserAgent