@egjs/agent

中文文档
2.4.4发布于 2024/08/23MITTypeScriptESM + CJS零依赖近 30 天下载 310.6万

Extracts browser and operating system information from the user agent string or user agent object(userAgentData).

原始使用文档
文档目录20 节
  1. Documents
  2. Download and Installation
  3. Installation with npm
  4. For development
  5. Prepare for Client Hints and User-Agent Reduction!
  6. User-Agent Reduction Proposed Rollout Plan
  7. In the future
  8. Possible (You can know exactly)
  9. If you want to use only the method using navigator.userAgent
  10. Not Possible (If synchronous)
  11. Possible (If asynchronous)
  12. If you dare to use synchrous, you have to choose.
  13. Supported Browsers
  14. Client Hints Support
  15. How to start developing egjs-agent?
  16. Development Environment
  17. Linting
  18. Test
  19. Bug Report
  20. License

egjs-agent

npm version Build Status Coverage Status Support TypeScript

Extracts browser and operating system information from the user agent string or user agent object(userAgentData).

Documents

Download and Installation

Download dist files from repo directly or install it via npm.

Installation with npm

The following command shows how to install egjs-agent using npm.

$ npm install @egjs/agent

For development

You can download the files for development

Prepare for Client Hints and User-Agent Reduction!

Chrome was planned to freeze userAgent to improve user privacy, and it is being applied as an experimental feature in 84+.

Not only Chrome, but other browsers will come someday.

It is still an experimental feature until Chrome(Chromium) 86.

If you want to test, enable the flag below.

chrome://flags/#enable-experimental-web-platform-features chrome://flags/#freeze-user-agent

This reduced format will be available for testing via chrome://flags/#reduce-user-agent in Chrome 93. chrome://flags/#reduce-user-agent

User-Agent Reduction Proposed Rollout Plan

Phase 4: Chrome 101 Ship reduced Chrome MINOR.BUILD.PATCH version numbers (“0.0.0”). Once rolled-out, the reduced UA string would apply to all page loads on desktop and mobile OSes that do not opt into the reverse Origin Trial.

Phase 5: Chrome 107 Begin roll-out of reduced Desktop UA string and related JS APIs (navigator.userAgent, navigator.appVersion, navigator.platform). Once rolled-out, the reduced UA string would apply to all page loads on desktop OSes that do not opt into the reverse Origin Trial.

Phase 6: Chrome 110 Begin roll-out of reduced Android Mobile (and Tablet) UA string and related JS APIs. Once rolled-out, the reduced UA string would apply to all page loads on Android that do not opt into the reverse Origin Trial.

Reduction Completion Phase 7: Chrome 113 reverse Origin Trial ends and all page loads receive the reduced UA string and related JS APIs.

In the future

  1. the following attribute values will not appear correctly.
    • navigator.userAgent
    • navigator.appVersion
    • navigator.platform
    • navigator.productSub
    • navigator.vendor
  2. You should use navigator.userAgentData instead of navigator.userAgent.
    • Browser version only shows major.
    • OS name, OS version, Browser full version should be fetched asynchronously.
    • The only OS name that you can check synchronously are iOS and Android.

Possible (You can know exactly)

  • Check browser name, major version
  • Check the full version under Chrome 101
  • Check mobile
  • Check OS name
  • Check Webkit, Chromium
  • Check old OS, OS Version (Chrome 84 support range or less)
    • < Android 5.0
    • < Mac OS X 10.10
    • < Windows 7
    • < iOS Unknown
import getAgent from "@egjs/agent";

const agent = getAgent();

// Check iOS
agent.os.name === "ios"
agent.os.majorVersion === 9
// Check Android
agent.os.name === "android"
agent.os.majorVersion === 4
parseFloat(agent.os.version) <= 4.4
// Check browser name
agent.browser.name === "safari"
// Check webkit
agent.browser.webkit
// Check chromium
agent.browser.chromium

If you want to use only the method using navigator.userAgent

