FP Read Page Sorter

Sorts threads by most unread posts

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         FP Read Page Sorter
// @namespace    member.php?u=197522
// @version      1.0
// @description  Sorts threads by most unread posts
// @author       Xemit
// @match        https://facepunch.com/fp_read.php
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Count the threads to sort
    var threadcount = 0;
    var t = document.getElementById("threads").children[0];
    var limit = t.children.length;
    var i;

    for(i = 1; i < limit; i++) {
        if(t.children[i].className != "threadlisthead") {
            threadcount++;
        } else {
            break;
        }
    }

    console.log("Sorting " + threadcount + " threads");

    // We need to get the threads to sort
    var threads = [];
    for(i = 0; i < threadcount; i++) {
        threads[i] = document.getElementById("threads").children[0].children[i + 1];
    }

    // Now make a map (I guess) of thread indexes and how many new posts they have
    var posts = [];
    for(i = 0; i < threads.length; i++) {
        var s = threads[i].getElementsByClassName("newposts");
        if(!s) {
            console.error("There's a thread with no new posts in there somehow, aborting!");
            return false;
        }
        s = s[0].innerText.trimLeft().substr(0, s[0].innerText.trimLeft().indexOf(" "));
        posts[i] = { index: i, posts: s };
    }

    // Finally sort those posts baby
    posts.sort(function(a, b) {
        return a.posts - b.posts;
    });

    // Making an array of sorted threads
    var sortedThreads = [];
    for(i = 0; i < threads.length; i++) {
        sortedThreads[i] = threads[posts[i].index].cloneNode(true);
    }

    // Remove old unsorted threads
    for(i = 0; i < threads.length; i++) {
        threads[i].parentNode.removeChild(threads[i]);
    }

    // Put those cool new sorted threads into place
    for(i = 0; i < sortedThreads.length; i++) {
        t = document.getElementById("threads").children[0];
        t.insertBefore(sortedThreads[i], t.children[1]);
    }

    console.log("Probably sorted threads successfully");
})();