Text To link

Turn plain text URLs into clickable links

  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.  
  30. "use strict";
  31. ;
  32. var clearLink, excludedTags, linkFilter, linkMixInit, linkPack, linkify, observePage, observer, setLink, urlPrefixes, url_regexp, xPath;
  33.  
  34. 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;
  35.  
  36. urlPrefixes = ['http://', 'https://', 'ftp://', 'thunder://', 'ed2k://', 'mailto://', 'file://'];
  37.  
  38. clearLink = function (event) {
  39. var j, len, link, prefix, ref, ref1, url;
  40. link = (ref = event.originalTarget) != null ? ref : event.target;
  41. if (!(link != null && link.localName === "a" && ((ref1 = link.className) != null ? ref1.indexOf("textToLink") : void 0) !== -1)) {
  42. return;
  43. }
  44. url = link.getAttribute("href");
  45. //console.log url
  46. for (j = 0, len = urlPrefixes.length; j < len; j++) {
  47. prefix = urlPrefixes[j];
  48. if (url.indexOf(prefix) === 0) {
  49. return;
  50. }
  51. }
  52. if (url.indexOf('@') !== -1) {
  53. return link.setAttribute("href", "mailto://" + url);
  54. } else {
  55. return link.setAttribute("href", "http://" + url);
  56. }
  57. };
  58.  
  59. document.addEventListener("mouseover", clearLink);
  60.  
  61. setLink = function (candidate) {
  62. var ref, ref1, ref2, span, text;
  63. 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") {
  64. return;
  65. }
  66. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" class="textToLink">$1</a>');
  67. if (((ref2 = candidate.textContent) != null ? ref2.length : void 0) === text.length) {
  68. return;
  69. }
  70. span = document.createElement("span");
  71. span.innerHTML = text;
  72. return candidate.parentNode.replaceChild(span, candidate);
  73. };
  74.  
  75. 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(",");
  76.  
  77. xPath = `//text()[not(ancestor::${excludedTags.join(') and not(ancestor::')})]`;
  78.  
  79. linkPack = function (result, start) {
  80. var i, j, k, ref, ref1, ref2, ref3, startTime;
  81. startTime = Date.now();
  82. while (start + 10000 < result.snapshotLength) {
  83. for (i = j = ref = start, ref1 = start + 10000; ref <= ref1 ? j <= ref1 : j >= ref1; i = ref <= ref1 ? ++j : --j) {
  84. setLink(result.snapshotItem(i));
  85. }
  86. start += 10000;
  87. if (Date.now() - startTime > 2500) {
  88. return;
  89. }
  90. }
  91. for (i = k = ref2 = start, ref3 = result.snapshotLength; ref2 <= ref3 ? k <= ref3 : k >= ref3; i = ref2 <= ref3 ? ++k : --k) {
  92. setLink(result.snapshotItem(i));
  93. }
  94. };
  95.  
  96. linkify = function (node) {
  97. var result;
  98. result = document.evaluate(xPath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  99. return linkPack(result, 0);
  100. };
  101.  
  102. linkFilter = function (node) {
  103. var j, len, tag;
  104. for (j = 0, len = excludedTags.length; j < len; j++) {
  105. tag = excludedTags[j];
  106. if (tag === node.parentNode.localName.toLowerCase()) {
  107. return NodeFilter.FILTER_REJECT;
  108. }
  109. }
  110. return NodeFilter.FILTER_ACCEPT;
  111. };
  112.  
  113. observePage = function (root) {
  114. var tW;
  115. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, { //+ NodeFilter.SHOW_ELEMENT,
  116. acceptNode: linkFilter
  117. }, false);
  118. while (tW.nextNode()) {
  119. setLink(tW.currentNode);
  120. }
  121. };
  122.  
  123. observer = new window.MutationObserver(function (mutations) {
  124. var Node, j, k, len, len1, mutation, ref;
  125. for (j = 0, len = mutations.length; j < len; j++) {
  126. mutation = mutations[j];
  127. if (mutation.type === "childList") {
  128. ref = mutation.addedNodes;
  129. for (k = 0, len1 = ref.length; k < len1; k++) {
  130. Node = ref[k];
  131. observePage(Node);
  132. }
  133. }
  134. }
  135. });
  136.  
  137. linkMixInit = function () {
  138. if (window !== window.top || window.document.title === "") {
  139. return;
  140. }
  141. //console.time('a')
  142. linkify(document.body);
  143. //console.timeEnd('a')
  144. return observer.observe(document.body, {
  145. childList: true,
  146. subtree: true
  147. });
  148. };
  149.  
  150. setTimeout(linkMixInit, 100);