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

QingJ © 2025

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