Greasy Fork is available in English.

Fix image links on Old Reddit

Fix opening redd.it image links on Old Reddit

// ==UserScript==
// @name        Fix image links on Old Reddit
// @namespace   https://github.com/abdurazaaqmohammed
// @version     1.0.2
// @description Fix opening redd.it image links on Old Reddit
// @author      Abdurazaaq Mohammed
// @homepage    https://github.com/abdurazaaqmohammed/userscripts
// @license     The Unlicense
// @supportURL  https://github.com/abdurazaaqmohammed/userscripts/issues
// @match       https://*.reddit.com/*
// @match       https://abdurazaaqmohammed.github.io/website/imgviewer
// @exclude     https://www.reddit.com/*
// @exclude     https://new.reddit.com/*
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==

(function () {
  'use strict';
  const url = window.location.href;

  function openImageViewer(imgLink) {
    GM_setValue('img', imgLink);
    // Uncomment below and comment/delete the other line if you want to redirect to the image directly instead of opening in a new tab.
    //window.location.href = 'https://abdurazaaqmohammed.github.io/website/imgviewer';
    window.open('https://abdurazaaqmohammed.github.io/website/imgviewer');
  }

  function fixImageLinks(mutationsList, observer) {
    document.querySelectorAll('a[href^="https://preview.redd.it"], a[href^="https://i.redd.it"]').forEach(link => {
      const imgLink = link.href;
      // Overwriting the href would break copying the image link, so remove the old href and set onClick to open the viewer instead.
      link.removeAttribute("href");
      link.addEventListener("click", function() { openImageViewer(imgLink); });
    });
  }

  url.startsWith('https://old.reddit.com/') ? new MutationObserver(fixImageLinks).observe(document.body, { subtree: true, childList: true }) : (document.getElementById('urlInput').value = GM_getValue('img'), document.getElementById('displayButton').click());

})();