Text To link

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

当前为 2017-09-29 提交的版本,查看 最新版本

  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 *play.google.com/music/*
  13. // @exclude *mail.google.com/*
  14. // @exclude *docs.google.com/*
  15. // @exclude *www.google.*
  16. // @exclude *acid3.acidtests.org/*
  17. // @exclude *.163.com/*
  18. // @exclude *.alipay.com/*
  19. // @version 2.8.4
  20. // @icon http://lkytal.qiniudn.com/link.png
  21. // @grant unsafeWindow
  22. // @homepageURL https://git.oschina.net/coldfire/GM
  23. // ==/UserScript==
  24.  
  25. "use strict";
  26. ;
  27. var clearLink, excludedTags, linkFilter, linkMixInit, linkPack, linkify, observePage, observer, setLink, urlPrefixes, url_regexp, xPath;
  28.  
  29. url_regexp = /((https?:\/\/|www\.)[\x21-\x7e]+[\w\/]|(\w[\w._-]+\.(com|cn|org|net|info|tv|cc|gov|edu))(\/[\x21-\x7e]*[\w\/])?|ed2k:\/\/[\x21-\x7e]+\|\/|thunder:\/\/[\x21-\x7e]+=)/gi;
  30.  
  31. urlPrefixes = ['http://', 'https://', 'ftp://', 'thunder://', 'ed2k://'];
  32.  
  33. clearLink = function (event) {
  34. var j, len, link, prefix, ref, url;
  35. link = (ref = event.originalTarget) != null ? ref : event.target;
  36. if (!(link != null && link.localName === "a" && link.className.indexOf("textToLink") !== -1)) {
  37. return;
  38. }
  39. url = link.getAttribute("href");
  40. //console.log url
  41. for (j = 0, len = urlPrefixes.length; j < len; j++) {
  42. prefix = urlPrefixes[j];
  43. if (url.indexOf(prefix) === 0) {
  44. return;
  45. }
  46. }
  47. return link.setAttribute("href", "http://" + url);
  48. };
  49.  
  50. document.addEventListener("mouseover", clearLink);
  51.  
  52. setLink = function (candidate) {
  53. var span, text;
  54. if (candidate == null || candidate.parentNode.className.indexOf("textToLink") !== -1 || candidate.nodeName === "#cdata-section") {
  55. return;
  56. }
  57. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" class="textToLink">$1</a>');
  58. if (candidate.textContent.length === text.length) {
  59. return;
  60. }
  61. span = document.createElement("span");
  62. span.innerHTML = text;
  63. return candidate.parentNode.replaceChild(span, candidate);
  64. };
  65.  
  66. 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(",");
  67.  
  68. xPath = '//text()[not(ancestor::' + excludedTags.join(') and not(ancestor::') + ')]';
  69.  
  70. linkPack = function (result, start) {
  71. var i, j, k, ref, ref1, ref2, ref3, startTime;
  72. startTime = Date.now();
  73. while (start + 10000 < result.snapshotLength) {
  74. for (i = j = ref = start, ref1 = start + 10000; ref <= ref1 ? j <= ref1 : j >= ref1; i = ref <= ref1 ? ++j : --j) {
  75. setLink(result.snapshotItem(i));
  76. }
  77. start += 10000;
  78. if (Date.now() - startTime > 2500) {
  79. return;
  80. }
  81. }
  82. for (i = k = ref2 = start, ref3 = result.snapshotLength; ref2 <= ref3 ? k <= ref3 : k >= ref3; i = ref2 <= ref3 ? ++k : --k) {
  83. setLink(result.snapshotItem(i));
  84. }
  85. };
  86.  
  87. linkify = function (node) {
  88. var result;
  89. result = document.evaluate(xPath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  90. return linkPack(result, 0);
  91. };
  92.  
  93. linkFilter = function (node) {
  94. var j, len, tag;
  95. for (j = 0, len = excludedTags.length; j < len; j++) {
  96. tag = excludedTags[j];
  97. if (tag === node.parentNode.localName.toLowerCase()) {
  98. return NodeFilter.FILTER_REJECT;
  99. }
  100. }
  101. return NodeFilter.FILTER_ACCEPT;
  102. };
  103.  
  104. observePage = function (root) {
  105. var tW;
  106. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, { //+ NodeFilter.SHOW_ELEMENT,
  107. acceptNode: linkFilter
  108. }, false);
  109. while (tW.nextNode()) {
  110. setLink(tW.currentNode);
  111. }
  112. };
  113.  
  114. observer = new window.MutationObserver(function (mutations) {
  115. var Node, j, k, len, len1, mutation, ref;
  116. for (j = 0, len = mutations.length; j < len; j++) {
  117. mutation = mutations[j];
  118. if (mutation.type === "childList") {
  119. ref = mutation.addedNodes;
  120. for (k = 0, len1 = ref.length; k < len1; k++) {
  121. Node = ref[k];
  122. observePage(Node);
  123. }
  124. }
  125. }
  126. });
  127.  
  128. linkMixInit = function () {
  129. if (window !== window.top || window.document.title === "") {
  130. return;
  131. }
  132. //console.time('a')
  133. linkify(document.body);
  134. //console.timeEnd('a')
  135. return observer.observe(document.body, {
  136. childList: true,
  137. subtree: true
  138. });
  139. };
  140.  
  141. setTimeout(linkMixInit, 100);