import { getLegacyAgent } from "@egjs/agent";

const agent = getLegacyAgent();
// Check iOS
agent.os.name === "ios"
agent.os.majorVersion === 9
// Check Android
agent.os.name === "android"
agent.os.majorVersion === 4
parseFloat(agent.os.version) <= 4.4
// Check browser name
agent.browser.name === "safari"
// Check webkit
agent.browser.webkit
// Check chromium
agent.browser.chromium

Not Possible (If synchronous)

  • Check OS version.
  • Check Browser full version.
import getAgent from "@egjs/agent";
const agent = getAgent();

// If the full version is 10.5, it is displayed as 10.
agent.browser.version
// "unknown"
agent.os.name
// -1
agent.os.majorVersion

Possible (If asynchronous)

  • You can get accurate agent information.
  • Check OS version.
  • Check Browser full version.
import { getAccurateAgent } from "@egjs/agent";

// Use Promise
getAccurateAgent().then(agent => {
    // Check OS version
    agent.os.version

    // Check Browser full verion
    agent.browser.version
});

// Use Callback
getAccurateAgent(agent => {});

If you dare to use synchrous, you have to choose.

  • If you're checking a version (ex. 93.0.1234.56) lower than Chrome 101 to fix the bug, there shouldn't be a problem.
  • However, if you need to check the minor or patch version of version 101 or higher, you have to check it asynchronously.
  • You can check the OS name, but if you want to check the OS version, consider switching to asynchronous before reduction is applied.

Supported Browsers

The following are the supported browsers.

Internet ExplorerChromeFirefoxSafariiOSAndroid
7+latestlatestlatest3+2.1+

Client Hints Support

Chromium (Chrome, Edge, Whale)Gecko (Firefox)Webkit (Safari)
84+ (Experimental)UnknownUnknown

How to start developing egjs-agent?

For anyone interested to develop egjs-agent, follow the instructions below.

Development Environment

1. Clone the repository

Clone the egjs-agent repository and install the dependency modules.

# Clone the repository.
$ git clone https://github.com/naver/egjs-agent.git

2. Install dependencies

npm is supported.

# Install the dependency modules.
$ npm install

3. Build

Use npm script to build eg.agent

# Run rollup
$ npm start

# Build
$ npm run build

# Generate jsdoc
$ npm run jsdoc

Two folders will be created after complete build is completed.

  • dist folder: Includes the agent.js and agent.min.js files.
  • doc folder: Includes API documentation. The home page for the documentation is doc/index.html.

Linting

To keep the same code style, we adopted ESLint to maintain our code quality. Setup your editor for check or run below command for linting.

$ npm run lint

Test

Once you created a branch and done with development, you must perform a test running npm run test command before you push code to a remote repository.

$ npm run test

Running a npm run test command will start Jest.

Bug Report

If you find a bug, please report it to us using the Issues page on GitHub.

License

egjs-agent is released under the MIT license.

Copyright (c) 2015 NAVER Corp.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

版本与发布记录

@egjs/agent 共发布 20 个版本,首次发布于 2017/04/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.4.4latest2024/08/232 年前
2.4.32022/08/124 年前
2.4.22022/05/044 年前
2.4.12022/05/044 年前
2.4.02022/04/184 年前
2.3.12022/03/244 年前

关于 @egjs/agent 的常见问题

@egjs/agent 是什么?

Extracts browser and operating system information from the user agent string or user agent object(userAgentData).

如何安装 @egjs/agent?

在项目目录运行 npm install @egjs/agent,或使用 pnpm add / yarn add / bun add。

@egjs/agent 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@egjs/agent 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@egjs/agent 可以商用吗?

@egjs/agent 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@egjs/agent 的下载量有多少?

根据 npm 官方下载统计,@egjs/agent 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,106,093 次,日均约 103,536 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@egjs/agent 还在维护吗?

最新版本 2.4.4 发布于 2024/08/23(2 年前);至今共发布 20 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/04/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。