User:BrownHairedGirl/CiteParamDashes.js

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:BrownHairedGirl/CiteParamDashes.js}}																</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:BrownHairedGirl/CiteParamDashes.js' ); // Backlink: [[User:BrownHairedGirl/CiteParamDashes.js]]			</nowiki></code> 

// This script is hacked from [[User:DannyS712/Draft no cat.js]]
// If forking this script, please note our contributions / give us credit

if ( mw.config.get( 'wgNamespaceNumber' ) === 0) {
    mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
        var link = mw.util.addPortletLink( 'p-cactions', '#', 'CiteParamDashes', 'ca-citeparamdashes', 'CiteParamDashes'); 
        $( link ).click( function ( event ) {
            event.preventDefault();
            tag_citeparamdashes();
        } );
    } );
} );
}

function tag_citeparamdashes() {
	var edit_summary = " dashes to cite parameters accessdate, archivedate, archiveurl, (using [[User:BrownHairedGirl/CiteParamDashes.js|a script]]) to avoid the [[phab:T291704]] problem with [[User:InternetArchiveBot|InternetArchiveBot]]";

	var send_req = {
	    action: 'query',
	    titles: mw.config.get( 'wgPageName' ),
	    prop: 'revisions',
	    intoken: 'edit',
	    rvprop: 'content',
	    indexpageids: 1,
	    dataType: 'xml',
	    format: 'xml'
	};
	var wgPageName = mw.config.get( 'wgPageName' );
	$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {
	    var text = $( response ).find( 'rev' ).text();
	    var citeParamsReplaced = 0;
	    var newText = text
	    		.replace(/(\|\s*)accessdate/gi,
	    			function(match, p1) {
	    				citeParamsReplaced += 1;
	    				return p1 + "access-date";
	    			}
	    			);
	    newText = newText
	    		.replace(/(\|\s*)archivedate/gi,
	    			function(match, p1) {
	    				citeParamsReplaced += 1;
	    				return p1 + "archive-date";
	    			}
	    			);
	    newText = newText
	    		.replace(/(\|\s*)archiveurl/gi,
	    			function(match, p1) {
	    				citeParamsReplaced += 1;
	    				return p1 + "archive-url";
	    			}
	    			);
	if (text == newText) {
		// nothing was changed
		alert("No dashes need to be added to cite parameters.")
		return;
	}
	if (confirm("[[" + wgPageName + "]]: " + citeParamsReplaced + " cite parameters lack dashes.\n\nAdd the dashes?")) {
		var submit = {
					action: 'edit',
					pageid: mw.config.get( 'wgArticleId' ),
					text: newText,
					summary: "Added " + citeParamsReplaced + edit_summary,
					minor: true,
					token: mw.user.tokens.get( 'csrfToken' )
				};
	    $.when(
	        $.post( mw.config.get( 'wgScriptPath' ) + '/api.php', submit, function( response ){ } )
	    ).done( function() { location.reload();} );
	}
	} );
}