Socket
Book a DemoInstallSign in
Socket

node-user-agent

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-user-agent

Generate random user-agent strings for browsers and operating systems, including Chinese alternatives

1.0.3
latest
Source
npmnpm
Version published
Weekly downloads
274
Maintainers
1
Weekly downloads
 
Created
Source
# node-user-agent

一个用于生成随机 User-Agent 字符串的 Node.js 模块,支持主流和国产操作系统及浏览器。

## 安装

```bash
npm install node-user-agent

使用方法

基本用法

const userAgent = require('node-user-agent');

// 生成完全随机的User-Agent(主要方法)
console.log(userAgent.toString());

// 向后兼容:generate方法仍然可用
console.log(userAgent.generate());

// 生成随机系统信息
console.log(userAgent.generateSystemInfo());

指定操作系统

// 只生成Windows系统的User-Agent
console.log(userAgent.toString({ osType: 'windows' }));

// 只生成Android系统的User-Agent
console.log(userAgent.toString({ osType: 'android' }));

// 生成国产操作系统的User-Agent
console.log(userAgent.toString({ osType: 'harmonyos' }));

指定浏览器

// 只生成Chrome浏览器的User-Agent
console.log(userAgent.toString({ browserType: 'chrome' }));

// 只生成Firefox浏览器的User-Agent
console.log(userAgent.toString({ browserType: 'firefox' }));

// 生成国产浏览器的User-Agent
console.log(userAgent.toString({ browserType: 'qqbrowser' }));

获取系统信息

// 获取随机系统信息(不生成User-Agent)
const systemInfo = userAgent.generateSystemInfo();
console.log(`操作系统: ${systemInfo.name}`);
console.log(`版本: ${systemInfo.version}`);
console.log(`类型: ${systemInfo.type}`);

获取支持的操作系统和浏览器列表

// 获取浏览器支持的操作系统列表
console.log(userAgent.getSupportedOS('chrome'));

// 获取操作系统支持的浏览器列表
console.log(userAgent.getSupportedBrowsers('windows'));

解析 User-Agent

// 解析 User-Agent 字符串
const parsedInfo = userAgent.parse('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
console.log(parsedInfo);
// 输出:
// {
//   browser: { name: 'Chrome', version: '91.0.4472.124', type: 'chrome' },
//   os: { name: 'Windows', version: '10.0', type: 'windows' },
//   device: 'Desktop'
// }

支持的操作系统

主流操作系统

  • Windows (windows)
  • macOS (macos)
  • Linux (linux)
  • Android (android)
  • iOS (ios)

国产操作系统

  • HarmonyOS (harmonyos)
  • 麒麟系统 (kylin)
  • 统信UOS (uos)

支持的浏览器

主流浏览器

  • Chrome (chrome)
  • Firefox (firefox)
  • Safari (safari)
  • Edge (edge)

国产浏览器

  • QQ浏览器 (qqbrowser)
  • UC浏览器 (ucbrowser)
  • 360安全浏览器 (360se)

API 参考

toString([options])

生成随机 User-Agent 字符串(主要方法)

参数

  • options.osType (string): 指定操作系统类型
  • options.browserType (string): 指定浏览器类型

返回值

(string): User-Agent 字符串

generate([options])

toString 方法的别名(向后兼容)

generateSystemInfo([options])

生成随机系统信息对象

参数

  • options.osType (string): 指定操作系统类型

返回值

(Object): 包含操作系统信息的对象

getSupportedOS(browserType)

获取浏览器支持的操作系统列表

参数

  • browserType (string): 浏览器类型

返回值

(Array): 支持的操作系统列表

getSupportedBrowsers(osType)

获取操作系统支持的浏览器列表

参数

  • osType (string): 操作系统类型

返回值

(Array): 支持的浏览器列表

parse(userAgent)

解析 User-Agent 字符串

参数

返回值

(Object): 解析结果,包含浏览器、操作系统和设备信息

许可证

MIT

Keywords

user-agent

FAQs

Package last updated on 24 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.