Text To link

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

当前为 2015-04-08 提交的版本,查看 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址