Text To link

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

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

  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.5.1
  13. // @icon http://lkytal.qiniudn.com/ic.ico
  14. // @grant unsafeWindow
  15. // @homepageURL https://git.oschina.net/coldfire/GM
  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, setHttp, setLink, url_regexp, xpath;
  23.  
  24. url_regexp = new RegExp("((https?://|www\\.|magnet\\:\\?)[\\x21-\\x7e]+[0-9a-zA-Z]|(\\w[\\x21-\\x2e\\x30-\\x7e]+\\.(com|cn|org|net|info|tv))(/[\\x21-\\x7e]*[0-9a-zA-Z])?)", "gi");
  25.  
  26. setHttp = function(event) {
  27. var url;
  28. url = event.target.getAttribute("href");
  29. if (url.indexOf("http") !== 0 && url.indexOf("magnet:?") !== 0) {
  30. return event.target.setAttribute("href", "http://" + url);
  31. }
  32. };
  33.  
  34. if (typeof exportFunction !== "undefined" && exportFunction !== null) {
  35. exportFunction(setHttp, unsafeWindow, {
  36. defineAs: "setHttp"
  37. });
  38. } else {
  39. unsafeWindow.setHttp = setHttp;
  40. }
  41.  
  42. setLink = function(candidate) {
  43. var span, text;
  44. if ((candidate == null) || candidate.nodeName === "#cdata-section") {
  45. return;
  46. }
  47. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" onmouseover="setHttp(event);">$1</a>');
  48. if (candidate.textContent.length === text.length) {
  49. return;
  50. }
  51. span = document.createElement("span");
  52. span.innerHTML = text;
  53. return candidate.parentNode.replaceChild(span, candidate);
  54. };
  55.  
  56. 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(",");
  57.  
  58. xpath = "//text()[not(ancestor::" + excludedTags.join(') and not(ancestor::') + ")]";
  59.  
  60. 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");
  61.  
  62. linkPack = function(result, start) {
  63. var i, _i, _j, _ref, _ref1;
  64. if (start + 10000 < result.snapshotLength) {
  65. for (i = _i = start, _ref = start + 10000; start <= _ref ? _i <= _ref : _i >= _ref; i = start <= _ref ? ++_i : --_i) {
  66. setLink(result.snapshotItem(i));
  67. }
  68. setTimeout(function() {
  69. return linkPack(result, start + 10000);
  70. }, 50);
  71. } else {
  72. for (i = _j = start, _ref1 = result.snapshotLength; start <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = start <= _ref1 ? ++_j : --_j) {
  73. setLink(result.snapshotItem(i));
  74. }
  75. }
  76. };
  77.  
  78. linkify = function(doc) {
  79. var result;
  80. result = document.evaluate(xpath, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  81. return linkPack(result, 0);
  82. };
  83.  
  84. LinkPage = function(root) {
  85. var tW;
  86. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
  87. acceptNode: function(a) {
  88. if (!filter.test(a.parentNode.localName)) {
  89. return NodeFilter.FILTER_ACCEPT;
  90. }
  91. }
  92. }, false);
  93. while (tW.nextNode()) {
  94. setLink(tW.currentNode);
  95. }
  96. };
  97.  
  98. observer = new window.MutationObserver(function(mutations) {
  99. var Node, mutation, _i, _j, _len, _len1, _ref;
  100. for (_i = 0, _len = mutations.length; _i < _len; _i++) {
  101. mutation = mutations[_i];
  102. _ref = mutation.addedNodes;
  103. for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
  104. Node = _ref[_j];
  105. LinkPage(Node);
  106. }
  107. }
  108. });
  109.  
  110. linkify(document.body);
  111.  
  112. observer.observe(document.body, {
  113. childList: true,
  114. subtree: true
  115. });