Imgur Mirror

Switches all imgur links to the mirror site http://kageurufu.net/imgur

  1. // ==UserScript==
  2. // @name Imgur Mirror
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.1.7
  5. // @description Switches all imgur links to the mirror site http://kageurufu.net/imgur
  6. // @author Adrien Pyke
  7. // @include http*
  8. // @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (() => {
  13. 'use strict';
  14.  
  15. const regex =
  16. /imgur\.com\/(?!a\/|gallery\/)(?:r\/[a-z0-9_]+\/)?([a-z0-9]+)(\.+[a-z0-9]+)?/iu;
  17. const extensions = [
  18. '.jpg',
  19. '.jpeg',
  20. '.png',
  21. '.gif',
  22. '.gifv',
  23. '.webm',
  24. '.mp4'
  25. ];
  26.  
  27. const getNewLink = function (imgurLink, useGif) {
  28. const match = imgurLink.match(regex);
  29. if (match) {
  30. const file = match[1];
  31. let extension = match[2].toLowerCase();
  32. if (!extension || !extensions.includes(extension)) {
  33. extension = '.png';
  34. } else if (
  35. extension === '.gifv' ||
  36. extension === '.gif' ||
  37. extension === '.webm'
  38. ) {
  39. extension = '.mp4';
  40. }
  41. if (useGif && extension === '.mp4') {
  42. extension = '.gif';
  43. }
  44. return `http://kageurufu.net/imgur/?${file + extension}`;
  45. } else {
  46. return null;
  47. }
  48. };
  49.  
  50. waitForElems({
  51. sel: 'img,a',
  52. onmatch(node) {
  53. const isImg = node.nodeName === 'IMG';
  54. const prop = isImg ? 'src' : 'href';
  55. const newLink = getNewLink(node[prop], isImg);
  56. if (newLink) {
  57. node[prop] = newLink;
  58. if (node.dataset.hrefUrl) {
  59. node.dataset.hrefUrl = newLink;
  60. }
  61. if (node.dataset.outboundUrl) {
  62. node.dataset.outboundUrl = newLink;
  63. }
  64. }
  65. }
  66. });
  67. })();