Frederick888 / U2 Sticky Manager (Torrent Detail)

// ==UserScript==
// @name				U2 Sticky Manager (Torrent Detail)
// @namespace           https://onee3.org/
// @version             0.16
// @description         Easier to manage the sticky torrents. Only for moderators.
// @include				*://u2.dmhy.org/details.php*
// @copyright			2015+, Frederick888
// @require				https://onee3.org/libs/jquery/2.2.3/jquery.min.js
// @grant				none
// ==/UserScript==

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

var torrent_id = 0;
$('#outer > table[cellpadding=5] > tbody > tr:first-child').before('<tr><td colspan="2" style="text-align:center"><button type="button" id="stickify" style="margin:0 5px" disabled="">设为置顶</button><button type="button" id="unstickify" style="margin:0 5px" disabled="">取消置顶</button></td></tr>');

$('#unstickify').click(function () {
    if (torrent_id > 0) {
        $.ajax({
            url: 'httpapi_torrentstick.php',
            data: {sticky: 0, id: torrent_id},
            success: function (data, status, xhr) {
                location.reload();
            },
            error: function (xhr, status, exception) {
                alert('Call API failed. Please check your network or contact with SysOp');
            }
        })
    }
});
$('#stickify').click(function () {
    if (torrent_id > 0) {
        $.ajax({
            url: 'httpapi_torrentstick.php',
            data: {sticky: 1, id: torrent_id},
            success: function (data, status, xhr) {
                window.location = window.location;
            },
            error: function (xhr, status, exception) {
                alert('Call API failed. Please check your network or contact with SysOp');
            }
        })
    }
});

var pattern = /id=(\d+)/;
if (pattern.test(document.location.search)) {
    torrent_id = parseInt(RegExp.$1);
    // httpapi_torrentinfo.php?tid=xxx
    $.ajax({
        url: 'httpapi_torrentinfo.php?tid=' + torrent_id,
        dataType: 'json',
        success: function (data, status, xhr) {
            if (data['code'] == 0) {
                if (data['data']['pos_state'] == 'sticky') {
                    $('#unstickify').removeAttr('disabled');
                } else if (data['data']['pos_state'] == 'normal' && data['data']['decide_by'] != 0) {
                    $('#stickify').removeAttr('disabled');
                }
            } else {
                console.log('Error returned by API: ' + data['err_msg']);
            }
        },
        error: function (xhr, status, exception) {
            console.log('Call API failed. Please check your network or contact with SysOp');
        }
    });
}