tianyun 3 anni fa
parent
commit
5374cc7fe1
2 ha cambiato i file con 74 aggiunte e 3 eliminazioni
  1. 60 3
      tmp/monkey/AAAAA-复制标题-到memory.js
  2. 14 0
      tmp/monkey/util.js

+ 60 - 3
tmp/monkey/AAAAA-复制标题-到memory.js

@@ -10,7 +10,7 @@
 // @version     1.0
 // @author      tianyunperfect
 // @require     https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js
-// @require     http://www.tianyunperfect.cn:3001/tianyunperfect/web-base/raw/master/tmp/monkey/util.js
+// @require     http://www.tianyunperfect.cn:3001/tianyunperfect/web-base/raw/master/tmp/monkey/util.js?a=1
 // @description 2021/1/30 下午7:09:51
 // ==/UserScript==
 
@@ -22,6 +22,9 @@
     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>`,
@@ -31,7 +34,7 @@
             "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);
@@ -42,7 +45,7 @@
                 alert(data.message);
             }
         })
-        myAlert(`${title}<br/>${location.host}`,2)
+        myAlert(`${title}<br/>${location.host}`, 2)
     }
 
     function copyTitle() {
@@ -57,3 +60,57 @@
 
     }
 })();
+
+
+let btnStyle = `
+  #copy-title-and-location {
+    position: fixed; top: 100px; left: -95px; opacity: 0.3; z-index: 2147483647; 
+    background-image: none; cursor:pointer; color: #fff; background-color: #0084ff !important; 
+    margin: 5px 0px; width: auto; border-radius: 3px; border: #0084ff; outline: none; padding: 3px 6px; height: 26px;
+    font-family: Arial, sans-serif; font-size: 12px; transition: left, 0.5s;
+    }
+  #copy-title-and-location:hover {left: 0px; opacity: 1;}
+  #copy-title-and-location svg {width: auto; vertical-align: middle; margin-left: 10px; border-style: none;text-align: center;display: inline-block !important;margin-bottom: 2px;}`;
+let styleTag = createEle('style', btnStyle, { type: "text/css" });
+
+// 将按钮图标由原来的img改为了svg,以增强适应性,同时也将对svg的样式设置移到了上面的 btnStyle 中
+let iconSVG = '<?xml version="1.0" encoding="UTF-8"?><svg width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M8 6C8 4.89543 8.89543 4 10 4H30L40 14V42C40 43.1046 39.1046 44 38 44H10C8.89543 44 8 43.1046 8 42V6Z" fill="none" stroke="#333" stroke-width="4" stroke-linejoin="round"/><path d="M16 20H32" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><path d="M16 28H32" stroke="#333" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>';
+let btn = createEle('button', '', { id: "copy-title-and-location" });
+btn.innerHTML = '复制标题和地址' + iconSVG;
+
+btn.addEventListener('click', () => {
+    let date = new Date();
+    let timeStamp = '更新:' + date.toLocaleDateString().replace('\/', '年').replace('\/', '月') + '日' + date.toLocaleTimeString('chinese', { hour12: false });
+    let titleInfo = document.querySelector('title').innerText;
+    let address = '\n> 参考:[' + titleInfo + ']' + '(' + location + ')';
+    // 匹配微信公众号的文章地址
+    let regWeChat = /https:\/\/mp.weixin.qq.com\//;
+    if (regWeChat.test(location.toString())) {
+        let officialAccount = document.getElementById('js_name');
+        let publishDate = document.getElementById('publish_time');
+        publishDate.click();
+        address = '\n> 参考:[【微信公众号:' + officialAccount.innerText + ' ' + publishDate.innerText + '】' + titleInfo + ']' + '(' + location + ')';
+    }
+    // 【更新:2021年9月12日22:48:36】添加了一个try catch来应对当前页面不能访问 navigator.clipboard 对象的问题
+    try {
+        navigator.clipboard.writeText(timeStamp + address);
+    } catch (err) {
+        console.log('当前页面不支持访问 navigator.clipboard 对象:' + err);
+        // if(/www.ruanyifeng.com/.test(location.toString())) {
+        //   location.protocol = 'https';
+        // }
+        myAlert(timeStamp, address);
+    }
+});
+
+// document.body.appendChild(style); // 这种写法会导致脚本在<iframe>标签的html文档的body标签也被选中
+// self === top 是用来判断当前页面是否是在<iframe>标签内,如果为true则表示不<iframe>标签内
+if (window.self === window.top) {
+    if (document.querySelector('body')) {
+        document.body.appendChild(btn);
+        document.body.appendChild(styleTag);
+    } else {
+        document.documentElement.appendChild(btn);
+        document.documentElement.appendChild(styleTag);
+    }
+}

+ 14 - 0
tmp/monkey/util.js

@@ -53,6 +53,20 @@ function getRandomInt(min, max) {
     return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
 }
 
+// 该函数用于创建一个<eleName k="attrs[k]">text</eleName>样式的页面元素
+function createEle(eleName, text, attrs) {
+    let ele = document.createElement(eleName);
+    // innerText 也就是 <p>text会被添加到这里</p>
+    ele.innerText = text;
+    // attrs 的类型是一个 map
+    for (let k in attrs) {
+        // 遍历 attrs, 给节点 ele 添加我们想要的属性
+        ele.setAttribute(k, attrs[k]);
+    }
+    // 返回节点
+    return ele;
+}
+
 function myCopy(inner_html) {
     let tmpId = "tmpId123123" + getRandomInt(1, 10000);
     let a = document.createElement('div');