Reddit auto-expand

Auto-expand posts in new reddit

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit auto-expand
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Auto-expand posts in new reddit
// @author       Blueshirt
// @match        https://www.reddit.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        GM_log
// @run-at document-end
// ==/UserScript==

(function() {
    'use strict';
    var excluded_posts = [];
    var auto_expand = true;

    function onKeyup(evt) {
        // Use https://keycode.info/ to get keys
        // ` shortcut
        if (evt.keyCode == 192) {
            // expando();
            auto_expand = !auto_expand;
            if (auto_expand) {
                window.alert("Auto-expand enabled.");
            }
            else {
                window.alert("Auto-expand disabled.");
            }
        }
    }
    function mouseClicked(event) {
        const elem = event.target;
        if (elem.className.includes("icon-expand")) {
            // add the post to the excluded list
            const post_id = elem.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.id;
            if (!excluded_posts.includes(post_id)) {
                excluded_posts.push(post_id);
            }

        }
        if (elem.className.includes("icon-collapse")) {
            // add the post to the excluded list
            const post_id = elem.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.id;
            if (!excluded_posts.includes(post_id)) {
                excluded_posts.push(post_id);
            }
        }

    }
    function getPostId(iconElement) {
        let id = iconElement.id;
        while(iconElement !== undefined && (id === undefined || id === "")) {
            iconElement = iconElement.parentElement;
            id = iconElement.id;
        }
        return id;
    }
    function autoExpand() {
        if (auto_expand && !window.location.href.includes("/comments/")) {
            let icons = document.getElementsByClassName("icon-expand");
            for (const icon of icons) {
                const postId = getPostId(icon);
                if (!excluded_posts.includes(postId)) {
                    let button = icon.parentNode
                    button.click();
                }
            }
        }
    }


    if (!window.location.href.includes("/comments/")) {
        // listen to "`" to enable / disable auto_expand
        document.addEventListener('keyup', onKeyup, true);
        // listen to click to add to excluded list
        document.addEventListener("click", mouseClicked);
        // listen to page scroll to expand new content
        const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
        var observer = new MutationObserver(function(mutations, observer) {
            autoExpand();
        });
        // TODO: probably tuning this can improve performances
        observer.observe(document, {
            subtree: true,
            attributes: true
        });
    }
})();