From 1d77582eb8f4aa87f9df3abf9a232849e9131d88 Mon Sep 17 00:00:00 2001 From: saqqdy Date: Wed, 16 Oct 2024 10:09:32 +0800 Subject: [PATCH] new function isNumberBrowser --- CHANGELOG.md | 5 +++ README-zh_CN.md | 1 + README.md | 65 ++++++++++++++++++++++++++---------- package.json | 2 +- src/appVersion.ts | 2 +- src/browserVersion.ts | 2 +- src/getAppVersion.ts | 4 +-- src/getOsVersion.ts | 4 +-- src/index.default.ts | 2 ++ src/index.mjs | 2 ++ src/index.ts | 1 + src/isNumberBrowser.ts | 76 ++++++++++++++++++++++++++++++++++++++++++ src/osVersion.ts | 2 +- 13 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 src/isNumberBrowser.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d8fd8654..e2e901a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README-zh_CN.md b/README-zh_CN.md index 8d7d0968..d5497eff 100644 --- a/README-zh_CN.md +++ b/README-zh_CN.md @@ -101,6 +101,7 @@ const functionList = { isIterable, // 判断是否可迭代对象 inBrowser, // 判断是否在浏览器端运行 inNodeJs, // 判断是否在nodejs环境运行 + isNumberBrowser, // 判断是否360浏览器 windowSize, // windowSize获取窗口大小 getAppVersion, // 获取APP版本号(deprecated) appVersion, // 获取APP版本号 diff --git a/README.md b/README.md index 92846761..64c5a0ca 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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` @@ -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` @@ -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` @@ -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` @@ -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` diff --git a/package.json b/package.json index cbdc54c6..125fdc8c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "js-cool", "description": "Collection of common JavaScript / TypeScript utilities", - "version": "5.21.2", + "version": "5.22.0", "packageManager": "pnpm@9.1.3", "main": "dist/index.cjs.js", "module": "dist/index.esm-bundler.js", diff --git a/src/appVersion.ts b/src/appVersion.ts index 3578792a..161784e1 100644 --- a/src/appVersion.ts +++ b/src/appVersion.ts @@ -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 */ diff --git a/src/browserVersion.ts b/src/browserVersion.ts index 9e36a891..c4f0e5e8 100644 --- a/src/browserVersion.ts +++ b/src/browserVersion.ts @@ -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 { diff --git a/src/getAppVersion.ts b/src/getAppVersion.ts index cfbdb326..3ef48f8c 100644 --- a/src/getAppVersion.ts +++ b/src/getAppVersion.ts @@ -5,7 +5,7 @@ * @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( @@ -13,7 +13,7 @@ function getAppVersion( 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) diff --git a/src/getOsVersion.ts b/src/getOsVersion.ts index 0ad2fe42..641ea85b 100644 --- a/src/getOsVersion.ts +++ b/src/getOsVersion.ts @@ -15,7 +15,7 @@ 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( @@ -23,7 +23,7 @@ function getOsVersion( 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 diff --git a/src/index.default.ts b/src/index.default.ts index e752b545..0fcdb43b 100644 --- a/src/index.default.ts +++ b/src/index.default.ts @@ -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' @@ -222,6 +223,7 @@ export default { isDarkMode, inBrowser, inNodeJs, + isNumberBrowser, minus, nextIndex, nextVersion, diff --git a/src/index.mjs b/src/index.mjs index 6a3a0222..507e7984 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -89,6 +89,7 @@ const { isDarkMode, inBrowser, inNodeJs, + isNumberBrowser, minus, nextIndex, nextVersion, @@ -217,6 +218,7 @@ export { isDarkMode, inBrowser, inNodeJs, + isNumberBrowser, minus, nextIndex, nextVersion, diff --git a/src/index.ts b/src/index.ts index 400e938c..de7f2136 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/isNumberBrowser.ts b/src/isNumberBrowser.ts new file mode 100644 index 00000000..3012734d --- /dev/null +++ b/src/isNumberBrowser.ts @@ -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 diff --git a/src/osVersion.ts b/src/osVersion.ts index ef9ca6e7..9cfbed4b 100644 --- a/src/osVersion.ts +++ b/src/osVersion.ts @@ -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 {