Jump to content

User:DannyS712/Draft re cat.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// Install with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/draft re cat.js}}																</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/draft re cat.js' ); // Backlink: [[User:DannyS712/draft re cat.js]]			</nowiki></code> 
//
// If forking this script, please note my contributions / give me credit
$(() => {
const DraftReCat = {};
window.DraftReCat = DraftReCat;
DraftReCat.config = {
	name: '[[User:DannyS712/Draft re cat|draft re cat]]',
	version: 1.1,
	debug: false
};

DraftReCat.summary = 'Restore categorization (using ' + DraftReCat.config.name + " v" + DraftReCat.config.version + ").";
	
DraftReCat.run = function () {
	if ( DraftReCat.config.debug ) {
		console.log ( DraftReCat.summary );
	}
	
	var api = new mw.Api();
	api.get( {
		action: 'query',
		titles: mw.config.get( 'wgPageName' ),
		prop: 'revisions',
		rvprop: 'content',
		rvslots: 'main',
		formatversion: 2
	} ).done(
		function ( response ) {
			console.log( response );
			var text = response.query.pages[0].revisions[0].slots.main.content;
			var newText = text
				.replace( /\[\[:Category/gi, '\[\[Category' ); // Replace links with categorization

			api.postWithEditToken( {
				action: 'edit',
				title: mw.config.get( 'wgPageName' ),
				text: newText,
				summary: DraftReCat.summary
			} ).done(
				function() {
					location.reload();
				}
			);
		}
	);
};

});

$( document ).ready( () => {
	if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
		mw.loader.using(
			[ 'mediawiki.util' ],
			function () {
				var link = mw.util.addPortletLink( 'p-cactions', '#', 'DRC', 'ca-drc', 'Draft re cats'); 
				$( link ).click( function ( event ) {
					event.preventDefault();
					mw.loader.using( 'mediawiki.api', window.DraftReCat.run );
				} );
			}
		);
	}
} );