Text To link

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

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

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