「水水」复制标题网址

复制当前页面标题及网址,支持复制为 HTML 及 Markdown。「QQ 群:189574683」

目前为 2023-08-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 「水水」复制标题网址
  3. // @namespace https://www.wdssmq.com/
  4. // @version 2.1.3
  5. // @author 沉冰浮水
  6. // @description 复制当前页面标题及网址,支持复制为 HTML 及 Markdown。「QQ 群:189574683」
  7. // @license MIT
  8. // @null ----------------------------
  9. // @contributionURL https://github.com/wdssmq#%E4%BA%8C%E7%BB%B4%E7%A0%81
  10. // @contributionAmount 5.93
  11. // @null ----------------------------
  12. // @link https://github.com/wdssmq/userscript
  13. // @link https://afdian.net/@wdssmq
  14. // @link https://gf.qytechs.cn/zh-CN/users/6865-wdssmq
  15. // @null ----------------------------
  16. // @noframes
  17. // @run-at document-end
  18. // @include http://*
  19. // @include https://*
  20. // @grant GM_registerMenuCommand
  21. // @grant GM_setClipboard
  22. // ==/UserScript==
  23.  
  24. /* eslint-disable */
  25. /* jshint esversion: 6 */
  26.  
  27. (function () {
  28. 'use strict';
  29.  
  30. // -------------------------------------
  31.  
  32. // const $ = window.$ || unsafeWindow.$;
  33. function $n(e) {
  34. return document.querySelector(e);
  35. }
  36. function $na(e) {
  37. return document.querySelectorAll(e);
  38. }
  39.  
  40. // 指定元素内查找子元素
  41. function fnFindDom(el, selector) {
  42. el = typeof el === "string" ? $n(el) : el;
  43. const queryList = el.querySelectorAll(selector);
  44. if (queryList.length === 1) {
  45. return queryList[0];
  46. }
  47. return queryList.length > 1 ? queryList : null;
  48. }
  49.  
  50. function fnReplace(params) {
  51. const { url, title } = params;
  52. // _log("fnReplace", params);
  53. const titleFilter = [
  54. // 贴吧
  55. [/^(.+吧-百度贴吧)--.+/, "$1"],
  56. ];
  57. const urlFilter = [
  58. // QQ
  59. ["?tdsourcetag=s_pctim_aiomsg", ""],
  60. // 哔哩哔哩
  61. [/\?spm_id_from=.+/, ""],
  62. [/\?vd_source=.+/, ""],
  63. ];
  64. let newTitle = title;
  65. let newUrl = url;
  66. titleFilter.forEach((item) => {
  67. newTitle = newTitle.replace(...item);
  68. });
  69. urlFilter.forEach((item) => {
  70. newUrl = newUrl.replace(...item);
  71. });
  72. if (location.host == "gf.qytechs.cn") {
  73. newUrl = newUrl.replace(/(\/\d+)-.+/, "$1");
  74. }
  75. // _log("fnReplace", { url, title }, { newUrl, newTitle });
  76. return { url: newUrl, title: newTitle };
  77. }
  78.  
  79. function fnGetInfo(md = false) {
  80. let { url, title } = fnReplace({
  81. url: document.location.href,
  82. title: document.title.trim(),
  83. });
  84. if (md) {
  85. // eslint-disable-next-line no-useless-escape
  86. title = title.replace(/([_\[\]])/g, "\\$1");
  87. }
  88. return [title, url];
  89. }
  90.  
  91. GM_registerMenuCommand("复制", () => {
  92. const [title, url] = fnGetInfo();
  93. GM_setClipboard(title + "\n" + url);
  94. });
  95.  
  96. GM_registerMenuCommand("复制 HTML", () => {
  97. const [title, url] = fnGetInfo();
  98. GM_setClipboard(
  99. `<p>${title}</p><p><a href="${url}" target="_blank" title="${title}">${url}</a></p>`,
  100. );
  101. });
  102.  
  103. GM_registerMenuCommand("复制为 Markdown「text」", () => {
  104. const [title, url] = fnGetInfo(true);
  105. GM_setClipboard(`[${title}](${url} "${title}")`);
  106. });
  107.  
  108. GM_registerMenuCommand("复制为 Markdown「link」", () => {
  109. const [title, url] = fnGetInfo(true);
  110. GM_setClipboard(`${title}:\n\n[${url}](${url} "${title}")`);
  111. });
  112.  
  113. const tplMarkQuote = `
  114. > {title}
  115. >
  116. > [{url}]({url} "{title}")
  117. `;
  118.  
  119. GM_registerMenuCommand("复制为 Markdown「引用」", () => {
  120. const [title, url] = fnGetInfo(true);
  121. GM_setClipboard(tplMarkQuote.replace(/\{title\}/g, title).replace(/\{url\}/g, url));
  122. });
  123.  
  124. // 指定元素中的链接增加 target="_blank"
  125. const config = [
  126. [".markdown_body", ".reply_content"],
  127. ["#additional-info"],
  128. ["div.forum_table table"],
  129. ];
  130.  
  131. const fnSetBlank = ($a) => {
  132. $a.setAttribute("target", "_blank");
  133. };
  134.  
  135. config.forEach((e) => {
  136. const selector = e.join(",");
  137. const $$container = $na(selector);
  138. // // print $$container
  139. // _log($$container);
  140. // 遍历 $$container
  141. [].forEach.call($$container, ($el) => {
  142. const $$a = fnFindDom($el, "a");
  143. // _log($$a);
  144. if ($$a.length > 0) {
  145. [].map.call($$a, fnSetBlank);
  146. }
  147. });
  148. });
  149.  
  150. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址