AAAAA-复制标题-到memory.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // ==UserScript==
  2. // @name AAAAA-复制标题-到memory
  3. // @namespace Violentmonkey Scripts
  4. // @icon https://memory.tianyunperfect.cn/pro_icon.svg
  5. // @match *://*/*
  6. // @exclude https://memory.tianyunperfect.cn/*
  7. // @exclude https://tracert.alipay.com/*
  8. // @exclude https://t.captcha.qq.com/*
  9. // @grant GM_registerMenuCommand
  10. // @version 1.0
  11. // @author tianyunperfect
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js
  13. // @require http://www.tianyunperfect.cn:3001/tianyunperfect/web-base/raw/master/tmp/monkey/util.js?a=1
  14. // @description 2021/1/30 下午7:09:51
  15. // ==/UserScript==
  16. (function () {
  17. 'use strict';
  18. GM_registerMenuCommand("!!!复制并加到memory", to_memory)
  19. GM_registerMenuCommand("!!!复制标题和 url", copyTitle)
  20. function to_memory() {
  21. let url = location.href;
  22. let title = document.title;
  23. if (window.self !== window.top) {
  24. return
  25. }
  26. let body = {
  27. "back": "<p></p>",
  28. "front": `<p>url: <a href="${url}">${title}</a></p>`,
  29. "period": 0,
  30. "onlyText": "",
  31. "remindTime": new Date(),
  32. "tag": "",
  33. "updateTime": "",
  34. "userId": 1
  35. };
  36. let memory_url = "https://memory.tianyunperfect.cn/api/memory-64B206F1-E915-4298-9AB7-9C561040B012/insert";
  37. axios.post(memory_url, body).then((res) => {
  38. console.log(res.data);
  39. let data = res.data;
  40. if (data.success) {
  41. } else {
  42. alert(data.message);
  43. }
  44. })
  45. myAlert(`${title}<br/>${location.host}`, 2)
  46. }
  47. function copyTitle() {
  48. myCopy(`<a href="${location.href}">${document.title}</a>`)
  49. }
  50. document.onkeydown = function (ev) {
  51. if (ev.key === 'c' && ev.shiftKey) {
  52. ev.preventDefault() // 关闭浏览器快捷键
  53. copyTitle()
  54. }
  55. }
  56. })();
  57. let btnStyle = `
  58. #copy-title-and-location {
  59. position: fixed; top: 100px; left: -95px; opacity: 0.3; z-index: 2147483647;
  60. background-image: none; cursor:pointer; color: #fff; background-color: #0084ff !important;
  61. margin: 5px 0px; width: auto; border-radius: 3px; border: #0084ff; outline: none; padding: 3px 6px; height: 26px;
  62. font-family: Arial, sans-serif; font-size: 12px; transition: left, 0.5s;
  63. }
  64. #copy-title-and-location:hover {left: 0px; opacity: 1;}
  65. #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;}`;
  66. let styleTag = createEle('style', btnStyle, { type: "text/css" });
  67. // 将按钮图标由原来的img改为了svg,以增强适应性,同时也将对svg的样式设置移到了上面的 btnStyle 中
  68. 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>';
  69. let btn = createEle('button', '', { id: "copy-title-and-location" });
  70. btn.innerHTML = '复制标题和地址' + iconSVG;
  71. btn.addEventListener('click', () => {
  72. let date = new Date();
  73. let timeStamp = '更新:' + date.toLocaleDateString().replace('\/', '年').replace('\/', '月') + '日' + date.toLocaleTimeString('chinese', { hour12: false });
  74. let titleInfo = document.querySelector('title').innerText;
  75. let address = '\n> 参考:[' + titleInfo + ']' + '(' + location + ')';
  76. // 匹配微信公众号的文章地址
  77. let regWeChat = /https:\/\/mp.weixin.qq.com\//;
  78. if (regWeChat.test(location.toString())) {
  79. let officialAccount = document.getElementById('js_name');
  80. let publishDate = document.getElementById('publish_time');
  81. publishDate.click();
  82. address = '\n> 参考:[【微信公众号:' + officialAccount.innerText + ' ' + publishDate.innerText + '】' + titleInfo + ']' + '(' + location + ')';
  83. }
  84. // 【更新:2021年9月12日22:48:36】添加了一个try catch来应对当前页面不能访问 navigator.clipboard 对象的问题
  85. try {
  86. navigator.clipboard.writeText(timeStamp + address);
  87. } catch (err) {
  88. console.log('当前页面不支持访问 navigator.clipboard 对象:' + err);
  89. // if(/www.ruanyifeng.com/.test(location.toString())) {
  90. // location.protocol = 'https';
  91. // }
  92. myAlert(timeStamp, address);
  93. }
  94. });
  95. // document.body.appendChild(style); // 这种写法会导致脚本在<iframe>标签的html文档的body标签也被选中
  96. // self === top 是用来判断当前页面是否是在<iframe>标签内,如果为true则表示不<iframe>标签内
  97. if (window.self === window.top) {
  98. if (document.querySelector('body')) {
  99. document.body.appendChild(btn);
  100. document.body.appendChild(styleTag);
  101. } else {
  102. document.documentElement.appendChild(btn);
  103. document.documentElement.appendChild(styleTag);
  104. }
  105. }