npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@h2orange/device-detector

v1.0.0

Published

Device detector for web (ESM/CJS/UMD) with TypeScript types。获取设备信息(操作系统、设备类型、浏览器、屏幕信息、网络在线状态)。

Readme

Device Detector

一个纯 JavaScript/TypeScript 的网页端设备信息检测插件,不依赖任何前端框架运行时,可在 Vue / React / Angular / 原生 HTML/JS 中直接使用。

GitHub 仓库 About 建议

  • Description:Device detector for web (ESM/CJS/UMD) with TypeScript types
  • Website:README 或 Demo 地址(例如 GitHub Pages / Vercel 预览)
  • Topics:device-detector、user-agent、browser-detection、os-detection、typescript、umd、vite

功能特点

  • 设备信息检测:操作系统、设备类型、浏览器、屏幕信息、网络在线状态
  • 通用模块产物:ESM / CommonJS / UMD(支持 <script> 直接引入)
  • 全局统一入口:window.DeviceDetector
  • 支持配置检测规则与事件监听(可选)
  • TypeScript 类型声明

本仓库仍保留了旧版 getDeviceInfo()(legacy)API,便于平滑迁移。

安装

npm install @h2orange/device-detector

使用方法

ESM / CommonJS(任何框架均可)

import { DeviceDetector, createDetector } from '@h2orange/device-detector'

const info = DeviceDetector.getInfo()
console.log(info)

const detector = createDetector({
  listen: true
})

detector.on('deviceChanged', (next) => {
  console.log('device changed', next)
})

<script> 直接引入(UMD)

<script src="https://cdn.jsdelivr.net/npm/@h2orange/device-detector/dist/device-detector.umd.min.js"></script>
<script>
  const DeviceDetector = window.DeviceDetector
  const info = DeviceDetector.getInfo()
  console.log(info)
  const detector = DeviceDetector.init({ listen: true })
  detector.on('deviceChanged', (next) => console.log(next))
</script>

React / Vue / Angular 的使用方式

核心原则是:把它当作“普通 JS 库”使用即可,不需要框架适配层。

设备信息对象

getInfo() / detect() 返回结构化对象:

{
  os: { name: 'iOS', version: '16.5' },
  device: { type: 'mobile', model: 'iPhone' },
  browser: { name: 'Safari', version: '16.5' },
  screen: { width: 1170, height: 2532, pixelRatio: 3 },
  network: { online: true, type: 'wifi' },
  ua: '...',
  userAgent: '...',
  platform: '...'
}

使用示例

判断设备类型 / 系统 / 浏览器

const info = DeviceDetector.getInfo()
if (info.device.type === 'mobile') {}
if (info.os.name === 'iOS') {}
if (info.browser.name === 'Safari') {}

兼容性

  • 支持浏览器:Chrome / Firefox / Safari / Edge(不依赖框架)
  • 网络类型字段:navigator.connection 在部分浏览器不可用,插件会回退到 unknown

许可证

MIT