Greasy Fork is available in English.

Direct links

Direct links out

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