Text To link

Turn plain text URLs into clickable links, linkify with high speed & accuracy 把文字链接转换为可点击链接

当前为 2015-02-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Text To link
  3. // @description Turn plain text URLs into clickable links, linkify with high speed & accuracy 把文字链接转换为可点击链接
  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. // @version 2.6.5
  16. // @icon http://lkytal.qiniudn.com/ic.ico
  17. // @grant unsafeWindow
  18. // @homepageURL https://git.oschina.net/coldfire/GM
  19. // ==/UserScript==
  20.  
  21. "use strict";
  22. var excludedTags, filter, linkMixInit, linkPack, linkify, observePage, observer, setHttp, setLink, url_regexp, xpath;
  23.  
  24. 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;
  25.  
  26. setHttp = function(event) {
  27. var url;
  28. url = event.target.getAttribute("href");
  29. if (url.indexOf("http") !== 0 && url.indexOf("ed2k://") !== 0 && url.indexOf("thunder://") !== 0) {
  30. return event.target.setAttribute("href", "http://" + url);
  31. }
  32. };
  33.  
  34. if (typeof exportFunction !== "undefined" && exportFunction !== null) {
  35. exportFunction(setHttp, unsafeWindow, {
  36. defineAs: "setHttp"
  37. });
  38. } else {
  39. unsafeWindow.setHttp = setHttp;
  40. }
  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" onmouseover="setHttp(event);">$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);