Jump to content

User:Zackmann08/scripts/cleanCsdLog.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function parse_log_text(text){
	return text.replaceAll('https://en.wikipedia.org/wiki/', '');
}

function cleanCsdLog() {
	mw.notify('Cleaning log...');
	mw.loader.using( "mediawiki.api", function () {
    	var api = new mw.Api();
        api.get( {
            prop: 'revisions',
            rvprop: 'content',
            rvlimit: 1,
            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[0]["*"];
            new_text = parse_log_text(text);
            if (new_text == null || new_text == text) {
            	mw.notify('Nothing to clean');
            	return;
            }
            api.postWithEditToken( {
                action: "edit",
                title: mw.config.get( "wgPageName" ),
                summary: 'cleaning log using [[User:Zackmann08/scripts/cleanCsdLog.js]]',
                text: new_text
            } ).done ( function ( data ) {
                if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                    mw.notify( "Log successfully cleaned! Reloading in 2 seconds..." );
                    window.setTimeout( function () {
                        document.location.reload( true );
                    }, 2000 );
                }
            } );
        } );
    } );
}

jQuery(document).ready(function($) {
  if (mw.config.get('wgPageName') == 'User:Zackmann08/log/CSD'){
  	mw.util.addPortletLink(
    	"p-cactions", 
    	"javascript:cleanCsdLog()", 
    	"Clean Log"
    	);
    }
});