Text To link

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

当前为 2017-07-11 提交的版本,查看 最新版本

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