Text To link

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

当前为 2017-05-16 提交的版本,查看 最新版本

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