Text To link

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

当前为 2014-06-22 提交的版本,查看 最新版本

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

QingJ © 2025

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