4chan Post Sorter

Sorts posts in a 4chan thread by reply count

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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);