@whitesev/utils 2.6.7 → 2.6.8

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.
@@ -1847,6 +1847,19 @@ declare class Utils {
1847
1847
  * @param timeId setInterval 返回的`id`
1848
1848
  */
1849
1849
  workerClearInterval(timeId: number | undefined): void;
1850
+ /**
1851
+ * 获取剪贴板信息
1852
+ */
1853
+ getClipboardInfo(): Promise<{
1854
+ /**
1855
+ * 错误信息,如果为null,则表示读取成功
1856
+ */
1857
+ error: Error | null;
1858
+ /**
1859
+ * 剪贴板内容
1860
+ */
1861
+ content: string;
1862
+ }>;
1850
1863
  }
1851
1864
  declare let utils: Utils;
1852
1865
  export { utils as Utils };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.6.7",
3
+ "version": "2.6.8",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
package/src/Utils.ts CHANGED
@@ -37,7 +37,7 @@ class Utils {
37
37
  this.windowApi = new WindowApi(option);
38
38
  }
39
39
  /** 版本号 */
40
- version = "2025.5.26";
40
+ version = "2025.5.28";
41
41
 
42
42
  /**
43
43
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -5295,6 +5295,86 @@ class Utils {
5295
5295
  globalThis.clearInterval(timeId);
5296
5296
  }
5297
5297
  }
5298
+ /**
5299
+ * 获取剪贴板信息
5300
+ */
5301
+ async getClipboardInfo() {
5302
+ return new Promise<{
5303
+ /**
5304
+ * 错误信息,如果为null,则表示读取成功
5305
+ */
5306
+ error: Error | null;
5307
+ /**
5308
+ * 剪贴板内容
5309
+ */
5310
+ content: string;
5311
+ }>((resolve) => {
5312
+ /** 读取剪贴板 */
5313
+ function readClipboardText() {
5314
+ navigator.clipboard
5315
+ .readText()
5316
+ .then((clipboardText) => {
5317
+ resolve({
5318
+ error: null,
5319
+ content: clipboardText,
5320
+ });
5321
+ })
5322
+ .catch((error: TypeError) => {
5323
+ resolve({
5324
+ error: error,
5325
+ content: "",
5326
+ });
5327
+ });
5328
+ }
5329
+ /** 申请读取剪贴板的权限 */
5330
+ function requestPermissionsWithClipboard() {
5331
+ navigator.permissions
5332
+ .query({
5333
+ // @ts-ignore
5334
+ name: "clipboard-read",
5335
+ })
5336
+ .then((permissionStatus) => {
5337
+ readClipboardText();
5338
+ })
5339
+ .catch((error: TypeError) => {
5340
+ /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
5341
+ readClipboardText();
5342
+ });
5343
+ }
5344
+ /**
5345
+ * 检查当前环境是否支持读取剪贴板Api
5346
+ */
5347
+ function checkClipboardApi() {
5348
+ if (typeof navigator?.clipboard?.readText !== "function") {
5349
+ return false;
5350
+ }
5351
+ if (typeof navigator?.permissions?.query !== "function") {
5352
+ return false;
5353
+ }
5354
+ return true;
5355
+ }
5356
+ if (!checkClipboardApi()) {
5357
+ resolve({
5358
+ error: new Error("当前环境不支持读取剪贴板Api"),
5359
+ content: "",
5360
+ });
5361
+ return;
5362
+ }
5363
+ if (document.hasFocus()) {
5364
+ requestPermissionsWithClipboard();
5365
+ } else {
5366
+ window.addEventListener(
5367
+ "focus",
5368
+ () => {
5369
+ requestPermissionsWithClipboard();
5370
+ },
5371
+ {
5372
+ once: true,
5373
+ }
5374
+ );
5375
+ }
5376
+ });
5377
+ }
5298
5378
  }
5299
5379
 
5300
5380
  let utils = new Utils();