User:JJPMaster/Scripts/fixaccessdate.js

This is an old revision of this page, as edited by JJPMaster (talk | contribs) at 00:34, 19 November 2024 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 => {
		alert(data.parse.wikitext['*']);
	});
}
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);
});