User:Kiteretsu/undo-last-edit.js

From Test Wiki
Revision as of 17:31, 29 August 2024 by Kiteretsu (talk | contribs) (Kiteretsu moved page User:Infinityboy7/undo-last-edit.js to User:Kiteretsu/undo-last-edit.js without leaving a redirect: Username changed)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$( function() {
    if( mw.config.get( "wgNamespaceNumber" ) < 0 ) return;
    mw.loader.using( [ "mediawiki.util", "mediawiki.api" ] ).then( function () {
        var link = mw.util.addPortletLink(
            "p-cactions",
            "#",
            "Undo last edit",
            "ca-undo",
            "Using the API, undo the last edit made to this page."
            );
        if( !link ) return;
        link.addEventListener( "click", function () {
            var api = new mw.Api();
            api.get( {
                prop: 'revisions',
                rvprop: 'content',
                rvlimit: 2,
                titles: mw.config.get( "wgPageName" )
            } ).done( function ( data ) {
                if ( !data.query || !data.query.pages ) return;
                var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
                    text = data.query.pages[pageid].revisions[1]["*"];
                api.postWithToken( "csrf", {
                    action: "edit",
                    title: mw.config.get( "wgPageName" ),
                    summary: "Undoing last edit ([[User:Infinityboy7/undo-last-edit.js|undo-last-edit]])",
                    text: text
                } ).done ( function ( data ) {
                    if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                        mw.notify( "Undid last edit successfully! Reloading..." );
                        document.location.reload( true );
                    }
                } );
            } );
        } );
    } );
} );