Frederick888 / My U2 MODs (nexusphp)

// ==UserScript==
// @name				My U2 MODs (nexusphp)
// @namespace           http://onee3.org/
// @version             0.50
// @description         Mainly for self-use. Making nexusphp a little more convenient.
// @include				*://u2.dmhy.org/*
// @copyright			2014+, Frederick888
// @require				https://onee3.org/libs/jquery/2.2.3/jquery.min.js
// @license             GPL-3.0-or-later
// ==/UserScript==

// Transferring UC
// event=1003&recv=[receiver]&amount=[amount]&message=[message]
// POST /mpshop.php
if (window.location.pathname == '/ucoin.php') {
    function rebind() {
        var del = $('input[value="Del"]');
        del.off('click');
        del.on('click', function () {
            $(this).closest('tr').remove();
        });
    }

    var box = $('a[name="transfer"]').closest('tbody');
    box.append('<tr><td style="padding: 10pt"><table class="embedded batch" style="min-width: 554px"><tbody><tr><td colspan="4"><b>Batch Transferring (Do not close while transferring)</b></td></tr><tr><td style="min-width: 73px">Receiver</td><td style="min-width: 238px">Amount</td><td style="min-width: 164px"><input type="submit" value="Add" style="width: 60px" /></td><td rowspan="500"><input type="submit" value="Transfer"></td></tr><tr><td><input type="text" name="recv1" value="0" size="6" style="text-align: right"></td><td><input type="text" name="amount1" size="8" style="text-align: right"></td><td><input type="submit" value="Del" style="width: 60px" /></td></tr><tr><td>Message</td><td colspan="2"><input type="text" name="msg" size="45" /></td></tr><tr><td>Status</td><td colspan="2"><textarea name="status" size="30" style="width: 400px; height: 150px;"></textarea></td></tr></tbody></table></td></tr>');
    rebind();
    var statusbox = $('textarea[name="status"]');
    var add = $('input[value="Add"]');
    add.on('click', function () {
        $('table.batch tr:nth-last-child(2)').before('<tr><td><input type="text" name="recv1" value="0" size="6" style="text-align: right"></td><td><input type="text" name="amount1" size="8" style="text-align: right"></td><td><input type="submit" value="Del" style="width: 60px" /></td></tr>');
        rebind();
    });
    function scrollToBottom() {
        statusbox.scrollTop(statusbox[0].scrollHeight);
    }

    function disableElements() {
        $('input[value="Add"]').attr('disabled', 'disabled');
        $('input[value="Del"]').attr('disabled', 'disabled');
        $('input[value="Transfer"]').attr('disabled', 'disabled');
        $('input[name="amount1"]').attr('disabled', 'disabled');
        $('input[name="recv1"]').attr('disabled', 'disabled');
        $('input[name="msg"]').attr('disabled', 'disabled');
    }

    function enableElements() {
        $('input[value="Add"]').removeAttr('disabled');
        $('input[value="Del"]').removeAttr('disabled');
        $('input[value="Transfer"]').removeAttr('disabled');
        $('input[name="amount1"]').removeAttr('disabled');
        $('input[name="recv1"]').removeAttr('disabled');
        $('input[name="msg"]').removeAttr('disabled');
    }

    var transfer = $('input[value="Transfer"]');
    transfer.on('click', function () {
        var receivers = $('input[name="recv1"]').map(function () {
            return $(this).val()
        }).get();
        var amounts = $('input[name="amount1"]').map(function () {
            return $(this).val()
        }).get();
        for (var i = 0; i < receivers.length; i++)
            if (receivers[i] === '' || receivers[i] <= 0 || amounts[i] === '' || amounts[i] <= 0) {
                alert('Illegal data found!');
                return;
            }
        var sum = 0;
        $.each(amounts, function (index, value) {
            sum += parseInt(value);
        });

        if (confirm('Transferring ' + sum + ' UC out, continue?')) {
            disableElements();
            var message = encodeURIComponent($('input[name="msg"]').val());

            statusbox.val('Action List:');
            i = 0;
            var receiversList = [];
            var amountsList = [];
            for (var k = 0; k < receivers.length; k++) {
                var tmp = amounts[k];
                while (tmp > 50000) {
                    receiversList[i] = receivers[k];
                    amountsList[i] = 50000;
                    i++;
                    tmp -= 50000;
                }
                receiversList[i] = receivers[k];
                amountsList[i] = tmp;
                i++;
            }
            for (k = 0; k < receiversList.length; k++) {
                statusbox.val(statusbox.val() + "\n");
                statusbox.val(statusbox.val() + 'Transfer $' + amountsList[k] + ' to #' + receiversList[k]);
            }
            statusbox.val(statusbox.val() + "\n<==============================>");
            scrollToBottom();
            function changeLastLine(t) {
                var lines = statusbox.val().split("\n");
                lines[lines.length - 1] = t;
                var statustext = lines[0];
                for (var k = 1; k < lines.length; k++) {
                    statustext += "\n" + lines[k];
                }
                statusbox.val(statustext);
                scrollToBottom();
            }

            var interval;
            // First time
            $.ajax({
                url: 'mpshop.php',
                type: 'POST',
                data: 'event=1003&recv=' + receiversList[0] + '&amount=' + amountsList[0] + '&message=' + message,
                success: function (result) {
                    statusbox.val(statusbox.val() + "\n" + 'Transferred $' + amountsList[0] + ' to #' + receiversList[0] + ' successfully');
                    scrollToBottom();
                },
                error: function (xhr, stat, err) {
                    clearInterval(interval);
                    changeLastLine('Failed! All actions stopped. Please check your network.');
                    enableElements();
                    return;
                }
            }).done(function () {
                // Others
                if (receiversList.length > 1) {
                    statusbox.val(statusbox.val() + "\n" + '300s to next transfer (actually longer for safety)');
                    scrollToBottom();
                    var timeleft = 300;
                    var quque = 1;
                    interval = setInterval(function () {
                        timeleft -= 10;
                        if (timeleft > 0) {
                            changeLastLine(timeleft + 's left...');
                        } else {
                            $.ajax({
                                url: 'mpshop.php',
                                type: 'POST',
                                data: 'event=1003&recv=' + receiversList[quque] + '&amount=' + amountsList[quque] + '&message=' + message,
                                success: function (result) {
                                    changeLastLine('Transferred $' + amountsList[quque] + ' to #' + receiversList[quque] + ' successfully');
                                    scrollToBottom();
                                },
                                error: function (xhr, stat, err) {
                                    clearInterval(interval);
                                    changeLastLine('Failed! All actions stopped. Please check your network.');
                                    enableElements();
                                    return;
                                }
                            }).done(function () {
                                quque++;
                                timeleft = 300;
                                if (quque == receiversList.length) {
                                    clearInterval(interval);
                                    statusbox.val(statusbox.val() + "\n" + 'All done!');
                                    scrollToBottom();
                                    enableElements();
                                    return;
                                }
                                statusbox.val(statusbox.val() + "\n" + '300s to next transfer (actually longer for safety)');
                            });
                        }
                    }, 10100);
                } else {
                    statusbox.val(statusbox.val() + "\n" + 'All done!');
                    scrollToBottom();
                    enableElements();
                    return;
                }
            });

        } else {
            return;
        }
    });
}

