Frederick888 / U2 Sticky Manager (Torrent List)

// ==UserScript==
// @name				U2 Sticky Manager (Torrent List)
// @namespace           https://onee3.org/
// @version             0.21
// @description         Easier to manage the sticky torrents. Only for moderators.
// @include				*://u2.dmhy.org/torrents.php*
// @copyright			2014+, Frederick888
// @require				https://onee3.org/libs/jquery/2.2.3/jquery.min.js
// @grant				none
// @license             GPL-3.0-or-later
// ==/UserScript==

console.log('Using jQuery v' + $.fn.jquery);

// id={ArticleID}&type=reply&body={URLEncodedMessage}
// Sticky GET httpapi_torrentstick.php?sticky={1|0}&id={torrentid}
function toggle(ev) {
    var chked = [];
    $('.torrents input[type="checkbox"]').each(function () {
        if (this.checked) {
            chked.push(parseInt($(this).val()));
        }
    });
    if (ev.shiftKey) {
        if ($('#toggle-selected').length == 0) {
            $('.torrents>tbody>tr:first-child').after('<tr><td id="toggle-selected" class="colhead" colspan="100"></td></tr>');
        }
        if (chked.length > 0) {
            $('#toggle-selected').text(chked.join(', '));
        } else {
            $('#toggle-selected').text('(empty)');
        }
        return;
    }
    for (var i = 0; i < chked.length; i++) {
        var nextStat = 1 - $('input[value="' + chked[i] + '"]').closest('tr').find('img.sticky').length;
        $.get('httpapi_torrentstick.php', {sticky: nextStat, id: chked[i]});
        if (i == chked.length - 1) {
            $(document).ajaxStop(function () {
                location.reload();
            });
        }
    }
}

$('.torrents>tbody>tr:first-child').prepend('<td class="colhead"><input type="button" class="toggle-sticky" value="T" title="Toggle!"></td>');
$('.toggle-sticky').on('click', toggle);

$('.torrents>tbody>tr:not(:first-child)').each(function () {
    var id = parseInt(/id=(\d+)/.exec($(this).html())[1]);
    $(this).prepend('<td><input type="checkbox" value="' + id + '"></input></td>');
});