4chan Post Sorter

Sorts posts in a 4chan thread by reply count

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name          4chan Post Sorter
// @namespace     4chan-post-sorter
// @description   Sorts posts in a 4chan thread by reply count
// @license       MIT
// @match         http://boards.4chan.org/*/*
// @match         https://boards.4chan.org/*/*
// @grant         GM_registerMenuCommand
// @version       1.0.2
// @author        asdaa
// @icon          https://4chan.org/favicon.ico
// ==/UserScript==

const parent = document.querySelector(".thread");

function getReplies(post) {
    const backlink = post.querySelector(".backlink");
    return backlink ? backlink.querySelectorAll(".quotelink").length : 0;
}

function getSorted(posts) {
   return posts.sort((a, b) => getReplies(b) - getReplies(a));
}

function sortPosts() {
   const posts = [...document.getElementsByClassName("postContainer replyContainer")];
   const sorted = getSorted(posts);
   parent.append(...sorted);
}

var sortBtn = document.createElement('a');
sortBtn.href = 'javascript:;';
sortBtn.innerText = 'Sort';
document.querySelector(".navLinks.desktop").appendChild(sortBtn);
sortBtn.insertAdjacentText('beforebegin', ' [');
sortBtn.insertAdjacentText('afterend', ']');
sortBtn.addEventListener("click", sortPosts, false);

GM_registerMenuCommand('Sort Posts by Reply Count', sortPosts);