MediaWiki:Common.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
add retry for country=XX - phab:T316578
convert to tabs
Line 8: Line 8:
// If there was a problem detecting country, try sending them round again
// If there was a problem detecting country, try sending them round again
// for another bite at the cookie - https://phabricator.wikimedia.org/T316578
// for another bite at the cookie - https://phabricator.wikimedia.org/T316578
var uri = new mw.Uri( window.location.href );
var uri = new mw.Uri( window.location.href );
if ( uri.query.title === 'Special:LandingPage' && uri.query.country === 'XX' && uri.query.retried === undefined ) {
if ( uri.query.title === 'Special:LandingPage' && uri.query.country === 'XX' && uri.query.retried === undefined ) {
uri.path = '';
uri.path = '';
uri.query.retried = 1; // only do this once
uri.query.retried = 1; // only do this once
delete uri.query.country;
delete uri.query.country;
delete uri.query.title;
delete uri.query.title;
window.location.href = uri.toString();
window.location.href = uri.toString();
}
}
// Disable logo link
// Disable logo link
$('#p-logo a').attr( { href: '#', title: '' } );
$('#p-logo a').attr( { href: '#', title: '' } );
// Code for donation forms - https://donate.wikimedia.org/wiki/MediaWiki:DonationForm.js
// Code for donation forms - https://donate.wikimedia.org/wiki/MediaWiki:DonationForm.js
if ( document.forms['donateForm'] ) {
if ( document.forms['donateForm'] ) {
var formJS = mw.util.getParamValue( 'formJS' );
var formJS = mw.util.getParamValue( 'formJS' );
if ( formJS && formJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
if ( formJS && formJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
mw.loader.load( '/w/index.php?title=' + formJS + '&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=' + formJS + '&action=raw&ctype=text/javascript' );
} else {
} else {
mw.loader.load( '/w/index.php?title=MediaWiki:DonationForm.js&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=MediaWiki:DonationForm.js&action=raw&ctype=text/javascript' );
}
}
// Load monthly convert JS if needed
// Load monthly convert JS if needed
// TODO: allow loading different JS variants, either here or defined in the Template
// TODO: allow loading different JS variants, either here or defined in the Template
if ( mw.util.getParamValue('monthlyconvert') ) {
if ( mw.util.getParamValue('monthlyconvert') ) {
mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
}
}
}
}
// Code for Thank You pages - https://donate.wikimedia.org/wiki/MediaWiki:ThankYouPage.js
// Code for Thank You pages - https://donate.wikimedia.org/wiki/MediaWiki:ThankYouPage.js
if ( mw.config.get( 'wgTitle' ).indexOf('Thank You') !== -1 ) {
if ( mw.config.get( 'wgTitle' ).indexOf('Thank You') !== -1 ) {
mw.loader.load( '/w/index.php?title=MediaWiki:ThankYouPage.js&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=MediaWiki:ThankYouPage.js&action=raw&ctype=text/javascript' );
}
}
// Code for support pages - https://donate.wikimedia.org/wiki/MediaWiki:SupportPage.js
// Code for support pages - https://donate.wikimedia.org/wiki/MediaWiki:SupportPage.js
// TODO: have a better way to trigger this
// TODO: have a better way to trigger this
var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving',
var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving',
'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
'US State Solicitation Disclosures', 'Draft:FAQ' ];
'US State Solicitation Disclosures', 'Draft:FAQ' ];
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 ) {
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 ) {
mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
}
}
// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
if ( mw.config.get( 'wgUserName' ) !== null ) {
if ( mw.config.get( 'wgUserName' ) !== null ) {
mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
}
}
/**
/**
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @rev 6
* @rev 6
Line 80: Line 80:
/* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
/* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
*/
*/
if ( typeof inlineScriptWhenReady !== 'undefined') {
if ( typeof inlineScriptWhenReady !== 'undefined') {
inlineScriptWhenReady();
inlineScriptWhenReady();
}
}
} );
} );
});
});

Revision as of 17:10, 9 September 2022

/**
 * MediaWiki:Common.js - Any JavaScript here will be loaded for all users on every page load.
 **/

$(function() {
	
	mw.loader.using( ['mediawiki.util', 'mediawiki.Uri'] ).done( function() {
		
		// If there was a problem detecting country, try sending them round again
		// for another bite at the cookie - https://phabricator.wikimedia.org/T316578
		var uri = new mw.Uri( window.location.href );
		if ( uri.query.title === 'Special:LandingPage' && uri.query.country === 'XX' && uri.query.retried === undefined ) {
			uri.path = '';
			uri.query.retried = 1; // only do this once
			delete uri.query.country;
			delete uri.query.title;
			window.location.href = uri.toString();
		}
	
		// Disable logo link
		$('#p-logo a').attr( { href: '#', title: '' } );
		
		// Code for donation forms - https://donate.wikimedia.org/wiki/MediaWiki:DonationForm.js
		if ( document.forms['donateForm'] ) {
			var formJS = mw.util.getParamValue( 'formJS' );
			if ( formJS && formJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
				mw.loader.load( '/w/index.php?title=' + formJS + '&action=raw&ctype=text/javascript' );
			} else {
				mw.loader.load( '/w/index.php?title=MediaWiki:DonationForm.js&action=raw&ctype=text/javascript' );
			}
			
			// Load monthly convert JS if needed
			// TODO: allow loading different JS variants, either here or defined in the Template
			if ( mw.util.getParamValue('monthlyconvert') ) {
				mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
			}
		}
	
		// Code for Thank You pages - https://donate.wikimedia.org/wiki/MediaWiki:ThankYouPage.js
		if ( mw.config.get( 'wgTitle' ).indexOf('Thank You') !== -1 ) {
			mw.loader.load( '/w/index.php?title=MediaWiki:ThankYouPage.js&action=raw&ctype=text/javascript' );
		}
	
		// Code for support pages - https://donate.wikimedia.org/wiki/MediaWiki:SupportPage.js
		// TODO: have a better way to trigger this
		var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving', 
							 'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
							 'US State Solicitation Disclosures', 'Draft:FAQ' ];
		if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 ) {
			mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
		}
	
		// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
		if ( mw.config.get( 'wgUserName' ) !== null ) {
			mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
		}
		
		/**
		 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
		 * @rev 6
		 */
		var extraCSS = mw.util.getParamValue( 'withCSS' ),
			extraJS = mw.util.getParamValue( 'withJS' );
	
		if ( extraCSS ) {
			if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
				mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
			} else {
				mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
			}
		}
	
		if ( extraJS ) {
			if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
				mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
			} else {
				mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
			}
		}
	
	   /* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
			TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
		*/
		if ( typeof inlineScriptWhenReady !== 'undefined') {
			inlineScriptWhenReady();
		}
		
	} );
});