User:Technical 13/Scripts/GANreview.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.
/*
* [[WP:GA]] reviewer helper script
* Author: [[User:Technical_13]]
* Description:
*	Finds and updates links to talk pages created by [[Template:GANentry]] without using the expensive #ifexist: parser function.
*/

/* Initialize variables */
var talkPage = '', talkPageTitle = '', talkPageNew = '', talkPageLinkTxt = '', talkPageURL = '';

/* Loop through ".GAN-review" links and replace href and text as is appropriate */
$( '.GAN-review' ).each( function() {
    talkPage = $( this ).closest( 'a' ).attr( 'title' );
    if ( talkPage.indexOf( ' (page does not exist)' ) !== -1 ) {
    	talkPageTitle = talkPage.substring( 0, talkPage.indexOf( ' (page does not exist)' ) ).replace( ' ', '_' );
    	talkPageNew = false;
    	talkPageLinkTxt = 'Start Review';
		talkPageURL = mw.util.getUrl( talkPageTitle, {
			action: 'edit',
			editintro: 'Template:GAN/editintro',
			preload: 'Template:GAN/preload'
		} );
    } else {
    	talkPageTitle = talkPage.replace( ' ', '_' );
    	talkPageNew = true;
    	talkPageLinkTxt = 'Discuss Review';
    	talkPageURL = mw.util.getUrl( talkPageTitle, { action: 'edit' } );
    }
    $( this ).closest( 'a' ).html( $( this ).closest( 'a' ).html().replace( 'Review', talkPageLinkTxt ) ).attr( 'href', talkPageURL );
} );