User:Wilfredor/test.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
function editSec(section, summary, content, editToken) {
    $.ajax({
        url: mw.util.wikiScript('api'),
        data: {
            format: 'json',
            action: 'edit',
            title: mw.config.get('wgPageName'),
            section: section,
            summary: summary,
            text: content,
            token: editToken
        },
        dataType: 'json',
        type: 'POST',
        success: function(data) {
            if (data && data.edit && data.edit.result == 'Success') {
               window.location.reload();
            } else if (data && data.error) {
                alert('Error: API returned error code "' + data.error.code + '": ' + data.error.info);
            } else {
                alert('Error: Unknown result from API.');
            }
        },
        error: function(xhr) {
            alert('Error: Request failed.');
        }
    });
}

if ( mw.config.get( 'wgPageName' ) === 'User:Wilfredor/test' ) {
  editSec(1, 'sample bot edit', '1', mw.user.tokens.get( 'editToken' ));
}