Skip to content

Commit

Permalink
new function isNumberBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Oct 16, 2024
1 parent 81e5ee1 commit 1d77582
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change logs

## 2024.10.16 v5.22.0

1. new `isNumberBrowser` function, see: [isNumberBrowser](https://github.com/saqqdy/js-cool#isnumberbrowser)
2. Default value of agent string for `getOsVersion` and `getAppVersion` changed to navigator.appVersion.

## 2024.08.27 v5.21.2

1. `pattern` support mac/ip4/ip4_pri
Expand Down
1 change: 1 addition & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const functionList = {
isIterable, // 判断是否可迭代对象
inBrowser, // 判断是否在浏览器端运行
inNodeJs, // 判断是否在nodejs环境运行
isNumberBrowser, // 判断是否360浏览器
windowSize, // windowSize获取窗口大小
getAppVersion, // 获取APP版本号(deprecated)
appVersion, // 获取APP版本号
Expand Down
65 changes: 48 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Collection of common JavaScript / TypeScript utilities
- [isIterable](#isiterable) - Determine if it is iterable
- [inBrowser](#inbrowser) - Determine if it is running on the browser side
- [inNodeJs](#innodejs) - Determine if it is running on node.js
- [isNumberBrowser](#isnumberbrowser) - Detect if the client is a 360 browser
- [windowSize](#windowsize) - Get the window size
- [getAppVersion](#getappversion) - Get the APP version number
- [appVersion](#appversion) - Get the app version number
Expand Down Expand Up @@ -1221,6 +1222,36 @@ if (inNodeJs) {
declare const inNodeJs: boolean
```

#### isNumberBrowser

Detect if the client is a 360 browser

- Since: `5.22.0`

- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------------- | -------- | -------- | -------- | --------------------- |
| userAgent | ua or any ua like string, allowed to be undefined | `string` | - | `false` | `navigator.userAgent` |

- Returns: `boolean`

- Example:

```ts
// 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36 QIHU 360EE'
// true
// 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36'
// true
```

- Types:

```ts
declare function isNumberBrowser(userAgent?: string): boolean
```

#### windowSize

Get the window size
Expand Down Expand Up @@ -1259,11 +1290,11 @@ Get the APP version number

- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------- | --------- | -------- | -------- | --------------------- |
| appName | app name | `string` | - | `true` | - |
| withApp | whether to bring the name | `boolean` | - | `false` | - |
| userAgent | ua or any ua like string, may not be passed | `string` | - | `false` | `navigator.userAgent` |
| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------------- | --------- | -------- | -------- | --------------------- |
| appName | app name | `string` | - | `true` | - |
| withApp | whether to bring the name | `boolean` | - | `false` | - |
| userAgent | ua or any ua like string, allowed to be undefined | `string` | - | `false` | `navigator.userAgent` |

- Returns: `string | boolean | null`

Expand Down Expand Up @@ -1294,11 +1325,11 @@ Get the app version number

- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------- | --------- | -------------- | -------- | ------------------- |
| appName | app name | `string` | - | `true` | - |
| ua | ua or any ua like string, may not be passed | `string` | - | `false` | navigator.userAgent |
| ignoreCase | whether to ignore case | `boolean` | `true`/`false` | `false` | `true` |
| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------------- | --------- | -------------- | -------- | ------------------- |
| appName | app name | `string` | - | `true` | - |
| ua | ua or any ua like string, allowed to be undefined | `string` | - | `false` | navigator.userAgent |
| ignoreCase | whether to ignore case | `boolean` | `true`/`false` | `false` | `true` |

- Returns: `string | null`

Expand Down Expand Up @@ -1336,7 +1367,7 @@ Get the phone system version
| ---------- | -------------------------------------------------- | -------- | -------- | -------- | --------------------- |
| osName | system type string Android, iPod, iWatch or iPhone | `string` | - | `true` | - |
| withOS | whether to bring the name | `string` | - | `false` | - |
| userAgent | ua or any ua like string, may not be passed | `string` | - | `false` | `navigator.userAgent` |
| userAgent | ua or any ua like string, allowed to be undefined | `string` | - | `false` | `navigator.userAgent` |

- Returns: `string | boolean | null`

Expand Down Expand Up @@ -1368,9 +1399,9 @@ get the system version

- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------- | -------- | -------- | -------- | ------------------- |
| ua | ua or any ua like string, may not be passed | `string` | - | `false` | navigator.userAgent |
| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------------- | -------- | -------- | -------- | ------------------- |
| ua | ua or any ua like string, allowed to be undefined | `string` | - | `false` | navigator.userAgent |

- Returns: `OsVersion | null`

Expand Down Expand Up @@ -1410,9 +1441,9 @@ Get the browser name and version

- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------- | -------- | -------- | -------- | ------------------- |
| ua | ua or any ua like string, may not be passed | `string` | - | `false` | navigator.userAgent |
| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ------------------------------------------------- | -------- | -------- | -------- | ------------------- |
| ua | ua or any ua like string, allowed to be undefined | `string` | - | `false` | navigator.userAgent |

- Returns: `BrowserVersion | null`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-cool",
"description": "Collection of common JavaScript / TypeScript utilities",
"version": "5.21.2",
"version": "5.22.0",
"packageManager": "[email protected]",
"main": "dist/index.cjs.js",
"module": "dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion src/appVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import inBrowser from './inBrowser'
* ```
* @since 5.1.0
* @param appName - app name
* @param ua - ua or any ua like string, may not be passed, default is navigator.userAgent
* @param ua - ua or any ua like string, allowed to be undefined, default is navigator.userAgent
* @param ignoreCase - whether to ignore case
* @return string|null
*/
Expand Down
2 changes: 1 addition & 1 deletion src/browserVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface BrowserVersion {
* browserVersion() // \{ name: 'Chrome', version: '114.0.0.0' \}
* ```
* @since 5.2.0
* @param ua - ua or any ua like string, may not be passed, default is navigator.userAgent
* @param ua - ua or any ua like string, allowed to be undefined, default is navigator.userAgent
* @return BrowserVersion|null
*/
function browserVersion(ua?: string): BrowserVersion | null {
Expand Down
4 changes: 2 additions & 2 deletions src/getAppVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* @since 1.0.1
* @param appName - app name
* @param withApp - whether to bring the name
* @param userAgent - ua, may not be passed, default is navigator.appVersion
* @param userAgent - ua, allowed to be undefined, default is navigator.userAgent
* @return null/true/false
*/
function getAppVersion(
appName: string,
withApp?: boolean,
userAgent?: string
): string | boolean | null {
userAgent = userAgent || navigator.appVersion
userAgent = userAgent || navigator.userAgent
const reg = new RegExp(appName + '\\/([\\d\\.]+)', 'i')
const isApp = userAgent.includes(appName)
const ver = userAgent.match(reg)
Expand Down
4 changes: 2 additions & 2 deletions src/getOsVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import getAppVersion from './getAppVersion'
* @since 1.0.1
* @param osName - system type string Android, iPod, iWatch or iPhone
* @param withOS - whether to bring the name
* @param userAgent - ua, may not be passed, default takes navigator.appVersion
* @param userAgent - ua, allowed to be undefined, default takes navigator.userAgent
* @return - null/true/false
*/
function getOsVersion(
osName: string,
withOS?: boolean,
userAgent?: string
): string | boolean | null {
userAgent = userAgent || navigator.appVersion
userAgent = userAgent || navigator.userAgent
const d = ['iPhone', 'iPad', 'iPod', 'iWatch', 'Mac', 'iMac', 'iOS']
let name = osName,
ver
Expand Down
2 changes: 2 additions & 0 deletions src/index.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import isArray from './isArray'
import isIterable from './isIterable'
import inBrowser from './inBrowser'
import inNodeJs from './inNodeJs'
import isNumberBrowser from './isNumberBrowser'
import windowSize from './windowSize'
import getAppVersion from './getAppVersion'
import appVersion from './appVersion'
Expand Down Expand Up @@ -222,6 +223,7 @@ export default {
isDarkMode,
inBrowser,
inNodeJs,
isNumberBrowser,
minus,
nextIndex,
nextVersion,
Expand Down
2 changes: 2 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const {
isDarkMode,
inBrowser,
inNodeJs,
isNumberBrowser,
minus,
nextIndex,
nextVersion,
Expand Down Expand Up @@ -217,6 +218,7 @@ export {
isDarkMode,
inBrowser,
inNodeJs,
isNumberBrowser,
minus,
nextIndex,
nextVersion,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export { default as isArray } from './isArray'
export { default as isIterable } from './isIterable'
export { default as inBrowser } from './inBrowser'
export { default as inNodeJs } from './inNodeJs'
export { default as isNumberBrowser } from './isNumberBrowser'
export { default as windowSize, type WindowSizeObj } from './windowSize'
export { default as getAppVersion } from './getAppVersion'
export { default as appVersion } from './appVersion'
Expand Down
76 changes: 76 additions & 0 deletions src/isNumberBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import inBrowser from './inBrowser'

/**
* Detect if the client is a 360 browser
*
* @example
* ```js
* // 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36 QIHU 360EE'
* // true
*
* // 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36'
* // true
* ```
* @since 5.22.0
* @param userAgent - ua, allowed to be undefined, default takes navigator.userAgent
* @returns - result
*/
function isNumberBrowser(userAgent?: string): boolean {
if (!userAgent && !inBrowser) return false
userAgent = userAgent || navigator.userAgent

return (
isNumberBrowserByUserAgent(userAgent) ||
isNumberBrowserByDll('np-mswmp.dll') ||
isNumberBrowserByMimeTypes('type', 'application/vnd.chromium.remoting-viewer')
)
}

/**
* Detect if the client is a 360 browser by userAgent
*
* @since 5.22.0
* @param userAgent - ua, allowed to be undefined, default takes navigator.userAgent
* @returns - result
*/
function isNumberBrowserByUserAgent(userAgent?: string): boolean {
userAgent = userAgent || navigator.userAgent
const ua = userAgent.toLowerCase()
if (ua.includes('360se') || ua.includes('360ee')) return true
else if (userAgent.includes('Safari') && ua.includes('wow64')) return true
return false
}

/**
* Detect if the client is a 360 browser by check dll file
*
* @since 5.22.0
* @param filename - file name
* @returns - result
*/
function isNumberBrowserByDll(filename: string) {
if (navigator.userAgent.includes('Safari')) {
for (const key in navigator.plugins) {
if (navigator.plugins[key].filename === filename) return true
}
}
return false
}

/**
* Detect if the client is a 360 browser by check mimeTypes
*
* @since 5.22.0
* @param option - mime option
* @param value - mime value
* @returns - result
*/
function isNumberBrowserByMimeTypes(option: string, value: string) {
const mimeTypes: any = navigator.mimeTypes
for (const mt in mimeTypes) {
if (mimeTypes[mt][option] === value) return true
}
return false
}

export default isNumberBrowser
2 changes: 1 addition & 1 deletion src/osVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface OsVersion {
*
* ```
* @since 5.1.0
* @param ua - ua or any ua like string, may not be passed, default is navigator.userAgent
* @param ua - ua or any ua like string, allowed to be undefined, default is navigator.userAgent
* @return OsVersion|null
*/
function osVersion(ua?: string): OsVersion | null {
Expand Down

0 comments on commit 1d77582

Please sign in to comment.