MediaWiki:Common.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
add code to load custom css/js in MediaWiki namespace for testing
No edit summary
 
(27 intermediate revisions by the same user not shown)
Line 4: Line 4:


$(function() {
$(function() {
// 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' );
}
}


mw.loader.using( ['mediawiki.util'] ).done( function() {
// 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
// If Jimmy tweets a bare link to https://donate.wikimedia.org/w/index.php?title=Special:LandingPage
// we are gonna have a bad time as geolocation doesn't happen. Do a redirect to send them round again
// TODO: have a better way to trigger this
try {
var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving',
var url = new URL( location.href );
'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
if (
'US State Solicitation Disclosures', 'Draft:FAQ' ];
url.searchParams.get( 'title' ) === 'Special:LandingPage' &&
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 ) {
url.searchParams.get( 'country' ) === null &&
mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
url.searchParams.get( 'retried' ) === null
}
) {
console.log( 'redirecting to pick up country parameter' );
url.pathname = '/';
url.searchParams.set( 'retried', 1 ); // only do this once
url.searchParams.delete( 'title' );
window.location.href = url.toString();
}
} catch (error) {
console.warn('Error parsing URL. Possibly due to unreplaced % signs?');
}


// Disable logo link
// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
$('#p-logo a').attr( { href: '#', title: '' } );
if ( mw.config.get( 'wgUserName' ) !== null ) {

mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
// 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$/ ) ) {
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
mw.loader.load( '/w/index.php?title=' + formJS + '&action=raw&ctype=text/javascript' );
* @rev 6
} else {
*/
mw.loader.load( '/w/index.php?title=MediaWiki:DonationForm.js&action=raw&ctype=text/javascript' );
var extraCSS = mw.util.getParamValue( 'withCSS' ),
}
extraJS = mw.util.getParamValue( 'withJS' );

// Load monthly convert JS
// TODO: allow loading different JS variants, either here or defined in the Template
mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
}

// Allow manual country selection and reload
// Run this only when needed
var enableCountrySelect = function() {
var language = mw.config.get('wgPageContentLanguage'),
countrySelect = document.getElementById('country-select');

// Translations
if ( language !== 'en' && language !== 'en-gb' ) {
const translationsUrl = 'https://donate.wikimedia.org/wiki/Module:Country_links/data.json?action=raw';
$.getJSON( translationsUrl, function( translations ) {
if ( translations[language] ) {
var list = Array.from( countrySelect.options );
list.forEach( option => {
option.innerText = translations[language][option.value] || option.innerText;
});
// Sort for new language
list.sort( function(a, b) {
return a.innerText.localeCompare( b.innerText );
});
for ( var i = 0; i < list.length; i++ ) {
list[i].parentNode.appendChild( list[i] );
}
}
});
}

document.getElementById('country-select-go').addEventListener( 'click', function(e) {
if ( countrySelect.value ) {
// Chapter countries, use the built-in donatewiki redirects
if ( countrySelect.value === 'DE' ) {
window.location = 'https://donate.wikimedia.org/?country=DE';
} else if ( countrySelect.value === 'CH' ) {
window.location = 'https://donate.wikimedia.org/?country=CH';
} else {
var url = new URL( location.href );
url.searchParams.set( 'country', countrySelect.value );
window.location = url.toString();
}
}
});
};

if ( document.getElementById('country-change') ) {
document.getElementById('country-change').addEventListener( 'click', function(e) {
document.getElementById('country-change-select').style.display = 'block';
enableCountrySelect();
});
}

if ( document.getElementById('geolocate-error') ) {
enableCountrySelect();
}
// Fundraise Up privacy footer
if ( mw.util.getParamValue('fundraiseupScript') ) {
document.getElementById('donate-privacy-third-party').style.display = 'block';
}

// 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', 'Workplace giving' ];
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 || document.querySelector( '.language-switcher' ) ) {
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 ( extraCSS ) {
if ( extraJS ) {
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
} else {
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
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)
if ( extraJS ) {
TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
*/
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
if ( typeof inlineScriptWhenReady !== 'undefined') {
} else {
inlineScriptWhenReady();
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();
}
});
});

Latest revision as of 17:19, 2 December 2024

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

$(function() {

	mw.loader.using( ['mediawiki.util'] ).done( function() {

		// If Jimmy tweets a bare link to https://donate.wikimedia.org/w/index.php?title=Special:LandingPage
		// we are gonna have a bad time as geolocation doesn't happen. Do a redirect to send them round again
		try {
			var url = new URL( location.href );
			if ( 
				url.searchParams.get( 'title' ) === 'Special:LandingPage' &&
				url.searchParams.get( 'country' ) === null &&
				url.searchParams.get( 'retried' ) === null
			) {
				console.log( 'redirecting to pick up country parameter' );
				url.pathname = '/';
				url.searchParams.set( 'retried', 1 ); // only do this once
				url.searchParams.delete( 'title' );
				window.location.href = url.toString();
			}
		} catch (error) {
			console.warn('Error parsing URL. Possibly due to unreplaced % signs?');
		}

		// 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
			// TODO: allow loading different JS variants, either here or defined in the Template
			mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
		}

		// Allow manual country selection and reload
		// Run this only when needed
		var enableCountrySelect = function() {
			var language = mw.config.get('wgPageContentLanguage'),
				countrySelect = document.getElementById('country-select');

			// Translations
			if ( language !== 'en' && language !== 'en-gb' ) {
				const translationsUrl = 'https://donate.wikimedia.org/wiki/Module:Country_links/data.json?action=raw';
				$.getJSON( translationsUrl, function( translations ) {
					if ( translations[language] ) {
						var list = Array.from( countrySelect.options );
						list.forEach( option => {
							option.innerText = translations[language][option.value] || option.innerText;
						});
						// Sort for new language
						list.sort( function(a, b) {
							return a.innerText.localeCompare( b.innerText );
						});
						for ( var i = 0; i < list.length; i++ ) {
							list[i].parentNode.appendChild( list[i] );
						}
					}
				});
			}

			document.getElementById('country-select-go').addEventListener( 'click', function(e) {
				if ( countrySelect.value ) {
					// Chapter countries, use the built-in donatewiki redirects
					if ( countrySelect.value === 'DE' ) {
						window.location = 'https://donate.wikimedia.org/?country=DE';
					} else if ( countrySelect.value === 'CH' ) {
						window.location = 'https://donate.wikimedia.org/?country=CH';
					} else {
						var url = new URL( location.href );
						url.searchParams.set( 'country', countrySelect.value );
						window.location = url.toString();
					}
				}
			});
		};

		if ( document.getElementById('country-change') ) {
			document.getElementById('country-change').addEventListener( 'click', function(e) {
				document.getElementById('country-change-select').style.display = 'block';
				enableCountrySelect();
			});
		}

		if ( document.getElementById('geolocate-error') ) {
			enableCountrySelect();
		}
		
		// Fundraise Up privacy footer
		if ( mw.util.getParamValue('fundraiseupScript') ) {
			document.getElementById('donate-privacy-third-party').style.display = 'block';
		}

		// 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', 'Workplace giving' ];
		if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 || document.querySelector( '.language-switcher' ) ) {
			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();
		}

	} );
});