Jump to content

User:Max/AutoResetGSR.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, 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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// "Reset" button on the Global sysops/Requests page uses API instead of manual edit
// when clicked, it instantly clears all requests
//<nowiki>
$(function() {
    if (
        mw.config.get('wgPageName') === 'Global_sysops/Requests'
    ) {
        const contentToReplace = [
            '{{Dynamite|title=:Global sysops/Requests/header}}',
            '<!-- Examples:',
            '* [[:q:fr:Casino Wins Today]]: Spam. ~~~~',
            '* [https://fr.wikiquote.org/wiki/Casino_Wins_Today]: Spam. ~~~~',
            '-->',
            '<!-- Edit below this line. New requests to the bottom. -->'
        ].join('\n');
        
        const editSummary = 'Clearing: all requests processed ([[User:Max/AutoResetGSR.js|AutoResetGSR.js]])';

        const replaceResetButton = function() {
            $('#GSR-reset').on('click', function(e) {
                e.preventDefault();
                const api = new mw.Api();
                api.get({
                    action: 'query',
                    prop: 'revisions',
                    titles: mw.config.get('wgPageName'),
                    rvprop: 'timestamp|content',
                    formatversion: 2
                }).done(function(data) {
                    const page = data.query.pages[0];
                    const baseTimestamp = (page && page.revisions && page.revisions[0] && page.revisions[0].timestamp) || null;
                    api.postWithToken('csrf', {
                        action: 'edit',
                        title: mw.config.get('wgPageName'),
                        text: contentToReplace,
                        summary: editSummary,
                        basetimestamp: baseTimestamp,
                        minor: true,
                        nocreate: 1
                    }).done(function(res) {
                        if (res && res.edit && res.edit.result === 'Success') {
                            location.reload();
                        } else {
                            alert('Edit failed');
                        }
                    }).fail(function() {
                        alert('Edit failed');
                    });
                });
            });
        };
        mw.loader.using('mediawiki.api', replaceResetButton);
    }
});
//</nowiki>