Text To link

把文字链接转换为可点击链接

当前为 2014-04-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Text To link
  3. // @description 把文字链接转换为可点击链接
  4. // @namespace Lkytal
  5. // @include *
  6. // @exclude *pan.baidu.com/*
  7. // @exclude *.renren.com/*
  8. // @exclude *userscripts.org/scripts/review/*
  9. // @version 2.3.5
  10. // @icon http://lkytal.qiniudn.com/ic.ico
  11. // @grant unsafeWindow
  12. // ==/UserScript==// Generated by CoffeeScript 1.7.1
  13.  
  14.  
  15. if (window != window.top || window.document.title == "")
  16. return;
  17. var LinkPage, excludedTags, filter, linkify, observer, setLink, url_regexp, xpath;
  18.  
  19. url_regexp = new RegExp("((https?://|www\\.|magnet\\:\\?)[\x21-\x7e]+|([\x21-\x2e\x30-\x7e]+\\.(com|cn|org|net|info|tv)))(/[\x21-\x7e]*)?", "gi");
  20.  
  21. setLink = function(candidate) {
  22. var a, lastLastIndex, match, span, text, url;
  23. if ((candidate == null) || candidate.nodeName === "#cdata-section") {
  24. return;
  25. }
  26. text = candidate.textContent;
  27. lastLastIndex = 0;
  28. match = url_regexp.exec(text);
  29. if (match) {
  30. span = document.createElement("span");
  31. while (match) {
  32. span.appendChild(document.createTextNode(text.substring(lastLastIndex, match.index)));
  33. url = match[0];
  34. if (url.indexOf("http") !== 0 && url.indexOf("magnet") !== 0) {
  35. url = "http://" + url;
  36. }
  37. a = document.createElement("a");
  38. a.setAttribute("href", url);
  39. a.setAttribute("target", "_blank");
  40. a.appendChild(document.createTextNode(match[0]));
  41. span.appendChild(a);
  42. lastLastIndex = url_regexp.lastIndex;
  43. match = url_regexp.exec(text);
  44. }
  45. span.appendChild(document.createTextNode(text.substring(lastLastIndex)));
  46. return candidate.parentNode.replaceChild(span, candidate);
  47. }
  48. };
  49.  
  50. excludedTags = "a,applet,input,button,area,embed,frame,frameset,head,iframe,img,map,meta,noscript,object,option,param,script,select,style,textarea,code".split(",");
  51.  
  52. xpath = "//text()[not(ancestor::" + excludedTags.join(') and not(ancestor::') + ")]";
  53.  
  54. filter = new RegExp("^(textarea|input|button|select|option|meta|link|noscript|a|html|head|object|embed|script|style|frameset|frame|iframe|img|code)$", "i");
  55.  
  56. linkify = function(doc) {
  57. var i, result, _i, _ref;
  58. result = document.evaluate(xpath, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  59. for (i = _i = 0, _ref = result.snapshotLength; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
  60. setLink(result.snapshotItem(i));
  61. }
  62. };
  63.  
  64. LinkPage = function(root) {
  65. var tW;
  66. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
  67. acceptNode: function(a) {
  68. if (!filter.test(a.parentNode.localName)) {
  69. return NodeFilter.FILTER_ACCEPT;
  70. }
  71. }
  72. }, false);
  73. while (tW.nextNode()) {
  74. setLink(tW.currentNode);
  75. }
  76. };
  77.  
  78. window._bodyHeight = document.body.clientHeight;
  79.  
  80. observer = new window.MutationObserver(function(mutations) {
  81. if (document.body.clientHeight < window._bodyHeight) {
  82. return;
  83. }
  84. window._bodyHeight = document.body.clientHeight;
  85. return mutations.forEach(function(mutation) {
  86. var Node, _i, _len, _ref;
  87. _ref = mutation.addedNodes;
  88. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  89. Node = _ref[_i];
  90. LinkPage(Node);
  91. }
  92. });
  93. });
  94.  
  95. observer.observe(document.body, {
  96. childList: true,
  97. subtree: true
  98. });
  99.  
  100. setTimeout((function() {
  101. return linkify(document.body);
  102. }), 200);