qubodup / Freesound Moderation Assigner

// ==UserScript==
// @name         Freesound Moderation Assigner
// @namespace    http://qubodup.net/
// @version      0.2
// @description  sort users and assign all in queue
// @author       qubodup
// @copyright    2018-2020, qubodup (https://openuserjs.org/users/qubodup)
// @license      MIT
// @match        https://freesound.org/tickets/moderation/
// @grant        none
// ==/UserScript==

/* Changelog
- 0.2 CSS, maybe somethinge else. I just realized my local copy had more code
- 0.1 basic functionality additions
*/

(function() {
    'use strict';

    // get row objects
    $(document).ready(function(){
        // style additions
        $("<style type='text/css'>\
        #header { height: 30px; width: 950px; }\
        body, #header, #account_nav { background: none !important; }\
        .button, #container { border: none !important; }\
        .sample_player_small { width: 440px; }\
        #sounds_info { overflow-y: auto !important; overflow-x: hidden !important; }\
        #container { padding: 0px; }\
        #ticket_menu { padding: 0px; width: 100%; }\
        #ticket_menu .button.selected { background-color: white; color: #CB3D44; }\
        #ticket_menu li { margin: 0; }\
        #ticket_menu li a { padding: 10px 60px !important; }\
        #ticket_menu li a:hover { background-color: #f3f3f3 !important; }\
        #wrapper, ul#site_nav li, ul#site_nav li a { background: white !important; }\
        #footer_wrapper, #search, #logo, h1 { display: none !important; }\
        #upload_button { width: auto; height: auto; background: none; display: inline; }\
        input { margin-left: .5em } \
        label { display: inline !important; padding-left: .25em; margin-right: .5em; }\
        </style>").appendTo("head");

        // sorting by number of sounds
        function sortByCount( selElem ) {
          var tmpAry = new Array();
          for ( var i = 0; i < selElem.length; i++ ) {
            tmpAry[i] = new Array();
            var tmp = $(selElem[i]).html().split("<br>")[1].split(" new sounds")[0].split(">")[1];
            tmpAry[i][0] = parseInt(tmp);
            tmpAry[i][1] = selElem[i];
          }
          tmpAry.sort(function(a,b){var x=a[0];var y=b[0]; return y-x;});
          selElem = [];
          for ( i = 0; i < tmpAry.length; i++ ) {
            //console.log(i, tmpAry[i][0]);
            var op = tmpAry[i][1];
            selElem.push(op);
          }

          $(".moderation-user-list").html(selElem).after('<div style="clear:both"></div>');

          // text
          $("#container h3").second().text("Users with most new sounds");
        };

        // sorting by date
        function sortByDate( selElem ) {

          $(".moderation-user-list").html(selElem).after('<div style="clear:both"></div>');

          // text
          $("#container h3").second().text("Users with oldest new sounds");
        };

        // sorting by username
        function sortByName( selElem ) {

          var tmpAry = new Array();
          for ( var i = 0; i < selElem.length; i++ ) {
            tmpAry[i] = new Array();
            var tmp = $("a", selElem[i]).first().text().toLowerCase();
            tmpAry[i] = [tmp, selElem[i]];
          }
          // http://stackoverflow.com/questions/8996963/
          tmpAry.sort(function(a,b){
            if(a < b) return -1;
            if(a > b) return 1;
            return 0;
          });
          selElem = [];
          for ( i = 0; i < tmpAry.length; i++ ) {
            //console.log(i, tmpAry[i][0]);
            var op = tmpAry[i][1];
            selElem.push(op);
          }

          $(".moderation-user-list").html(selElem).after('<div style="clear:both"></div>');

          // text
          $("#container h3").second().text("Users by name");

        };

        // assign all sounds to self by creating iframe for each assign page
        // might break browser
        function assignAll( selElem ) {
          for ( var i = 0; i < selElem.length; i++ ) {
            var tmp = $("a", selElem[i]).last().attr('href');
            $("#container h3").eq(1).after('<iframe style="width:100%;height:25px;" src="' + tmp + '" /></iframe>');

          }
          $('#assignall').after("<p>Wait until (you might not have to wait) all slim iframes above have content loaded and then reload this page.</p>");
          $('#assignall').remove();
        }

        // assign 1-9 sounds user's sounds to self by creating iframe for each assign page
        function assignSingles ( selElem ) {
          for ( var i = 0; i < selElem.length; i++ ) {
            if ($(selElem[i]).html().split("<br>")[1].split(" new sound")[0].split(">")[1] < 10) {
                var tmp = $("a", selElem[i]).last().attr('href');
                $("#container h3").eq(1).after('<iframe style="width:100%;height:25px;" src="' + tmp + '" /></iframe>');
            }

          }
          $('#assignsingles').after("<p>Wait until (you might not have to wait) all slim iframes above have content loaded and then reload this page.</p>");
          $('#assignsingles').remove();
        }

        // assign 1 sound user's sounds to self by creating iframe for each assign page
        function assignSolos ( selElem ) {
          for ( var i = 0; i < selElem.length; i++ ) {
            if ($(selElem[i]).html().split("<br>")[1].split(" new sound")[0].split(">")[1] == 1) {
                var tmp = $("a", selElem[i]).last().attr('href');
                $("#container h3").eq(1).after('<iframe style="width:100%;height:25px;" src="' + tmp + '" /></iframe>');
            }

          }
          $('#assignsolos').after("<p>Wait until (you might not have to wait) all slim iframes above have content loaded and then reload this page.</p>");
          $('#assignsolos').remove();
        }

        // backup user 'list' for date sorting
        var userlist = $(".moderation-user-list .moderation-user-list-user");

        // HTML sorting interface
        // date is the default server side sorting
        $("#container h2").after('<form>Sort by \
        <input checked="checked" name="sort" type="radio" id="radiodate" value="date" /><label for="radiodate">Date</label>\
        <input id="radiocount" name="sort" type="radio" value="count" /><label for="radiocount">Count</label>\
        <input id="radioname" name="sort" type="radio" value="count" /><label for="radioname">Username</label>\
        </form>');

        // HTML assign all interface
        $("#container h3").eq(0).before('<h3>Assign multiple</h3>\
        <button id="assignall" title="assign all users (warning, this might freeze/crash your browser)">Assign all</button> <button id="assignsolos" title="assign all users with 1 sound">Assign solos</button> <button id="assignsingles" title="assign all users with < 10 sounds">Assign singles</button> <button id="assignheaps" title="Assign all users with more than one sounds">Assign heaps</button> <button id="assignheaps" title="Assign all users with at least 10 sounds">Assign doubles</button><br><br>');

        // radio button interaction
        $("#radiodate").bind('click', function(){
          sortByDate( userlist );
        });

        $("#radiocount").bind('click', function(){
          sortByCount( userlist );
        });

        $("#radioname").bind('click', function(){
          sortByName( userlist );
        });

        // assign all interaction
        $("#assignall").bind('click', function(){
          assignAll( userlist );
        });

        // assign all interaction
        $("#assignsingles").bind('click', function(){
          assignSingles( userlist );
        });

        // assign all interaction
        $("#assignsolos").bind('click', function(){
          assignSolos( userlist );
        });

        // default sort
        sortByCount(userlist);
        // version check, see https://stackoverflow.com/questions/5665915/
        if ( parseInt(jQuery.fn.jquery.split(".")[0], 10) == 1 && parseInt(jQuery.fn.jquery.split(".")[1], 10) < 6) {
            $('#radiocount').attr('checked', 'checked');
        } else {
            $('#radiocount').prop('checked', true);
        }
    });
})();