Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@tool-developer/wo-useragent

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tool-developer/wo-useragent

detect browser, os and device based on the userAgent string

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

wo-useragent

浏览器环境正则处理输出,支持微信,QQ,国内等环境。

用法

//
import UserAgent from '@tool-developer/wo-useragent';
const ua = new UserAgent(navigator.userAgent);

// console.log(ua)

输出结果举例:

{
  mobile: true,
  qq: true,
  'qq-version': '6.7.1.416',
  client: { name: 'qq', version: '6.7.1.416', major: 6, minor: 7, patch: 1 },
  webkit: true,
  engine: 'webkit',
  ios: true,
  'ios-version': 'iphone',
  iphone: true,
  iphone10: true,
  'iphone-version': '10.2.1',
  device: { name: 'iphone', version: '10.2.1', major: 10, minor: 2, patch: 1 }
}

二级结果输出

目前支持engine、device、browser、client二级输出

  {
    name:"iphone",
    major:10,
    minor:2,
    patch:1,
    version:"10.2.1"
  }

  console.log(us.engine)
  console.log(us.engine)
  console.log(us.browser)
  console.log(us.client)

举例说明:

微信环境

// 微信
// ua.webchat
if(ua.micromessenger){
  //
}

Android环境

// Android
if(ua.android){

}

iOS环境

// iOS
if(ua.ios){

}

添加自定义规则

UserAgent.custom({
  taobao: {
    regex:/(?:tao|taobao)browser(?:.*version|)\/([\d.]+)/,
    browser:true
  }
})

规则说明

属性说明
regex创建一个新的请求对象,形成请求链
alias满足regex条件后,alias别名对应值也会标记为true
mobile标记为移动端,未标记的话默认会desktop为true
browser标记为浏览器处理
device标记为设备处理
client标记为客户端处理
engine标记为浏览器引擎
filter过滤掉某个属性的影响
devtools识别开发者工具标记
desktop识别桌面端标记

正则匹配说明

如果值输出true,正则中不能含有[子表达式捕获]处理,如果需要输出版本version,则需要对版本号进行[子表达式捕获]。

举例说明

webkit: {
  regex:/webkit\/(?:[\d.]+)/
},

只会输出webkit匹配结果

webkit: {
  regex:/webkit\/([\d.]+)/
},

除了输出webkit匹配结果,还会输出webkit version,通过['webkit-version']输出。

Keywords

os

FAQs

Package last updated on 01 Oct 2021

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