用于在浏览器环境中基于多维信号(输入能力、Web API、WebGL、UA 等)做设备/系统类型推断的轻量库。
- 检测目标系统:
android、ios、ipados、macos、windows、linux - 返回各系统分值、Top 结果和置信度
- 提供 WebGL、NFC 能力等辅助信息
- 将检测结果写入全局变量
window.detectedOSType - 支持 ESM/CJS/IIFE 三种产物格式
pnpm install
pnpm build构建后会在 dist 目录生成:
index.mjs(ESM)index.cjs(CommonJS)index.global.js(浏览器全局变量window.RealDevice)index.d.ts(类型声明)
import DeviceInfoService from 'devicejs';
// 或: import { DeviceInfoService } from 'devicejs';
const service = new DeviceInfoService();
const result = await service.detect();
console.log(result);<script src="./dist/index.global.js"></script>
<script>
const service = new window.RealDevice.DeviceInfoService();
service.detect().then((result) => {
console.log(result);
console.log(window.detectedOSType);
});
</script>创建检测服务实例。
执行检测并返回:
{
scores: Record<string, number>;
top: string;
confidence: number;
}同时会更新:
- 内部检测缓存
window.detectedOSType
获取最近一次检测结果(未检测时可能为 null)。
获取 WebGL 信息(vendor、renderer),不可用时返回 null。
获取 NFC 能力探测结果(API 类型、可否扫描、异常信息等)。
获取设备信号对象(当前版本可能为 null,可按后续版本迭代)。
获取格式化后的设备类型字符串,如 Android、iOS、Windows。
获取完整设备信息汇总(检测结果、UA、屏幕信息、运行环境信息等)。
examples 目录下包含两个页面:
-
examples/full-feature-test.html- 全功能测试页
- 覆盖导出校验、
detect()、各 getter 调用 - 输出 PASS/FAIL 测试摘要与详细日志
-
examples/demo.html- 展示型 Demo 页
- 白日扁平化风格 UI
- 展示检测结果、分值、完整设备信息与判据卡片
建议通过本地静态服务器打开(避免部分浏览器策略影响能力探测):
# 示例:使用任意静态服务器工具
npx serve .然后访问对应页面:
http://localhost:3000/examples/demo.htmlhttp://localhost:3000/examples/full-feature-test.html
- 该库基于启发式多信号评分,不保证 100% 准确。
- 隐私模式、浏览器安全策略、厂商定制内核会影响判据可见性。
- 建议将结果用于“环境判断与适配”而非强安全场景。