Group By Subreddit

Groups submissions by subreddit on main page

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Group By Subreddit
// @author          Redditlien
// @namespace       groupbysubreddit
// @version         201710131432
// @description     Groups submissions by subreddit on main page
// @include         http://www.reddit.com/*
// @include         https://www.reddit.com/*
// @exclude         http://www.reddit.com/message/*
// @exclude         http://www.reddit.com/user/*
// @exclude         https://www.reddit.com/message/*
// @exclude         https://www.reddit.com/user/*
// @grant           none
// ==/UserScript==

var atags = document.getElementsByTagName('a');
var sitetable = document.getElementById('siteTable');
var myhash = new Object();
var nextprev;

function getParentDiv(node) {
    while(node.classList.contains("thing") == false && node.parentNode) {
        node = node.parentNode;
    }

    return node;
}

// build a hash of subreddit => array<submissions from that subreddit>
for(i=0;i<atags.length;i++) {
    var node = atags[i];
    var match = node.className.match(/.*subreddit.*/);
    if(match) {
        var subreddit = node.innerHTML;
        if(!myhash[subreddit]) {
            myhash[subreddit] = new Array();
            myhash[subreddit+"_link"] = node.href;
        }
        var parentdiv = getParentDiv(node);
        myhash[subreddit].push(parentdiv);
    }
}

// Clear the sitetable of all links, then store the next/prev links
if (sitetable.hasChildNodes() && Object.keys(myhash).length){
    while(sitetable.childNodes.length > 1){
        sitetable.removeChild(sitetable.firstChild);
    }
    nextprev = sitetable.firstChild;
}

// Rebuild the html from the myhash links with subreddit titles
for(var subreddit in myhash) {
    if(!subreddit.match(/.*_link$/)) {
        var titlenode = document.createElement('a');
        var mybr = document.createElement('br');

        titlenode.setAttribute('href', myhash[subreddit+"_link"]);
        titlenode.setAttribute('style', "color: #336699; font-size: 20px;");
        titlenode.innerHTML = subreddit;

        sitetable.appendChild(titlenode);
        sitetable.appendChild(mybr);

        var submissions = myhash[subreddit];
        for(var i=0;i<submissions.length;i++) {
            submissions[i].style.display = 'inline';
            sitetable.appendChild(submissions[i]);
        }
        sitetable.appendChild(nextprev);
    }
}