tianyunperfect 2 gadi atpakaļ
vecāks
revīzija
262493507f
3 mainītis faili ar 103 papildinājumiem un 36 dzēšanām
  1. 38 36
      monkey/AAAAA-复制标题-到memory.js
  2. 41 0
      monkey/util.js
  3. 24 0
      tmp.js

+ 38 - 36
monkey/AAAAA-复制标题-到memory.js

@@ -7,54 +7,56 @@
 // @version     1.0
 // @author      tianyunperfect
 // @require     https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js
-// @require     https://git.tianyunperfect.cn/tianyunperfect/web-base/raw/master/monkey/util.js?a=1
+// @require     https://git.tianyunperfect.cn/tianyunperfect/web-base/raw/master/monkey/util.js?a=2022年10月17日14:12:19
 // @description 2021/1/30 下午7:09:51
 // ==/UserScript==
 
 (function () {
     'use strict';
-    GM_registerMenuCommand("!!!复制并加到memory", to_memory)
+    // GM_registerMenuCommand("!!!复制并加到memory", to_memory)
     GM_registerMenuCommand("!!!复制标题和 url", copyTitle)
 
-    function to_memory() {
-        let url = location.href;
-        let title = document.title;
-        if (window.self !== window.top) {
-            return
-        }
-        let body = {
-            "back": "<p></p>",
-            "front": `<p>url: <a href="${url}">${title}</a></p>`,
-            "period": 0,
-            "onlyText": "",
-            "remindTime": new Date(),
-            "tag": "",
-            "updateTime": "",
-            "userId": 1
-        };
-        let memory_url = "https://memory.tianyunperfect.cn/api/memory-64B206F1-E915-4298-9AB7-9C561040B012/insert";
-        axios.post(memory_url, body).then((res) => {
-            console.log(res.data);
-            let data = res.data;
-            if (data.success) {
-
-            } else {
-                alert(data.message);
-            }
-        })
-        myAlert(`${title}<br/>${location.host}`, 2)
-    }
+    // function to_memory() {
+    //     let url = location.href;
+    //     let title = document.title;
+    //     if (window.self !== window.top) {
+    //         return
+    //     }
+    //     let body = {
+    //         "back": "<p></p>",
+    //         "front": `<p>url: <a href="${url}">${title}</a></p>`,
+    //         "period": 0,
+    //         "onlyText": "",
+    //         "remindTime": new Date(),
+    //         "tag": "",
+    //         "updateTime": "",
+    //         "userId": 1
+    //     };
+    //     let memory_url = "https://memory.tianyunperfect.cn/api/memory-64B206F1-E915-4298-9AB7-9C561040B012/insert";
+    //     axios.post(memory_url, body).then((res) => {
+    //         console.log(res.data);
+    //         let data = res.data;
+    //         if (data.success) {
+    //
+    //         } else {
+    //             alert(data.message);
+    //         }
+    //     })
+    //     myAlert(`${title}<br/>${location.host}`, 2)
+    // }
 
-    function copyTitle() {
-        myCopy(`<a href="${location.href}">${document.title}</a>`)
-    }
+    // function copyTitle() {
+    //     myCopy(`<a href="${location.href}">${document.title}</a>`)
+    // }
+
+    // unsafeWindow.to_memory = to_memory;
 
-    unsafeWindow.to_memory = to_memory;
+    addJSFile("//cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js");
 
     document.onkeydown = function (ev) {
         if (ev.key === 'c' && ev.shiftKey) {
-            ev.preventDefault() // 关闭浏览器快捷键
-            copyTitle()
+            ev.preventDefault();
+            copyTextToClipboard(document.title, location.href);
         }
     }
 })();

+ 41 - 0
monkey/util.js

@@ -178,6 +178,47 @@ function addJQ() {
     }, 200)
 }
 
+/**
+ * 复制
+ * 需要添加:addJSFile("//cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js")
+ * @param text
+ * @param uri
+ */
+function copyTextToClipboard(text,uri) {
+    let copyFrom, agent, body;
+
+    copyFrom = document.createElement("a");
+    copyFrom.setAttribute("id","target");
+    copyFrom.setAttribute("href",uri);
+    copyFrom.innerHTML = text;
+    agent = document.createElement("button");
+    body = document.getElementsByTagName('body')[0];
+    body.appendChild(copyFrom);
+    body.appendChild(agent);
+    // 麻烦:无法传入'.btn'元素 我们可以创建一个btn作为代理
+    let clipboard = new ClipboardJS(agent, {
+        target: function() {
+            return document.querySelector('#target');
+        }
+    });
+
+    clipboard.on('success', function(e) {
+        console.log(e);
+    });
+
+    clipboard.on('error', function(e) {
+        console.log(e);
+    });
+    agent.click();
+    // copyFrom.focus();
+    // copyFrom.select();  // 问题所在 无法对copyFrom对象使用select()方法
+    // document.execCommand('copy'); // 采用Clipboard.js方案
+    body.removeChild(copyFrom);
+    body.removeChild(agent);
+
+
+}
+
 function addLink(domSelector, link_str, linkUrl) {
     const eleScript = document.createElement("a");
     eleScript.innerHTML = link_str.replaceAll(" ", "&nbsp;");

+ 24 - 0
tmp.js

@@ -0,0 +1,24 @@
+import svgToEx from "svg-to-excalidraw";
+
+const heartSVG = `
+<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
+  <path d="M 10,30
+           A 20,20 0,0,1 50,30
+           A 20,20 0,0,1 90,30
+           Q 90,60 50,90
+           Q 10,60 10,30 z"/>
+</svg>
+`;
+
+const { hasErrors, errors, content } = svgToEx.convert(heartSVG);
+
+// SVG parsing errors are propagated through.
+if (hasErrors) {
+    console.error(errors);
+    return;
+}
+
+navigator.clipboard.writeText(content);
+
+// the heart excalidraw json is now copied to your clipboard.
+// Just Paste it into your Excalidraw session!