Jump to content

User:JJPMaster/Scripts/fixaccessdate.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 correctText(text) {
    return text.replace(/|(access-date|date)=(\d{2})\/(\d{2})\/(\d{4})/g, (match, key, mm, dd, yyyy) => {
    const months = [
        "January", "February", "March", "April", "May", "June", 
        "July", "August", "September", "October", "November", "December"
    ];
    const monthName = months[parseInt(mm, 10) - 1];
    return `${key}=${monthName} ${parseInt(dd, 10)}, ${yyyy}`;
    });
}
function getPageContent(name) {
	var FADparams = {
		action: 'parse',
		page: name,
		prop: 'wikitext',
		format: 'json'
	};
	const api = new mw.Api();

	api.get(FADparams).done(data => {
		var res = data.parse.wikitext['*'].toString();
		alert(res);
		return res;
	});
}
function doTheEdit() {
	var FADparams2 = {
			action: 'edit',
			title: mw.config.get("wgPageName"),
			text: correctText(getPageContent(mw.config.get("wgPageName"))),
			format: 'json'
		},
		api = new mw.Api();
	
	api.postWithToken( 'csrf', FADparams2 ).done( function ( data ) {
		console.log( data );
	} );
}
$(() => {
	var portletLink = mw.util.addPortletLink('p-cactions', '#', 'Fix access dates', 'ca-fixaccessdate', 'Use a script to fix the access-date and date parameters of citation templates', '');
	$(portletLink).click(doTheEdit);
});