Text To link

Turn plain text URLs into clickable links, 把文字链接转换为可点击链接

当前为 2017-08-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Text To link
  3. // @description Turn plain text URLs into clickable links, 把文字链接转换为可点击链接
  4. // @author lkytal
  5. // @namespace Lkytal
  6. // @homepage https://lkytal.github.io/
  7. // @include *
  8. // @exclude *pan.baidu.com/*
  9. // @exclude *renren.com/*
  10. // @exclude *exhentai.org/*
  11. // @exclude *music.google.com/*
  12. // @exclude *play.google.com/music/*
  13. // @exclude *mail.google.com/*
  14. // @exclude *docs.google.com/*
  15. // @exclude *www.google.*
  16. // @exclude *acid3.acidtests.org/*
  17. // @exclude *.163.com/*
  18. // @version 2.8.2
  19. // @icon http://lkytal.qiniudn.com/link.png
  20. // @grant unsafeWindow
  21. // @homepageURL https://git.oschina.net/coldfire/GM
  22. // ==/UserScript==
  23.  
  24. "use strict";
  25. var clearLink, excludedTags, linkFilter, linkMixInit, linkPack, linkify, observePage, observer, setLink, urlPrefixes, url_regexp, xPath;
  26.  
  27. url_regexp = /((https?:\/\/|www\.)[\x21-\x7e]+[\w\/]|(\w[\w._-]+\.(com|cn|org|net|info|tv|cc|gov|xxx))(\/[\x21-\x7e]*[\w\/])?|ed2k:\/\/[\x21-\x7e]+\|\/|thunder:\/\/[\x21-\x7e]+=)/gi;
  28.  
  29. urlPrefixes = ['http://', 'https://', 'ftp://', 'thunder://', 'ed2k://'];
  30.  
  31. clearLink = function(event) {
  32. var j, len, link, prefix, ref, url;
  33. link = (ref = event.originalTarget) != null ? ref : event.target;
  34. if (!((link != null) && link.localName === "a" && link.className.indexOf("textToLink") !== -1)) {
  35. return;
  36. }
  37. url = link.getAttribute("href");
  38. for (j = 0, len = urlPrefixes.length; j < len; j++) {
  39. prefix = urlPrefixes[j];
  40. if (url.indexOf(prefix) === 0) {
  41. return;
  42. }
  43. }
  44. return link.setAttribute("href", "http://" + url);
  45. };
  46.  
  47. document.addEventListener("mouseover", clearLink);
  48.  
  49. setLink = function(candidate) {
  50. var span, text;
  51. if ((candidate == null) || candidate.parentNode.className.indexOf("textToLink") !== -1 || candidate.nodeName === "#cdata-section") {
  52. return;
  53. }
  54. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" class="textToLink">$1</a>');
  55. if (candidate.textContent.length === text.length) {
  56. return;
  57. }
  58. span = document.createElement("span");
  59. span.innerHTML = text;
  60. return candidate.parentNode.replaceChild(span, candidate);
  61. };
  62.  
  63. 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(",");
  64.  
  65. xPath = "//text()[not(ancestor::" + (excludedTags.join(') and not(ancestor::')) + ")]";
  66.  
  67. linkPack = function(result, start) {
  68. var i, j, k, ref, ref1, ref2, ref3, startTime;
  69. startTime = Date.now();
  70. while (start + 10000 < result.snapshotLength) {
  71. for (i = j = ref = start, ref1 = start + 10000; ref <= ref1 ? j <= ref1 : j >= ref1; i = ref <= ref1 ? ++j : --j) {
  72. setLink(result.snapshotItem(i));
  73. }
  74. start += 10000;
  75. if (Date.now() - startTime > 2500) {
  76. return;
  77. }
  78. }
  79. for (i = k = ref2 = start, ref3 = result.snapshotLength; ref2 <= ref3 ? k <= ref3 : k >= ref3; i = ref2 <= ref3 ? ++k : --k) {
  80. setLink(result.snapshotItem(i));
  81. }
  82. };
  83.  
  84. linkify = function(node) {
  85. var result;
  86. result = document.evaluate(xPath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  87. return linkPack(result, 0);
  88. };
  89.  
  90. linkFilter = function(node) {
  91. var j, len, tag;
  92. for (j = 0, len = excludedTags.length; j < len; j++) {
  93. tag = excludedTags[j];
  94. if (tag === node.parentNode.localName.toLowerCase()) {
  95. return NodeFilter.FILTER_REJECT;
  96. }
  97. }
  98. return NodeFilter.FILTER_ACCEPT;
  99. };
  100.  
  101. observePage = function(root) {
  102. var tW;
  103. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
  104. acceptNode: linkFilter
  105. }, false);
  106. while (tW.nextNode()) {
  107. setLink(tW.currentNode);
  108. }
  109. };
  110.  
  111. observer = new window.MutationObserver(function(mutations) {
  112. var Node, j, k, len, len1, mutation, ref;
  113. for (j = 0, len = mutations.length; j < len; j++) {
  114. mutation = mutations[j];
  115. if (mutation.type === "childList") {
  116. ref = mutation.addedNodes;
  117. for (k = 0, len1 = ref.length; k < len1; k++) {
  118. Node = ref[k];
  119. observePage(Node);
  120. }
  121. }
  122. }
  123. });
  124.  
  125. linkMixInit = function() {
  126. if (window !== window.top || window.document.title === "") {
  127. return;
  128. }
  129. linkify(document.body);
  130. return observer.observe(document.body, {
  131. childList: true,
  132. subtree: true
  133. });
  134. };
  135.  
  136. setTimeout(linkMixInit, 100);