Text To link

Turn plain text URLs into clickable links

当前为 2019-09-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Text To link
  3. // @description Turn plain text URLs into clickable links
  4. // @description:zh 把文字链接转换为可点击链接
  5. // @author lkytal
  6. // @namespace Lkytal
  7. // @version 2.8.7
  8. // @homepage https://lkytal.github.io/
  9. // @homepageURL https://lkytal.github.io/GM
  10. // @license AGPL
  11. // @include *
  12. // @exclude *pan.baidu.com/*
  13. // @exclude *renren.com/*
  14. // @exclude *exhentai.org/*
  15. // @exclude *music.google.com/*
  16. // @exclude *play.google.com/music/*
  17. // @exclude *mail.google.com/*
  18. // @exclude *docs.google.com/*
  19. // @exclude *www.google.*
  20. // @exclude *acid3.acidtests.org/*
  21. // @exclude *.163.com/*
  22. // @exclude *.alipay.com/*
  23. // @run-at document-end
  24. // @icon https://github.com/lkytal/GM/raw/master/icons/link.png
  25. // @grant unsafeWindow
  26. // @charset UTF-8
  27. // @supportURL https://github.com/lkytal/GM/issues
  28. // ==/UserScript==
  29. "use strict";
  30.  
  31. ;
  32. var clearLink, excludedTags, linkFilter, linkMixInit, linkPack, linkify, observePage, observer, setLink, urlPrefixes, url_regexp, xPath;
  33. url_regexp = /((https?:\/\/|www\.)[\x21-\x7e]+[\w\/=]|\w([\w._-])+@\w[\w\._-]+\.(com|cn|org|net|info|tv|cc|gov|edu)|(\w[\w._-]+\.(com|cn|org|net|info|tv|cc|gov|edu))(\/[\x21-\x7e]*[\w\/])?|ed2k:\/\/[\x21-\x7e]+\|\/|thunder:\/\/[\x21-\x7e]+=)/gi;
  34. urlPrefixes = ['http://', 'https://', 'ftp://', 'thunder://', 'ed2k://', 'mailto://', 'file://'];
  35.  
  36. clearLink = function (event) {
  37. var j, len, link, prefix, ref, ref1, url;
  38. link = (ref = event.originalTarget) != null ? ref : event.target;
  39.  
  40. if (!(link != null && link.localName === "a" && ((ref1 = link.className) != null ? ref1.indexOf("textToLink") : void 0) !== -1)) {
  41. return;
  42. }
  43.  
  44. url = link.getAttribute("href"); //console.log url
  45.  
  46. for (j = 0, len = urlPrefixes.length; j < len; j++) {
  47. prefix = urlPrefixes[j];
  48.  
  49. if (url.indexOf(prefix) === 0) {
  50. return;
  51. }
  52. }
  53.  
  54. if (url.indexOf('@') !== -1) {
  55. return link.setAttribute("href", "mailto://" + url);
  56. } else {
  57. return link.setAttribute("href", "http://" + url);
  58. }
  59. };
  60.  
  61. document.addEventListener("mouseover", clearLink);
  62.  
  63. setLink = function (candidate) {
  64. var ref, ref1, ref2, span, text;
  65.  
  66. if (candidate == null || ((ref = candidate.parentNode) != null ? (ref1 = ref.className) != null ? typeof ref1.indexOf === "function" ? ref1.indexOf("textToLink") : void 0 : void 0 : void 0) !== -1 || candidate.nodeName === "#cdata-section") {
  67. return;
  68. }
  69.  
  70. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" class="textToLink">$1</a>');
  71.  
  72. if (((ref2 = candidate.textContent) != null ? ref2.length : void 0) === text.length) {
  73. return;
  74. }
  75.  
  76. span = document.createElement("span");
  77. span.innerHTML = text;
  78. return candidate.parentNode.replaceChild(span, candidate);
  79. };
  80.  
  81. excludedTags = "a,svg,canvas,applet,input,button,area,pre,embed,frame,frameset,head,iframe,img,option,map,meta,noscript,object,script,style,textarea,code".split(",");
  82. xPath = "//text()[not(ancestor::" + excludedTags.join(') and not(ancestor::') + ")]";
  83.  
  84. linkPack = function (result, start) {
  85. var i, j, k, ref, ref1, ref2, ref3, startTime;
  86. startTime = Date.now();
  87.  
  88. while (start + 10000 < result.snapshotLength) {
  89. for (i = j = ref = start, ref1 = start + 10000; ref <= ref1 ? j <= ref1 : j >= ref1; i = ref <= ref1 ? ++j : --j) {
  90. setLink(result.snapshotItem(i));
  91. }
  92.  
  93. start += 10000;
  94.  
  95. if (Date.now() - startTime > 2500) {
  96. return;
  97. }
  98. }
  99.  
  100. for (i = k = ref2 = start, ref3 = result.snapshotLength; ref2 <= ref3 ? k <= ref3 : k >= ref3; i = ref2 <= ref3 ? ++k : --k) {
  101. setLink(result.snapshotItem(i));
  102. }
  103. };
  104.  
  105. linkify = function (node) {
  106. var result;
  107. result = document.evaluate(xPath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  108. return linkPack(result, 0);
  109. };
  110.  
  111. linkFilter = function (node) {
  112. var j, len, tag;
  113.  
  114. for (j = 0, len = excludedTags.length; j < len; j++) {
  115. tag = excludedTags[j];
  116.  
  117. if (tag === node.parentNode.localName.toLowerCase()) {
  118. return NodeFilter.FILTER_REJECT;
  119. }
  120. }
  121.  
  122. return NodeFilter.FILTER_ACCEPT;
  123. };
  124.  
  125. observePage = function (root) {
  126. var tW;
  127. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
  128. //+ NodeFilter.SHOW_ELEMENT,
  129. acceptNode: linkFilter
  130. }, false);
  131.  
  132. while (tW.nextNode()) {
  133. setLink(tW.currentNode);
  134. }
  135. };
  136.  
  137. observer = new window.MutationObserver(function (mutations) {
  138. var Node, j, k, len, len1, mutation, ref;
  139.  
  140. for (j = 0, len = mutations.length; j < len; j++) {
  141. mutation = mutations[j];
  142.  
  143. if (mutation.type === "childList") {
  144. ref = mutation.addedNodes;
  145.  
  146. for (k = 0, len1 = ref.length; k < len1; k++) {
  147. Node = ref[k];
  148. observePage(Node);
  149. }
  150. }
  151. }
  152. });
  153.  
  154. linkMixInit = function () {
  155. if (window !== window.top || window.document.title === "") {
  156. return;
  157. } //console.time('a')
  158.  
  159.  
  160. linkify(document.body); //console.timeEnd('a')
  161.  
  162. return observer.observe(document.body, {
  163. childList: true,
  164. subtree: true
  165. });
  166. };
  167.  
  168. setTimeout(linkMixInit, 100);