Text To link

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

目前為 2015-02-21 提交的版本,檢視 最新版本

  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. // @version 2.6.4
  15. // @icon http://lkytal.qiniudn.com/ic.ico
  16. // @grant unsafeWindow
  17. // @homepageURL https://git.oschina.net/coldfire/GM
  18. // ==/UserScript==
  19.  
  20. "use strict";
  21. var excludedTags, filter, linkMixInit, linkPack, linkify, observePage, observer, setHttp, setLink, url_regexp, xpath;
  22.  
  23. 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;
  24.  
  25. setHttp = function(event) {
  26. var url;
  27. url = event.target.getAttribute("href");
  28. if (url.indexOf("http") !== 0 && url.indexOf("ed2k://") !== 0 && url.indexOf("thunder://") !== 0) {
  29. return event.target.setAttribute("href", "http://" + url);
  30. }
  31. };
  32.  
  33. if (typeof exportFunction !== "undefined" && exportFunction !== null) {
  34. exportFunction(setHttp, unsafeWindow, {
  35. defineAs: "setHttp"
  36. });
  37. } else {
  38. unsafeWindow.setHttp = setHttp;
  39. }
  40.  
  41. setLink = function(candidate) {
  42. var span, text;
  43. if ((candidate == null) || candidate.nodeName === "#cdata-section") {
  44. return;
  45. }
  46. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" class="texttolink" onmouseover="setHttp(event);">$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, _i, _j, _ref, _ref1;
  63. if (start + 10000 < result.snapshotLength) {
  64. for (i = _i = start, _ref = start + 10000; start <= _ref ? _i <= _ref : _i >= _ref; i = start <= _ref ? ++_i : --_i) {
  65. setLink(result.snapshotItem(i));
  66. }
  67. setTimeout(function() {
  68. return linkPack(result, start + 10000);
  69. }, 15);
  70. } else {
  71. for (i = _j = start, _ref1 = result.snapshotLength; start <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = start <= _ref1 ? ++_j : --_j) {
  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, mutation, _i, _j, _len, _len1, _ref;
  99. for (_i = 0, _len = mutations.length; _i < _len; _i++) {
  100. mutation = mutations[_i];
  101. if (mutation.type === "childList") {
  102. _ref = mutation.addedNodes;
  103. for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
  104. Node = _ref[_j];
  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);