mazey 5.6.10 → 5.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -5
- package/README.zh-CN.md +75 -2
- package/lib/index.cjs.js +2 -2
- package/lib/index.d.ts +128 -15
- package/lib/index.esm.js +2 -2
- package/lib/mazey.min.js +2 -2
- package/package.json +10 -5
package/README.md
CHANGED
|
@@ -34,6 +34,17 @@ You can also download and serve the
|
|
|
34
34
|
[latest browser bundle](https://cdn.jsdelivr.net/npm/mazey@latest/lib/mazey.min.js)
|
|
35
35
|
yourself.
|
|
36
36
|
|
|
37
|
+
## Browser support
|
|
38
|
+
|
|
39
|
+
Mazey supports Chrome 109+, Edge 109+, Firefox 115+, Safari 16.4+, iOS Safari
|
|
40
|
+
16.4+, Android Chrome 109+, and Samsung Internet 21+. Package output can
|
|
41
|
+
contain ES2022 syntax and does not include JavaScript polyfills. Internet
|
|
42
|
+
Explorer, Opera Mini, KaiOS, the legacy Android Browser, and older browser
|
|
43
|
+
versions are outside this support policy.
|
|
44
|
+
|
|
45
|
+
Node.js 22 is used for development and continuous integration. Mazey does not
|
|
46
|
+
declare a Node.js runtime compatibility range.
|
|
47
|
+
|
|
37
48
|
## Usage
|
|
38
49
|
|
|
39
50
|
Example: Use a function to verify if a value is a number suitable for standard calculations and comparisons.
|
|
@@ -121,6 +132,7 @@ There are some examples maintained by hand below. For more information, please c
|
|
|
121
132
|
- [Storage Helpers](#storage-helpers)
|
|
122
133
|
- [DOM](#dom)
|
|
123
134
|
- [Class Helpers](#class-helpers)
|
|
135
|
+
- [hide and show](#hide-and-show)
|
|
124
136
|
- [isValidCssSelector](#isvalidcssselector)
|
|
125
137
|
- [resolveElementTarget](#resolveelementtarget)
|
|
126
138
|
- [extractElementText](#extractelementtext)
|
|
@@ -139,6 +151,9 @@ There are some examples maintained by hand below. For more information, please c
|
|
|
139
151
|
- [resolveLanguagePreference](#resolvelanguagepreference)
|
|
140
152
|
- [setLanguagePreference](#setlanguagepreference)
|
|
141
153
|
- [detectVisitorType](#detectvisitortype)
|
|
154
|
+
- [isPhone](#isphone)
|
|
155
|
+
- [isDesktop](#isdesktop)
|
|
156
|
+
- [isTablet](#istablet)
|
|
142
157
|
- [isIOS](#isios)
|
|
143
158
|
- [isAndroid](#isandroid)
|
|
144
159
|
- [isMacOS](#ismacos)
|
|
@@ -1125,6 +1140,29 @@ addClass(dom, "test");
|
|
|
1125
1140
|
removeClass(dom, "test");
|
|
1126
1141
|
```
|
|
1127
1142
|
|
|
1143
|
+
#### hide and show
|
|
1144
|
+
|
|
1145
|
+
Hide or show a CSS selector, one element, or an iterable or array-like element
|
|
1146
|
+
collection. Both helpers return the original target, so a caller can retain its
|
|
1147
|
+
own chaining convention. Duplicate elements are changed only once, and invalid
|
|
1148
|
+
selectors or unsupported values are ignored.
|
|
1149
|
+
|
|
1150
|
+
`hide()` preserves a visible element's inline `display` value. `show()` restores
|
|
1151
|
+
that value, or recovers the element's normal display when a stylesheet would
|
|
1152
|
+
otherwise keep it hidden.
|
|
1153
|
+
|
|
1154
|
+
```javascript
|
|
1155
|
+
import { hide, show } from "mazey";
|
|
1156
|
+
|
|
1157
|
+
const notices = document.querySelectorAll(".notice");
|
|
1158
|
+
|
|
1159
|
+
hide(notices);
|
|
1160
|
+
show(notices);
|
|
1161
|
+
|
|
1162
|
+
hide("#temporary-message");
|
|
1163
|
+
show(document.querySelector("#temporary-message"));
|
|
1164
|
+
```
|
|
1165
|
+
|
|
1128
1166
|
#### isValidCssSelector
|
|
1129
1167
|
|
|
1130
1168
|
Check whether a value is a CSS selector supported by the supplied query root.
|
|
@@ -1541,8 +1579,8 @@ Output: `true`
|
|
|
1541
1579
|
|
|
1542
1580
|
#### listenMediaQueryChanges
|
|
1543
1581
|
|
|
1544
|
-
Register a media-query change callback using the
|
|
1545
|
-
|
|
1582
|
+
Register a media-query change callback using the standard `change` event. The
|
|
1583
|
+
returned cleanup function is idempotent.
|
|
1546
1584
|
|
|
1547
1585
|
```javascript
|
|
1548
1586
|
const media = window.matchMedia("(prefers-color-scheme: dark)");
|
|
@@ -1636,6 +1674,84 @@ or changed, so false positives and false negatives are possible.
|
|
|
1636
1674
|
> generally requires server-side request information and provider-specific
|
|
1637
1675
|
> validation.
|
|
1638
1676
|
|
|
1677
|
+
#### isPhone
|
|
1678
|
+
|
|
1679
|
+
Check whether the current browser represents a phone or handset-class device.
|
|
1680
|
+
The result excludes tablets.
|
|
1681
|
+
|
|
1682
|
+
```javascript
|
|
1683
|
+
const result = isPhone();
|
|
1684
|
+
|
|
1685
|
+
console.log(result);
|
|
1686
|
+
```
|
|
1687
|
+
|
|
1688
|
+
#### isDesktop
|
|
1689
|
+
|
|
1690
|
+
Check whether the current browser represents a desktop or laptop-class device.
|
|
1691
|
+
Recognized touchscreen Windows laptops remain desktop devices.
|
|
1692
|
+
|
|
1693
|
+
```javascript
|
|
1694
|
+
const result = isDesktop();
|
|
1695
|
+
|
|
1696
|
+
console.log(result);
|
|
1697
|
+
```
|
|
1698
|
+
|
|
1699
|
+
#### isTablet
|
|
1700
|
+
|
|
1701
|
+
Check whether the current browser represents a tablet. The helper recognizes
|
|
1702
|
+
conventional iPads, modern iPadOS desktop mode, Android user agents without a
|
|
1703
|
+
`Mobile` token, and bounded `Tablet` tokens.
|
|
1704
|
+
|
|
1705
|
+
```javascript
|
|
1706
|
+
const result = isTablet();
|
|
1707
|
+
|
|
1708
|
+
console.log(result);
|
|
1709
|
+
```
|
|
1710
|
+
|
|
1711
|
+
You can pass a user-agent string for deterministic or server-side
|
|
1712
|
+
classification:
|
|
1713
|
+
|
|
1714
|
+
```javascript
|
|
1715
|
+
const result = isTablet(
|
|
1716
|
+
"Mozilla/5.0 (Linux; Android 14; SM-X710) AppleWebKit/537.36"
|
|
1717
|
+
);
|
|
1718
|
+
|
|
1719
|
+
console.log(result);
|
|
1720
|
+
```
|
|
1721
|
+
|
|
1722
|
+
Output:
|
|
1723
|
+
|
|
1724
|
+
```text
|
|
1725
|
+
true
|
|
1726
|
+
```
|
|
1727
|
+
|
|
1728
|
+
The three helpers use mutually exclusive form-factor classifications for
|
|
1729
|
+
recognized devices:
|
|
1730
|
+
|
|
1731
|
+
| Device | `isPhone` | `isDesktop` | `isTablet` |
|
|
1732
|
+
|:---------------|:-----------|:------------|:-----------|
|
|
1733
|
+
| iPhone | `true` | `false` | `false` |
|
|
1734
|
+
| Android phone | `true` | `false` | `false` |
|
|
1735
|
+
| iPad | `false` | `false` | `true` |
|
|
1736
|
+
| Android tablet | `false` | `false` | `true` |
|
|
1737
|
+
| Windows laptop | `false` | `true` | `false` |
|
|
1738
|
+
| MacBook | `false` | `true` | `false` |
|
|
1739
|
+
| Unknown | `false` | `false` | `false` |
|
|
1740
|
+
|
|
1741
|
+
Each helper accepts an optional explicit user-agent string. Explicit input does
|
|
1742
|
+
not borrow the current browser's platform or touch signals. Without explicit
|
|
1743
|
+
input, all three return `false` during SSR when browser signals are unavailable.
|
|
1744
|
+
|
|
1745
|
+
Device classification is heuristic and spoofable. It does not use viewport
|
|
1746
|
+
width and is not a security API or a replacement for responsive CSS and feature
|
|
1747
|
+
detection. `getBrowserInfo().platform` remains a legacy broad grouping that
|
|
1748
|
+
reports iOS and Android as `"mobile"`; the new helpers provide a more specific
|
|
1749
|
+
phone, tablet, or desktop classification.
|
|
1750
|
+
|
|
1751
|
+
`isPhone` checks device form factor. The separate `isMobile` API is a direct
|
|
1752
|
+
alias of `isValidPhoneNumber`; it validates an 11-digit Chinese mobile-shaped
|
|
1753
|
+
number and does not inspect the browser or device.
|
|
1754
|
+
|
|
1639
1755
|
#### isIOS
|
|
1640
1756
|
|
|
1641
1757
|
Check whether the current browser represents iOS or iPadOS. The current-browser
|
|
@@ -1791,10 +1907,10 @@ if (isStandalonePWA()) {
|
|
|
1791
1907
|
|
|
1792
1908
|
#### getPerformance
|
|
1793
1909
|
|
|
1794
|
-
Get page
|
|
1910
|
+
Get page-load metrics from `PerformanceNavigationTiming`.
|
|
1795
1911
|
|
|
1796
|
-
This function
|
|
1797
|
-
|
|
1912
|
+
This function rejects when the browser does not provide a navigation entry. It
|
|
1913
|
+
does not fall back to the deprecated `PerformanceTiming` API.
|
|
1798
1914
|
|
|
1799
1915
|
Usage:
|
|
1800
1916
|
|
package/README.zh-CN.md
CHANGED
|
@@ -28,6 +28,12 @@ npm install mazey
|
|
|
28
28
|
|
|
29
29
|
你也可以下载 [jsdelivr/lib/mazey.min.js](https://cdn.jsdelivr.net/npm/mazey@latest/lib/mazey.min.js),并自行托管该文件。
|
|
30
30
|
|
|
31
|
+
## 浏览器支持
|
|
32
|
+
|
|
33
|
+
Mazey 支持 Chrome 109 及以上版本、Edge 109 及以上版本、Firefox 115 及以上版本、Safari 16.4 及以上版本、iOS Safari 16.4 及以上版本、Android Chrome 109 及以上版本,以及 Samsung Internet 21 及以上版本。软件包输出可能包含 ES2022 语法,并且不包含 JavaScript polyfill。Internet Explorer、Opera Mini、KaiOS、旧版 Android Browser 和更早的浏览器版本不在支持范围内。
|
|
34
|
+
|
|
35
|
+
开发和持续集成环境使用 Node.js 22。Mazey 未声明 Node.js 运行时兼容范围。
|
|
36
|
+
|
|
31
37
|
## 使用
|
|
32
38
|
|
|
33
39
|
下面的示例使用一个函数,判断某个值是否适合参与常规计算和比较。
|
|
@@ -111,6 +117,9 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
|
|
|
111
117
|
- [longestComSubsequence](#longestcomsubsequence)
|
|
112
118
|
- [浏览器信息](#浏览器信息)
|
|
113
119
|
- [detectVisitorType](#detectvisitortype)
|
|
120
|
+
- [isPhone](#isphone)
|
|
121
|
+
- [isDesktop](#isdesktop)
|
|
122
|
+
- [isTablet](#istablet)
|
|
114
123
|
- [getBrowserInfo](#getbrowserinfo)
|
|
115
124
|
- [isSafePWAEnv](#issafepwaenv)
|
|
116
125
|
- [isStandalonePWA](#isstandalonepwa)
|
|
@@ -1145,6 +1154,70 @@ crawler
|
|
|
1145
1154
|
|
|
1146
1155
|
> `unknown` 不表示访问者已经通过真人验证。此函数只使用浏览器端启发式规则,不能作为安全边界。请勿单独使用此结果进行身份验证、授权、支付决策、速率限制、欺诈防范或访问控制。验证真实爬虫通常需要服务端请求信息,以及服务提供商规定的验证流程。
|
|
1147
1156
|
|
|
1157
|
+
#### isPhone
|
|
1158
|
+
|
|
1159
|
+
检查当前浏览器是否代表手机或手持设备。此结果不包含平板电脑。
|
|
1160
|
+
|
|
1161
|
+
```javascript
|
|
1162
|
+
const result = isPhone();
|
|
1163
|
+
|
|
1164
|
+
console.log(result);
|
|
1165
|
+
```
|
|
1166
|
+
|
|
1167
|
+
#### isDesktop
|
|
1168
|
+
|
|
1169
|
+
检查当前浏览器是否代表桌面或笔记本电脑。触摸屏 Windows 笔记本电脑仍归类为桌面设备。
|
|
1170
|
+
|
|
1171
|
+
```javascript
|
|
1172
|
+
const result = isDesktop();
|
|
1173
|
+
|
|
1174
|
+
console.log(result);
|
|
1175
|
+
```
|
|
1176
|
+
|
|
1177
|
+
#### isTablet
|
|
1178
|
+
|
|
1179
|
+
检查当前浏览器是否代表平板电脑。此函数支持常规 iPad 和 iPadOS 桌面模式。他还支持不含 `Mobile` 令牌的 Android User-Agent,以及独立的 `Tablet` 令牌。
|
|
1180
|
+
|
|
1181
|
+
```javascript
|
|
1182
|
+
const result = isTablet();
|
|
1183
|
+
|
|
1184
|
+
console.log(result);
|
|
1185
|
+
```
|
|
1186
|
+
|
|
1187
|
+
可以传入 User-Agent 字符串。此方式适合确定性测试或服务端分类。
|
|
1188
|
+
|
|
1189
|
+
```javascript
|
|
1190
|
+
const result = isTablet(
|
|
1191
|
+
"Mozilla/5.0 (Linux; Android 14; SM-X710) AppleWebKit/537.36"
|
|
1192
|
+
);
|
|
1193
|
+
|
|
1194
|
+
console.log(result);
|
|
1195
|
+
```
|
|
1196
|
+
|
|
1197
|
+
输出:
|
|
1198
|
+
|
|
1199
|
+
```text
|
|
1200
|
+
true
|
|
1201
|
+
```
|
|
1202
|
+
|
|
1203
|
+
对于已识别的设备,这 3 个函数使用互斥的设备形态分类:
|
|
1204
|
+
|
|
1205
|
+
| 设备 | `isPhone` | `isDesktop` | `isTablet` |
|
|
1206
|
+
|:-----------------|:-----------|:------------|:-----------|
|
|
1207
|
+
| iPhone | `true` | `false` | `false` |
|
|
1208
|
+
| Android 手机 | `true` | `false` | `false` |
|
|
1209
|
+
| iPad | `false` | `false` | `true` |
|
|
1210
|
+
| Android 平板电脑 | `false` | `false` | `true` |
|
|
1211
|
+
| Windows 笔记本 | `false` | `true` | `false` |
|
|
1212
|
+
| MacBook | `false` | `true` | `false` |
|
|
1213
|
+
| 未知设备 | `false` | `false` | `false` |
|
|
1214
|
+
|
|
1215
|
+
每个函数都接受可选的 User-Agent 字符串。显式输入不会读取当前浏览器的平台或触摸信号。SSR 环境无法读取浏览器信号且没有显式输入时,这 3 个函数均返回 `false`。
|
|
1216
|
+
|
|
1217
|
+
设备分类使用可伪造的启发式规则,不读取视口宽度。这些函数不能作为安全 API,也不能替代响应式 CSS 和功能检测。`getBrowserInfo().platform` 保留原有的宽泛分类,并将 iOS 和 Android 报告为 `"mobile"`。新函数提供更具体的手机、平板电脑或桌面设备分类。
|
|
1218
|
+
|
|
1219
|
+
`isPhone` 用于检查设备形态。独立的 `isMobile` API 是 `isValidPhoneNumber` 的直接别名,用于验证 11 位中国手机号码形式的字符串,不会检查浏览器或设备。
|
|
1220
|
+
|
|
1148
1221
|
#### getBrowserInfo
|
|
1149
1222
|
|
|
1150
1223
|
获取浏览器信息。
|
|
@@ -1219,9 +1292,9 @@ if (isStandalonePWA()) {
|
|
|
1219
1292
|
|
|
1220
1293
|
#### getPerformance
|
|
1221
1294
|
|
|
1222
|
-
|
|
1295
|
+
通过 `PerformanceNavigationTiming` 获取页面加载指标。
|
|
1223
1296
|
|
|
1224
|
-
|
|
1297
|
+
如果浏览器未提供导航条目,该函数返回的 Promise 会进入 rejected 状态。函数不会回退到已弃用的 `PerformanceTiming` API。
|
|
1225
1298
|
|
|
1226
1299
|
用法:
|
|
1227
1300
|
|