Filter Ideabox

Sort ideabox ideas by different methods

2018-02-05 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Filter Ideabox
// @namespace    pxgamer
// @version      0.3
// @description  Sort ideabox ideas by different methods
// @author       pxgamer
// @include      *kat.cr/ideabox/*
// @grant        none
// ==/UserScript==
/*jshint multistr: true */

(function() {
    'use strict';
    var rows = [];

    $('.buttonsline.floatleft').append('<select id="sortIdeas-select" style="letter-spacing: 0px; width: 220px !important;"><option value="all">Show all</option><option value="az">Sort Alphabetically (A-Z)</option><option value="no">Sort by Age (Newest to Oldest)</option><option value="views">Sort by Views (High to Low)</option></select> <button class="siteButton bigButton" id="sortIdeas"><span>Sort</span></button>');

    $('.ideaBox').each(function(){
        var html  = $(this).html();
        var title = $('.ideaBoxBody h3 a', $(this)).text();
        var age   = $('.ideaBoxBody div.lightgrey:last', $(this)).html().split('</span> ')[6].trim();
        var views = parseInt($('.ideaBoxBody div.lightgrey:last', $(this)).html().split('</span> ')[2].split(' views')[0], 10);
        rows.push({"title":title,"age":age,"views":views,"html":html});
    });

    console.log(rows);
    $('#sortIdeas').on('click', function() {
        var ideaSortType = $('#sortIdeas-select').val();

        switch (ideaSortType) {
            case 'all':
                $('div.ideaBox').show();
                break;
            case 'az':    // Sort alphabetically (A-Z)
                $('div.ideaBox').show();
                sortIdeas('title', 'desc');
                break;
            case 'no':    // Sort by age (Newest to Oldest)
                $('div.ideaBox').show();
                sortIdeas('age', 'desc');
                break;
            case 'views': // Sort by views (High to Low)
                $('div.ideaBox').show();
                sortIdeas('views', 'desc');
                break;
            default:
                $('div.ideaBox').show();
                break;
        }
    });

    function sortByKey(array, key) {
        return array.sort(function(a, b) {
            var x = a[key];
            var y = b[key];
            if (typeof x == "string") {
                x = x.toLowerCase();
                y = y.toLowerCase();
            }
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
    }

    function sortIdeas(sortName, sortType) {
        sortByKey(rows, sortName);
        if (sortName !== 'views') { rows.reverse(); }
        $('div.ideaBox').remove();
        for (var i=0;i<rows.length;i++) {
            $('.buttonsline.floatleft').after('<div class="ideaBox">'+rows[i].html+'</div>');
        }

    }
})();