User:逆襲的天邪鬼/+1s.js

From TestWiki
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
(function () {
    if (mw.config.get('wgUserGroups').indexOf('sysop') === -1) {
        return;
    }

    var SUMMARY = '+1s by [[User:逆襲的天邪鬼/+1s.js|+1s.js]] (Test)';

    document.styleSheets[0].insertRule('.donate1second { color:red; }');
    document.styleSheets[0].insertRule('a.donating1second { color:grey; }');

    var block = function (username) {
        return $.ajax({
            url: mw.util.wikiScript('api'),
            data: {
                format: 'json',
                action: 'block',
                user: username,
                expiry: '1 second',
                reason: SUMMARY,
                allowusertalk: '',
                token: mw.user.tokens.get('editToken'),
            },
            dataType: 'json',
            type: 'POST',
        });
    };

    var startHaMode = function () {
        var getUserName = function (item) {
            var title = item.getAttribute('title');

            if (title.indexOf('/') > -1) {
                return null;
            }

            // deal with markblocked
            if (title.indexOf(';') > -1) {
                return title.substr(5, title.indexOf(';') - 5);
            } else {
                return title.substr(5);
            }
        };

        var donate1second = function (e) {
            e.preventDefault();
            var username = this.getAttribute('data-username');
            var that = this;

            if (that.className.indexOf('donating1second') !== -1) {
                return;
            }

            that.classList.add('donating1second');

            block(username).then(function (data) {
                if (data.error) {
                    mw.notify("Cannot donate " + username + "'s 1 second.");
                } else {
                    mw.notify("+1s succeeded.");
                }
            }).fail(function () {
                mw.notify("Cannot donate " + username + "'s 1 second.");
            }).always(function () {
                that.classList.remove('donating1second');
            });
        };

        $('a[title^="User:"]', '#mw-content-text').each(function (index, item) {
            var username = getUserName(item);
            if (username) {
                var x = $('<a href="#" class="donate1second"><sup>+1s</sup></a>')
                            .attr('data-username', username)
                            .click(donate1second);
                $(item).after(x);
            }
        });
    };

    var isHaMode = function() { return localStorage['+1s'] !== undefined && localStorage['+1s'] !== 'false'; };
    var label = function() { return isHaMode() ? '[√] +1s Mode' : '[ ] +1s Mode'; };

    var node = mw.util.addPortletLink('p-cactions', '#', label());
    $(node).click(function (e) {
        e.preventDefault();
        localStorage['+1s'] = !isHaMode();
        $('a', this).text(label());

        if (isHaMode()) {
            if ($('.donate1second').length === 0) {
                startHaMode();
            }
        } else {
            $('.donate1second').remove();
        }
    });

    if (isHaMode()) {
        startHaMode();
    }
})();