Text To link

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

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

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