// ==UserScript== // 脚本名称 // @name 复制标题和地址(myFirstScript) // @namespace http://tampermonkey.net/ // @version 0.5 // @description 一键复制标题和地址为Markdown格式并带上当前时间(myFirstScript) // @author LiarCoder // 在哪些页面生效, 支持通配符 // @match *://*/* // @grant none // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABTklEQVQ4jY3TO0tcURQF4G/ixFdhCIg2qWysZPwDsUkXSJk2Qso0IpLOTtLERkGwEjQhXV5d0gkGK6v7E/IHgg9EHR0nHLLvzGHujGbB5pzLOXvttfc6twaNRiMta1g1GG28xJfyRlEU6rF/FskpjvA4o7nABD7gM17hY3lYEjzHOd4NqP8QW3gURA+wJzYJI2hV0rq4xtfsexdPcwW3EQmLaSw4wVCQr+M1DtDEJt7iVz1jLRUsB0GOn9iPymWRJ7mChOFY5ysNVFHrHaKwKWEGUyG1HZcnMY4/0UarH0Ez1u+Yq9T8h5tw5BTTg1p4EYfNnuR6WC3srCgo+/odcRdK+7ubrGIRvfeLq7hz3KsgESXPE97HDFKfOUazxLFegst4MAmf7pGfkP6NNNAOwTesYAM/YkgdrzOcYRYLeJMTHMbTTE926T8U7GAb/gI+kkP5n3CsvwAAAABJRU5ErkJggg== // @downloadURL none // ==/UserScript== (function () { 'use msgict'; // 该函数用于创建一个text样式的页面元素 function createEle(eleName, text, attrs) { let ele = document.createElement(eleName); // innerText 也就是

text会被添加到这里

ele.innerText = text; // attrs 的类型是一个 map for (let k in attrs) { // 遍历 attrs, 给节点 ele 添加我们想要的属性 ele.setAttribute(k, attrs[k]); } // 返回节点 return ele; } /** * 复制链接至剪贴板 * * @param text */ const copyToClipboard = (text) => { if (text && text.length) { const textarea = document.createElement("textarea"); textarea.style.background = "transparent"; textarea.style.color = "transparent"; textarea.value = text; document.body.appendChild(textarea); textarea.select(); document.execCommand("copy"); document.body.removeChild(textarea); } }; 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 = ''; 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); copyToClipboard(timeStamp + address); } }); // document.body.appendChild(style); // 这种写法会导致脚本在