MediaWiki:CentralNotice/Resources/BannerShowHideAdjustable.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
<script type="text/javascript">
// CentralNotice/Resources/BannerShowHideAdjustable.js
// This script determines based on the contents of the '{{{hide-cookie-name}}}'
// cookie and a random roll of the die whether to show a banner or not.
// Parameters:
//   hide-cookie-name      - Name of the cookie that stores the count
//   hide-cookie-max-count - Counts less than this will show the banner
//   hide-die-size         - 1 over this number is the probability this banner will show
(function( $, mw ) {
	// Set up the required variables
	var hideCookieName = '{{{hide-cookie-name}}}',
		hideAtCount = parseInt( '{{{hide-cookie-max-count}}}' ),
		numDieSides = parseInt( '{{{hide-die-size}}}' );

	mw.centralNotice.bannerData.cookieCount = parseInt(
		$.cookie( hideCookieName ) | 0
	);

	// Compute show hide
	mw.centralNotice.bannerData.hideResult = (function( mw ) {
		if ( wgCanonicalSpecialPageName === "CentralNotice" || wgCanonicalSpecialPageName === "NoticeTemplate" ) {
			// Don't show on CN pages
			return true;
		} else if ( ( mw.centralNotice.bannerData.cookieCount < hideAtCount &&
		              Math.floor( Math.random() * numDieSides ) === 0 ) ||
                             window.location.search.match( /\bforce=1/ )
		) {
			// But do show if conditions are met
			return false;
		} else {
			// Otherwise don't show :)
			return true;
		}
	})( mediaWiki );

	// Modify the show/hide cookie
	if ( mw.centralNotice.bannerData.hideResult === false ) {
		$.cookie(
			hideCookieName,
			mw.centralNotice.bannerData.cookieCount + 1,
			{ expires: 365, path: '/' }
		);
	}
	
	// Setup the CentralNotice callback hooks
	mw.centralNotice.bannerData.alterImpressionData = function( impressionData ) {
		return !mw.centralNotice.bannerData.hideResult;
	}

	// Now show or hide the banner
	if ( !mw.centralNotice.bannerData.hideResult ) {
		addBannerSpace();
		$(window).resize(function() {
			addBannerSpace();
		});
	}
})( jQuery, mediaWiki );
</script>