Text To link

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

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

QingJ © 2025

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