Goodreads - Sort lists by rating or votes

Sort lists

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        Goodreads - Sort lists by rating or votes
// @namespace   goodreads
// @description Sort lists
// @include     https://www.goodreads.com/list/show/*
// @version     1.1
// @grant       none
// ==/UserScript==

function isNumeric(num){
    return !isNaN(num)
}

function parseNumber(str) {
    var out = "";
    for(var i = 0; i < str.length; ++i) {
        if(isNumeric(str[i])) {
            out += str[i];
        }
    }
    return parseInt(out);
}

var App = {
    order: "desc",
    loadPage: 2,
    sort: function(cmp) {
        var elems = jQuery(".tableList").find("tr").get();
        elems.sort(cmp);
        if(App.order === "asc") {
            App.order = "desc";
        }
        else {
            App.order = "asc";
            elems.reverse();
        }
        for(var i = 0; i < elems.length; ++i) {
            elems[i].parentNode.appendChild(elems[i]);
        }
    },
    init: function() {
        $sortByRating = jQuery("<a></a>").attr("id", "sortByRating")
                                         .attr("href", "#")
                                         .text("sort by rating")
                                         .addClass("tab");
        $sortByVotes = jQuery("<a></a>").attr("id", "sortByVotes")
                                        .attr("href", "#")
                                        .text("sort by votes")
                                        .addClass("tab");
                                        
        jQuery(".bigTabs > div:nth-child(1)", ".leftContainer").append($sortByRating)
                                                               .append($sortByVotes);
                                                          
        jQuery("#sortByRating").on("click", function(){
            App.sort(function(lhs, rhs){
                var lhsRating = jQuery(lhs).find(".minirating").text().match(/[0-9]{1}\.[0-9]{2}/)[0];
                var rhsRating = jQuery(rhs).find(".minirating").text().match(/[0-9]{1}\.[0-9]{2}/)[0];
                return parseFloat(lhsRating) - parseFloat(rhsRating); 
            });
        });
        
        jQuery("#sortByVotes").on("click", function(){
            App.sort(function(lhs, rhs){
                var lhsVotes = parseNumber(jQuery(lhs).find(".minirating").text().match(/\d{1,3}(?:[,]\d{3})* rating/)[0]);
                var rhsVotes = parseNumber(jQuery(rhs).find(".minirating").text().match(/\d{1,3}(?:[,]\d{3})* rating/)[0]);
                if(lhsVotes < rhsVotes) { return -1; }
                if(lhsVotes > rhsVotes) { return 1; }
                return 0; 
            });
        });
        
        var hasPages = jQuery("#all_votes > .pagination").length > 0;
        if(hasPages) {
            $pagination = jQuery("#all_votes > .pagination > a");
            var numPages = parseInt($pagination.get(-2).innerHTML);
            $loadPage = jQuery("<a></a>").attr("id", "loadNextPage")
                                       .attr("href", "#")
                                       .text("load page #2")
                                       .addClass("tab");
            jQuery(".bigTabs > div:nth-child(1)", ".leftContainer").append($loadPage);
            
            var nextPageUrl = "https://www.goodreads.com" + $pagination.last().attr("href");
            jQuery("#loadNextPage").on("click", function(){
                jQuery("#loadNextPage").text("loading page #" + App.loadPage);
                jQuery.get(nextPageUrl, function(data){
                    $html = jQuery(data);
                    $html.find(".tableList tr").appendTo(jQuery(".tableList tbody"));
                    nextPageUrl = "https://www.goodreads.com" + $html.find(".pagination a").last().attr("href");
                    App.loadPage++;
                    if(App.loadPage > numPages) {
                        // Remove load link if loaded last page
                        jQuery("#loadNextPage").remove();
                    }
                    else {                
                        jQuery("#loadNextPage").text("load page #" + App.loadPage);
                    }
                });            
            });
        }
    }    
};

jQuery(document).ready(App.init);