User:逆襲的天邪鬼/MassUndelete.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.
/**
 * Warning: Not completed. Use at your own risk!
 */
$(function () {
    var massUndelete = function (pages, reason, callback) {
        var promises = [];
        for (var i=0; i<pages.length; i++) {
            promises.push($.ajax({
                url: mw.util.wikiScript('api'),
                data: {
                    format: 'json',
                    action: 'undelete',
                    title: pages[i],
                    reason: reason,
                    token: mw.user.tokens.get('editToken'),
                },
                dataType: 'json',
                type: 'POST',
            }));
        }
        $.when.apply($, promises).always(function () {
            if (typeof callback === 'function') {
                callback();
            }
        });
    };

    if (mw.config.get('wgCanonicalNamespace') === 'Special' && mw.config.get('wgCanonicalSpecialPageName') === 'Log') {
        var node = mw.util.addPortletLink('p-cactions', '#', 'MassUndelete');
        $(node).click(function (e) {
            e.preventDefault();

            var checked = $('input[type=checkbox]', '.mw-logline-delete').filter(function (index, e) { return e.checked; });
            if (checked.length === 0) {
                alert('Please select at least one page.');
            } else {
                var reason = prompt('Restore reason', 'Test done (by [[User:逆襲的天邪鬼/MassUndelete.js|MassUndelete]])');
                if (reason !== null) {
                    var pages = [];
                    
                    checked.each(function (index, e) {
                        var title = $('a.new:last', e.parentElement).text();
                        console.log(title);
                        if (title) {
                            pages.push(title);
                        }
                    });
                     
                    massUndelete(pages, reason, function () {
                        alert('done');
                        history.go(0);
                    });
                }
            }
        });
    }
});