Reddit Multi Colum

Multi column layout for reddit redesign

目前為 2018-08-23 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit Multi Colum
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  Multi column layout for reddit redesign
// @author       Can Altıparmak
// @match        https://www.reddit.com/*
// @grant        none
// ==/UserScript==
/* jshint esversion: 6 */

(function() {
    'use strict';
    const MIN_WIDTH = 400;
    const COLUMNS = 4;
    let columns = COLUMNS;
    let cleanup = false;

    const OUTER = [
        '#SHORTCUT_FOCUSABLE_DIV > div > div:nth-child(2) > div > div > div > div:nth-child(2) > div:nth-child(3) > div:nth-child(1)',
        '#SHORTCUT_FOCUSABLE_DIV > div > div:nth-child(2) > div > div > div > div > div:nth-child(2) > div:nth-child(3) > div:nth-child(1)']
    const cardButton = () => document.querySelector('#layoutSwitch--card');

    const select = function() {
        let outer = null;
        for (let o of OUTER) {
            outer = document.querySelector(o);
            if (outer !== null) break;
        }
        let inner = outer !== null ? outer.firstChild.firstChild : null;
        return { outer, inner };
    }

    const makeLayout = function() {
        const c = cleanup;
        const { outer, inner } = select();
        if (inner === null) return;

        const cols = Math.floor(inner.offsetWidth / MIN_WIDTH);
        //if (cols === columns) return;

        if (c) {
            outer.style.width = '';
            inner.removeAttribute("style");
        } else {
            outer.style.width = "100%";
            inner.setAttribute("style", "display: flex; width: 100%; flex-flow: column wrap; position: relative;");
        }

        columns = cols;
        const WIDTH = Math.floor((100-columns)/columns);

        let posts = inner.children;
        let heights = Array(columns).fill(0);
        for (let i=0; i<posts.length; i++) {
            let post = posts[i];
            let col = i % columns;
            post.style.position = c ? "" : "absolute";
            post.style.width = c ? "" : `${WIDTH}%`;
            post.style.left = c ? "" : `${col*(WIDTH+1)}%`;
            post.style.top = c ? "" : `${heights[col]}px`;
            heights[col] += post.offsetHeight;
        }
        inner.style.height = c ? "" : `${Math.max(...heights)}px`;
    };

    const setLayout = function(changes, observer) {
        const button = cardButton();
        cleanup = button.getAttribute("aria-pressed") === "false";

        makeLayout();
    };

    const pageChange = new MutationObserver(makeLayout);
    const resize = new ResizeObserver(makeLayout);
    const layoutSwitch = new MutationObserver(setLayout);

    const watch = function(changes, observer) {
        const { inner } = select();
        if (inner === null) return;
        layoutSwitch.observe(cardButton(), {attributes: true});
        pageChange.observe(inner, {childList: true});
        resize.observe(inner, {childList: true});
    };

    const apply = new MutationObserver(watch);
    const page = document.querySelector('#SHORTCUT_FOCUSABLE_DIV > div > div:nth-child(2)');
    apply.observe(page, {childList: true, subtree: true});

    watch();
    setLayout();
})();