Jump to content

Snippets/Replace a page's contents with something else

From mediawiki.org
How to use Snippets
List of Snippets
Replace a page's contents with something else
Language(s): JavaScript
Compatible with: MediaWiki 1.18+ (Vector; Monobook)

Description

[edit]

Adds a tab that can be clicked to replace the current page's contents with different text. Released into the public domain. Mostly just a proof-of-concept here.

Code

[edit]

This code demonstrates how to make nested API calls as well as use tokens.

/**
 * Proposed deletion script for wikimediafoundation.org
 * @author Legoktm, MZMcBride
 * Public domain; 2013
 */

var prodlink = mw.util.addPortletLink( 'p-cactions',
                                       '#',
                                       'Prod',
                                       'ca-prod',
                                       'Propose deletion' );

// Bind click handler
$( prodlink ).click( function () {
    var page_title = mw.config.get('wgPageName');
    prod( page_title );
});

function prod( page_title ) {
    var api = new mw.Api();

    api.get( { action: 'query',
               prop: 'info',
               intoken: 'edit',
               titles: 'beepboop', // Hello. I'm here.
               format: 'json'
             } ).done( 

    function( data ) {
        var edit_token = data.query.pages['-1'].edittoken;
        console.log(edit_token);
        api.post( { action: 'edit',
                    title: page_title,
                    text: '{'+'{prod}}',
                    summary: 'proposed deletion',
                    token: edit_token
                  } ).done(

    function( data ) {
        window.location = '/wiki/'+page_title;
                     }    );
                     }
                     );     }