tianyun 1 年之前
父節點
當前提交
55cc42f976
共有 2 個文件被更改,包括 64 次插入5 次删除
  1. 26 0
      simple-demo/cp_excalidraw_frame.html
  2. 38 5
      simple-demo/js/util.js

+ 26 - 0
simple-demo/cp_excalidraw_frame.html

@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>复制一个frame</title>
+    <script src="js/util.js"></script>
+</head>
+<body>
+
+<script>
+    let str = "{\"type\":\"excalidraw/clipboard\",\"elements\":[{\"type\":\"embeddable\",\"version\":180,\"versionNonce\":1920335448,\"isDeleted\":false,\"id\":\"f9LOsIKv8drPM8aOetFXl\",\"fillStyle\":\"hachure\",\"strokeWidth\":1,\"strokeStyle\":\"solid\",\"roughness\":1,\"opacity\":100,\"angle\":0,\"x\":864.2622041015625,\"y\":232.2603759765625,\"strokeColor\":\"#000000\",\"backgroundColor\":\"transparent\",\"width\":722.40234375,\"height\":318.40625,\"seed\":607107403,\"groupIds\":[],\"frameId\":null,\"roundness\":null,\"boundElements\":[],\"updated\":1704786477631,\"link\":\"https://web.tianyunperfect.cn/simple/edit_online.html?id=667374\",\"locked\":false,\"validated\":true}],\"files\":{}}";
+    // 时间戳 秒
+    let timestamp = Date.parse(new Date()) / 1000;
+    str = str.replace(/667374/g, timestamp);
+    // 复制到剪切板
+    copyUtil.copyText(str);
+    setInterval(function () {
+        copyUtil.getClipboardText().then(function (text) {
+            if (text.indexOf(timestamp) > -1) {
+                closeWindow();
+            }
+        });
+    }, 500);
+</script>
+</body>
+</html>

+ 38 - 5
simple-demo/js/util.js

@@ -11,12 +11,13 @@ function lt(obj) {
 function deepClone(obj) {
 function deepClone(obj) {
     return JSON.parse(JSON.stringify(obj))
     return JSON.parse(JSON.stringify(obj))
 }
 }
+
 function debounce(fn, wait = 50) {
 function debounce(fn, wait = 50) {
     // 通过闭包缓存一个定时器 id
     // 通过闭包缓存一个定时器 id
     let timer = null
     let timer = null
     // 将 debounce 处理结果当作函数返回
     // 将 debounce 处理结果当作函数返回
     // 触发事件回调时执行这个返回函数
     // 触发事件回调时执行这个返回函数
-    return function(...args) {
+    return function (...args) {
         // 如果已经设定过定时器就清空上一次的定时器
         // 如果已经设定过定时器就清空上一次的定时器
         if (timer) clearTimeout(timer)
         if (timer) clearTimeout(timer)
 
 
@@ -363,6 +364,36 @@ const copyUtil = {
         } catch (err) {
         } catch (err) {
             console.error('复制失败', err);
             console.error('复制失败', err);
         }
         }
+    },
+    copyText: async function (text) {
+        if (navigator.clipboard) {
+            // clipboard api 复制
+            await navigator.clipboard.writeText(text);
+        } else {
+            const textarea = document.createElement('textarea');
+            document.body.appendChild(textarea);
+            // 隐藏此输入框
+            textarea.style.position = 'fixed';
+            textarea.style.clip = 'rect(0 0 0 0)';
+            textarea.style.top = '10px';
+            // 赋值
+            textarea.value = text;
+            // 选中
+            textarea.select();
+            // 复制
+            document.execCommand('copy', true);
+            // 移除输入框
+            document.body.removeChild(textarea);
+        }
+    },
+    getClipboardText: async function () {
+        try {
+            const text = await navigator.clipboard.readText();
+            console.log("剪贴板内容:", text);
+            return text;
+        } catch (err) {
+            console.error("无法读取剪贴板内容:", err);
+        }
     }
     }
 }
 }
 
 
@@ -399,14 +430,16 @@ async function convertToWebPAsync(base64Image) {
 }
 }
 
 
 
 
-function closeWindow(){
+function closeWindow() {
     const userAgent = navigator.userAgent;
     const userAgent = navigator.userAgent;
-    if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") !=-1) {
-        window.location.href="about:blank";
+    if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") != -1) {
+        window.location.href = "about:blank";
         window.close();
         window.close();
     } else {
     } else {
         window.opener = null;
         window.opener = null;
         window.open("", "_self");
         window.open("", "_self");
         window.close();
         window.close();
     }
     }
-}
+}
+
+