Direct links

Direct links out

当前为 2016-10-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Direct links
  3. // @name:ru Прямые ссылки
  4. // @namespace FIX
  5. // @version 0.0.10
  6. // @description Direct links out
  7. // @description:ru Замена ссылок на прямые. Высокое быстродействие в сравнении с аналогичными скриптами.
  8. // @copyright 2016, raletag
  9. // @author raletag
  10. // @match *://*/*
  11. // @grant unsafeWindow
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. if (document.documentElement.clientWidth < 5 || document.documentElement.clientHeight < 5) return;
  18.  
  19. console.time('Direct links load');
  20.  
  21. var win = (unsafeWindow || window),
  22. doc = (document.body !== null ? document.body : document),
  23. r0 = new RegExp('\/(share|intent\/tweet)([^?]*)\?', 'i'),
  24. r1 = new RegExp('[?&](url|r|z|to|u|go|st.link|link|href)=([^&]*)(&|$)', 'i'),
  25. r2 = new RegExp('(\/leech_out\\.php\\?.:|\/phpBB2\/goto\/|\/go\/\\?)(.*)', 'i'),
  26. impCodes = '%3B%2C%2F%3F%3A%40%26%3D%2B%24%23',
  27. impRegex = new RegExp((impCodes.replace(/%/g,'|%').replace('|','')), 'gi'),
  28. impDecoded = decodeURIComponent(impCodes),
  29. impReplacer = function(ch) {
  30. return impDecoded[impCodes.indexOf(ch.toUpperCase())/3];
  31. },
  32. decodeImportant = function (text) {
  33. return text.replace(impRegex, impReplacer);
  34. };
  35.  
  36. function Handler (e) {
  37. console.time('HandlerTime');
  38. try {
  39. var link = e.target, url = link.href, tourl;
  40. while (!url && link !== this) {
  41. link = link.parentNode;
  42. url = link.href;
  43. }
  44. link.removeEventListener('mouseenter', Handler, false);
  45. if (url.length < 5 || r0.test(url)) {
  46. return;
  47. }
  48. tourl = ((url.match(r1)||url.match(r2)||[])[2]);
  49. if (!tourl) {
  50. return;
  51. }
  52. try {
  53. tourl = decodeURIComponent(tourl);
  54. tourl = win.atob(tourl);
  55. tourl = decodeURIComponent(tourl);
  56. tourl = escape(tourl);
  57. } catch (err) {
  58. }
  59. tourl = decodeImportant(tourl);
  60. if (tourl.match(/^http(|s):\/\//i)) {
  61. console.group("Direct links");
  62. console.info(url);
  63. console.info(tourl);
  64. link.removeAttribute('onclick');
  65. link.rel = 'noreferrer';
  66. link.href = tourl;
  67. console.timeEnd('HandlerTime');
  68. console.groupEnd();
  69. }
  70. } catch (err) {
  71. console.error('Direct links error: ' + err);
  72. console.timeEnd('HandlerTime');
  73. alert('Direct links error: ' + err);
  74. }
  75. return;
  76. }
  77.  
  78. function attachEvent (e) {
  79. for (var links = e.querySelectorAll('a[href*="/"], a[href*="?"]'), i = links.length - 1; i >= 0; --i) {
  80. links[i].addEventListener('mouseenter', Handler, false);
  81. }
  82. }
  83.  
  84. attachEvent(doc);
  85.  
  86. new MutationObserver(function(ms) {
  87. var m, n;
  88. for (m of ms) {
  89. for (n of m.addedNodes) {
  90. if (n.nodeType === Node.ELEMENT_NODE) {
  91. if (n.href) {
  92. n.addEventListener('mouseenter', Handler, false);
  93. } else {
  94. attachEvent(n);
  95. }
  96. }
  97. }
  98. }
  99. }).observe(doc, {childList: true, subtree: true});
  100.  
  101. console.timeEnd('Direct links load');
  102. })();