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

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":{
    }
}