Text To link

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

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

  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.7.3
  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, filter, 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))(\/[\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. filter = new RegExp("^(" + (excludedTags.join('|')) + ")$", "i");
  61.  
  62. linkPack = function(result, start) {
  63. var i, j, k, ref, ref1, ref2, ref3;
  64. if (start + 10000 < result.snapshotLength) {
  65. for (i = j = ref = start, ref1 = start + 10000; ref <= ref1 ? j <= ref1 : j >= ref1; i = ref <= ref1 ? ++j : --j) {
  66. setLink(result.snapshotItem(i));
  67. }
  68. setTimeout(function() {
  69. return linkPack(result, start + 10000);
  70. }, 15);
  71. } else {
  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.  
  78. linkify = function(doc) {
  79. var result;
  80. result = document.evaluate(xpath, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  81. return linkPack(result, 0);
  82. };
  83.  
  84. observePage = function(root) {
  85. var tW;
  86. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
  87. acceptNode: function(a) {
  88. if (!filter.test(a.parentNode.localName)) {
  89. return NodeFilter.FILTER_ACCEPT;
  90. }
  91. }
  92. }, false);
  93. while (tW.nextNode()) {
  94. setLink(tW.currentNode);
  95. }
  96. };
  97.  
  98. observer = new window.MutationObserver(function(mutations) {
  99. var Node, j, k, len, len1, mutation, ref;
  100. for (j = 0, len = mutations.length; j < len; j++) {
  101. mutation = mutations[j];
  102. if (mutation.type === "childList") {
  103. ref = mutation.addedNodes;
  104. for (k = 0, len1 = ref.length; k < len1; k++) {
  105. Node = ref[k];
  106. observePage(Node);
  107. }
  108. }
  109. }
  110. });
  111.  
  112. linkMixInit = function() {
  113. if (window !== window.top || window.document.title === "") {
  114. return;
  115. }
  116. linkify(document.body);
  117. return observer.observe(document.body, {
  118. childList: true,
  119. subtree: true
  120. });
  121. };
  122.  
  123. setTimeout(linkMixInit, 100);