Sort Soundgasm Posts

Allows you to sort soundgasm.net posts by play count

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Sort Soundgasm Posts
// @version      1
// @description  Allows you to sort soundgasm.net posts by play count
// @author       Oniisama
// @match        *soundgasm.net/u/*
// @namespace https://greasyfork.org/users/616912
// ==/UserScript==

// Get all posts on the page as a NodeList, then transform into Array
let container = document.getElementsByTagName("body")[0];
let posts = document.getElementsByClassName("sound-details");
posts = Array.prototype.slice.call(posts, 0);

let sorting_array = [];
let default_post_array = [];

// Add posts and their respective playcount to sorting_array
posts.forEach(function(post){
    let playcount = post.getElementsByClassName("playCount")[0].textContent.slice(12);

    // [0] = playcount number (1 * string = number)
    // [1] = the element itself
    sorting_array.push([1 * playcount, post]);
});

// Keep a copy of this array so the page can be restored to default
default_post_array = Array.from(sorting_array);

function SortPage (sorting_array){
    // Sort the elements on the page itself
    for (let i=0; i<sorting_array.length; i++)
    {
        // sorting_array[ID][ELEMENT]
        container.appendChild(sorting_array[i][1]);
    }
}

function SortPostsDescending(sorting_array)
{
    // I don't really get the function parameter but it works ig
    sorting_array.sort(function(x, y) {
        return y[0] - x[0];
    });

    SortPage(sorting_array);
}

function SortPostsAscending(sorting_array)
{
    // I don't really get the function parameter but it works ig
    sorting_array.sort(function(x, y) {
        return x[0] - y[0];
    });

    SortPage(sorting_array);
}

function SortPostsDefault(default_post_array)
{
    SortPage(default_post_array);
}

function CreateButton(text, func)
{
    let btn = document.createElement("button");
    btn.innerHTML = text;
    btn.style.margin = "10px 5px 5px 0";
    btn.onclick = func;

    return btn;
}

// Create buttons
let insertion_location = document.getElementsByClassName("sound-details")[0];
container.insertBefore(CreateButton("Sort By Play Count (Desc)", function(){SortPostsDescending(sorting_array)}), insertion_location);
container.insertBefore(CreateButton("Sort By Play Count (Asc)", function(){SortPostsAscending(sorting_array)}), insertion_location);
container.insertBefore(CreateButton("Reset to Default", function(){SortPostsDefault(default_post_array)}), insertion_location);