Greasy Fork 支持简体中文。

Text To link

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

目前為 2014-06-14 提交的版本,檢視 最新版本

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