Skip to content

Instantly share code, notes, and snippets.

@Kobold
Created April 27, 2016 04:16
Show Gist options
  • Save Kobold/2e7eb69f3c4dd6513742cd78eea13dba to your computer and use it in GitHub Desktop.
Save Kobold/2e7eb69f3c4dd6513742cd78eea13dba to your computer and use it in GitHub Desktop.

Revisions

  1. Kobold created this gist Apr 27, 2016.
    31 changes: 31 additions & 0 deletions sortGoodreadsByPopularity.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    javascript:(() => {
    if (window.tinysort === undefined) {
    var script = document.createElement('script');
    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.3.0/tinysort.min.js';
    script.onload = function() {
    sortGoodreads();
    };
    document.getElementsByTagName('head')[0].appendChild(script);
    } else {
    sortGoodreads();
    }

    function getRatingCount(ratingString) {
    var countString = /([\d,]+) rating/.exec(ratingString)[1];
    return parseInt(countString.replace(/,/g, ''), 10);
    }

    function sortGoodreads() {
    var listSelector = '.tableList tr[itemtype="http://schema.org/Book"]';
    tinysort(listSelector, {
    order: 'desc',
    sortFunction: (a, b) => {
    var countA = getRatingCount(jQuery(a.elm).find('.minirating').text());
    var countB = getRatingCount(jQuery(b.elm).find('.minirating').text());
    return countA === countB ?
    0 :
    (countA > countB ? 1 : -1);
    }
    });
    }
    })();
    32 changes: 32 additions & 0 deletions sortGoodreadsByRating.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    javascript:(() => {
    if (window.tinysort === undefined) {
    var script = document.createElement('script');
    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.3.0/tinysort.min.js';
    script.onload = function() {
    sortGoodreads();
    };
    document.getElementsByTagName('head')[0].appendChild(script);
    } else {
    sortGoodreads();
    }

    function getRatingCount(ratingString) {
    var rating = /([\d.]+) avg rating/.exec(ratingString)[1];
    return parseFloat(rating);
    }

    function sortGoodreads() {
    var listSelector = '.tableList tr[itemtype="http://schema.org/Book"]';
    tinysort(listSelector, {
    order: 'desc',
    sortFunction: (a, b) => {
    var countA = getRatingCount(jQuery(a.elm).find('.minirating').text());
    var countB = getRatingCount(jQuery(b.elm).find('.minirating').text());
    return countA === countB ?
    0 :
    (countA > countB ? 1 : -1);
    }
    });
    }
    })();