Text To link

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

当前为 2014-05-16 提交的版本,查看 最新版本

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

QingJ © 2025

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