MediaWiki:Gadget-HideFundraisingNotice.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * Prevent CentralNotice banners from being loaded.
 *
 * This script relies on being able to listen for events emitted
 * by the browser when it starts proccessing a script, image or iframe.
 * If this script is loaded after the one it is trying to prevent, it will
 * effectivelty do nothing.
 */
if ( document.addEventListener ) {

	function blockBannerLoader (e) {
		var element, src;
		if (!e || !e.preventDefault) {
			return;
		};
		element = e.target;
		if (!element) {
			return;
		}
	
		if (element.nodeName.toLowerCase() === 'script') {
			src = String(element.src);
			if (src.indexOf('Special:BannerLoader') !== -1 || src.indexOf('Special:BannerListLoader') !== -1) {
				e.preventDefault();
			}
		}
	}
	
	// Listen to every script, image, iframe etc. being addded
	document.addEventListener('beforeload', blockBannerLoader, true);
}