@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.
package/dist/index.esm.js CHANGED
@@ -5250,7 +5250,7 @@ class Utils {
5250
5250
  this.windowApi = new WindowApi(option);
5251
5251
  }
5252
5252
  /** 版本号 */
5253
- version = "2025.5.26";
5253
+ version = "2025.5.28";
5254
5254
  addStyle(cssText) {
5255
5255
  if (typeof cssText !== "string") {
5256
5256
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -8517,6 +8517,74 @@ class Utils {
8517
8517
  globalThis.clearInterval(timeId);
8518
8518
  }
8519
8519
  }
8520
+ /**
8521
+ * 获取剪贴板信息
8522
+ */
8523
+ async getClipboardInfo() {
8524
+ return new Promise((resolve) => {
8525
+ /** 读取剪贴板 */
8526
+ function readClipboardText() {
8527
+ navigator.clipboard
8528
+ .readText()
8529
+ .then((clipboardText) => {
8530
+ resolve({
8531
+ error: null,
8532
+ content: clipboardText,
8533
+ });
8534
+ })
8535
+ .catch((error) => {
8536
+ resolve({
8537
+ error: error,
8538
+ content: "",
8539
+ });
8540
+ });
8541
+ }
8542
+ /** 申请读取剪贴板的权限 */
8543
+ function requestPermissionsWithClipboard() {
8544
+ navigator.permissions
8545
+ .query({
8546
+ // @ts-ignore
8547
+ name: "clipboard-read",
8548
+ })
8549
+ .then((permissionStatus) => {
8550
+ readClipboardText();
8551
+ })
8552
+ .catch((error) => {
8553
+ /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
8554
+ readClipboardText();
8555
+ });
8556
+ }
8557
+ /**
8558
+ * 检查当前环境是否支持读取剪贴板Api
8559
+ */
8560
+ function checkClipboardApi() {
8561
+ if (typeof navigator?.clipboard?.readText !== "function") {
8562
+ return false;
8563
+ }
8564
+ if (typeof navigator?.permissions?.query !== "function") {
8565
+ return false;
8566
+ }
8567
+ return true;
8568
+ }
8569
+ if (!checkClipboardApi()) {
8570
+ resolve({
8571
+ error: new Error("当前环境不支持读取剪贴板Api"),
8572
+ content: "",
8573
+ });
8574
+ return;
8575
+ }
8576
+ if (document.hasFocus()) {
8577
+ requestPermissionsWithClipboard();
8578
+ }
8579
+ else {
8580
+ window.addEventListener("focus", () => {
8581
+ requestPermissionsWithClipboard();
8582
+ }, {
8583
+ once: true,
8584
+ });
8585
+ }
8586
+ });
8587
+ }
8520
8588
  }
8521
8589
  let utils = new Utils();
8522
8590