Better Reddit Image Previews

Fixes issues with reddit image previews. Specifically, clicking image preview now links directly to image (instead of thread) and fits height of image previews in carousel so they don't get clipped out.

Per 04-03-2022. Zie de nieuwste versie.

// ==UserScript==
// @name         Better Reddit Image Previews
// @namespace    https://lawrenzo.com/p/better-reddit-image-previews
// @version      0.1.4
// @description  Fixes issues with reddit image previews. Specifically, clicking image preview now links directly to image (instead of thread) and fits height of image previews in carousel so they don't get clipped out.
// @author       Lawrence Sim
// @license      WTFPL (http://www.wtfpl.net)
// @grant        none
// @match        *://*.reddit.com/*
// ==/UserScript==
'use strict';
(function() {
    var fixImages = mutated => {
        mutated.forEach(mutant => {
            mutant.target.querySelectorAll("[data-click-id='background'] div[tabindex='0'] ul li figure img")
                .forEach(img => {
                    if(img.getAttribute("ifix")) return;
                    img.style.height = "100%";
                    img.parentElement.style.height = "100%";
                    img.addEventListener("click", evt => {
                        window.open(img.getAttribute("src").replace("preview.redd.it", "i.redd.it"));
                        evt.stopPropagation();
                    });
                    img.setAttribute("ifix", 1);
                });
            mutant.target.querySelectorAll("[data-click-id='background'] img[alt='Post image']")
                .forEach(img => {
                    if(img.getAttribute("ifix")) return;
                    img.addEventListener("click", evt => {
                        window.open(img.getAttribute("src").replace("preview.redd.it", "i.redd.it"));
                        evt.stopPropagation();
                        evt.preventDefault();
                    });
                    img.setAttribute("ifix", 1);
                });
        });
    }
    fixImages([{target: document.body}]);
    var listingLayout = document.querySelector(".ListingLayout-outerContainer");
    if(listingLayout) {
        (new MutationObserver(fixImages)).observe(
            listingLayout,
            {childList:true, subtree:true}
        );
    }
})();