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