a2bei4-utils 1.0.8 → 1.0.9

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.
@@ -461,7 +461,37 @@ const fullscreenHelper = {
461
461
  document.removeEventListener("msfullscreenchange", handler);
462
462
  };
463
463
  }
464
- };
464
+ };
465
+
466
+ /**
467
+ * 复制文本到剪贴板
468
+ * @param {String} text
469
+ * @returns {Promise<void>}
470
+ */
471
+ async function copyTextToClipboard(text) {
472
+ if (window.navigator.clipboard) {
473
+ await window.navigator.clipboard.writeText(text);
474
+ return;
475
+ }
476
+
477
+ return new Promise((resolve, reject) => {
478
+ const ta = document.createElement("textarea");
479
+ ta.value = text;
480
+ ta.style.position = "fixed"; // 防止滚动
481
+ ta.style.opacity = "0";
482
+ document.body.appendChild(ta);
483
+ ta.focus();
484
+ ta.select();
485
+
486
+ try {
487
+ document.execCommand("copy") ? resolve() : reject(new Error("execCommand failed"));
488
+ } catch (err) {
489
+ reject(err);
490
+ } finally {
491
+ document.body.removeChild(ta);
492
+ }
493
+ });
494
+ }
465
495
 
466
496
  //#region 数据类型判断
467
497
 
@@ -2478,6 +2508,7 @@ exports.MyEvent_CrossPagePlugin = MyEvent_CrossPagePlugin;
2478
2508
  exports.MyId = MyId;
2479
2509
  exports.WebSocketManager = WebSocketManager;
2480
2510
  exports.assignExisting = assignExisting;
2511
+ exports.copyTextToClipboard = copyTextToClipboard;
2481
2512
  exports.debounce = debounce;
2482
2513
  exports.deepCloneByJSON = deepCloneByJSON;
2483
2514
  exports.downloadByBlob = downloadByBlob;