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.

当前为 2022-03-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         Better Reddit Image Previews
// @namespace    https://lawrenzo.com/p/better-reddit-image-previews
// @version      0.2.3
// @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() {
    function fixImage(img, fixHeight) {
        if(!img || img.getAttribute("ifix")) return;
        if(fixHeight) {
            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();
            evt.preventDefault();
        });
        img.setAttribute("ifix", 1);
    }
    var watchGallery = new MutationObserver(mutated => {
        mutated.forEach(mutant => {
            mutant.addedNodes.forEach(node => {
                if(node.nodeName == "IMG") fixImage(node, true);
            });
        });
    });
    function checkPost(mutated) {
        mutated[0].addedNodes.forEach(node => {
            if(!node.querySelectorAll) return;
            node.querySelectorAll("img[alt='Post image']")
                .forEach(img => fixImage(img));
            let gallery = node.querySelector("ul");
            if(gallery) {
                watchGallery.observe(gallery, {childList: true, subtree:true});
                gallery.querySelectorAll("li figure img")
                    .forEach(img => fixImage(img, true));
            }
        });
    }
    var lastFeedWatcher = null,
        lastFeedWrapper = null,
        lastPostWatcher = null;
    function processNodes(nodes) {
        nodes.forEach(node => {
            node.querySelectorAll("div[data-testid='post-container']").forEach(post => {
                if(post.getAttribute("ifix")) return;
                let postbg = post.querySelector("[data-click-id='background']");
                if(postbg) lastPostWatcher.observe(postbg, {childList:true});
                post.setAttribute("ifix", 1);
            });
        });
    }
    (new MutationObserver(() => {
        let listingLayout = document.querySelector(".ListingLayout-outerContainer"),
            firstPost = listingLayout && listingLayout.querySelector("div[data-testid='post-container']"),
            feedWrapper = firstPost ? firstPost.parentNode.parentNode.parentNode : listingLayout;
        if(feedWrapper && feedWrapper !== lastFeedWrapper) {
            lastFeedWrapper = feedWrapper;
            if(lastPostWatcher) lastPostWatcher.disconnect();
            lastPostWatcher = new MutationObserver(checkPost);
            processNodes([lastFeedWrapper]);
            if(lastFeedWatcher) lastFeedWatcher.disconnect();
            lastFeedWatcher = new MutationObserver(mutated => mutated.forEach(mutant => processNodes(mutant.addedNodes)));
            lastFeedWatcher.observe(lastFeedWrapper, {childList:true});
        }
    })).observe(document.body, {childList:true, subtree:true});
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址