Old Reddit Inline Images

Displays image posts and replies inline in threads on the Old Reddit interface.

目前為 2024-06-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Old Reddit Inline Images
// @namespace    http://tampermonkey.net/
// @version      1.50
// @description  Displays image posts and replies inline in threads on the Old Reddit interface.
// @author       Spencer Ayers-Hale
// @license      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @match        https://*.reddit.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var num = document.getElementsByTagName("a").length; //number of links on page
    var cnt = 0; //current link number
    var newLink;

    //Read image metadata
    const getMeta = (url, cb) => {
        const img = new Image();
        img.onload = () => cb(null, img);
        img.onerror = (err) => cb(err);
        img.src = url;
    };

    while(cnt < num){
        const link = document.getElementsByTagName("a")[cnt]; //get original link

        //replace text with image
        //comment images
        if(link.innerText=="<image>"){
            //get image width
            getMeta(link.href, (err, img) => {
                //keep oringal size
                if(img.naturalWidth < 480){
                    link.innerHTML="<img src=\""+link.href+"\">"
                }
                //scale down large images
                else{
                    link.innerHTML="<img src=\""+link.href+"\" width=\"480\">"
                }
            });
        }
        //linked post images
        else if(link.innerText==link.href && link.href.indexOf("preview.redd.it") > -1){
            //get image width
            getMeta(link.href, (err, img) => {
                //keep oringal size
                if(img.naturalWidth < 480){
                    link.innerHTML="<img src=\""+link.href+"\">"
                }
                //scale down large images
                else{
                    link.innerHTML="<img src=\""+link.href+"\" width=\"840\">"
                }
            });
        }

        cnt++;
    }

})();