// Restore to the legacy Upload button.
$("#nav a[href='upload.php?addoffer=1']").attr('href', 'upload.php');

// Make the links of the download buttons, which in the torrents list page, appended with your passkey,
// so that you can paste it to your BT software.
if (window.location.pathname == '/torrents.php') {
    $.ajax({
        url: 'usercp.php',
        success: function (result) {
            var passkey = $(result).find('.hidden-click').attr('data-content');

            var url = window.location;
            var toAdd = '&http=1';
            if (/https.+/.test(url))
                toAdd = '&https=1';

            $('.download[src]').parent().each(function () {
                var originalAtt = $(this).attr('href');
                $(this).attr('href', originalAtt + '&passkey=' + passkey + toAdd);
            });
        }
    });
}

// Check all the boxes in the cheater list
if (window.location.pathname == '/cheaterbox.php') {
    $('input[name="delcheater[]"]').each(function () {
        $(this).prop('checked', true);
    });
}


// Make the input box wider, and permit inputting multi-lined content.
// Most of the following codes are copied from qip's script.
var shbox = document.getElementsByName('shbox_text').item(0);	//as some nexusphp sites do not have a proper id, thus not getElementById
if (shbox) {
    var altbox = document.createElement('textarea');
    altbox.setAttribute('type', 'text');
    altbox.setAttribute('name', 'shbox_text');
    altbox.setAttribute('id', 'shbox_text');
    altbox.setAttribute('size', '100');
    altbox.setAttribute('style', 'width: 850px; height: 40px; border: 1px solid gray;');
    altbox.setAttribute('rows', '3');
    shbox.parentNode.replaceChild(altbox, shbox);
}
var shsubmit = document.getElementById('hbsubmit');
if (shsubmit) {
    shsubmit.setAttribute('accessKey', 'S');
    shsubmit.value += '(Alt+S)';
}


// lipsum for daily attendance
if (/^\/showup\.php/.test(window.location.pathname)) {
    var message = $('textarea[name="message"]');
    if (message.length > 0) {
        message.text('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed dignissim sem eget felis viverra, non varius turpis elementum. Quisque dignissim velit vel consequat dignissim. Nunc dictum justo sed orci interdum laoreet. Nunc nec metus fringilla, pulvinar enim eu, eleifend augue. Ut a orci a tortor sodales auctor. Donec luctus mauris.');
    }
}