MediaWiki:CentralNoticeHideBanner.js

From Meta, a Wikimedia project coordination wiki

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">
// This script allows the hiding of a banner through a JS call
// It replaces the CN provided hide banner call for Emergency
// priority banners, or banners which do not want to affect other
// banners in their category

// To use
// * Have your banner hidden by default (css in the div)
// * Add this script to the bottom of your banner
// * To hide - call mw.centralNotice.bannerData.hideBanner()
// * Fill in the parameters:
// ** hide-cookie-div Name of div to show/hide
// ** hide-cookie-name Name of cookie
// ** hide-cookie-expire Number of days for cookie to last

mw.centralNotice.bannerData.hideBanner = function() {
  $('#{{{hide-cookie-div}}}').hide();
  $.cookie('{{{hide-cookie-name}}}', 'hide', { expires: {{{hide-cookie-expire}}}, path: '/' });
}

mw.centralNotice.bannerData.alterImpressionData = function( impressionData ) {
  if (window.location.search.match(/\breset=1/) || window.location.search.match(/\bforce=1/)) {
    $.cookie('{{{hide-cookie-name}}}', 0, { expires: {{{hide-cookie-expire}}}, path: '/' });
    mw.centralNotice.bannerData.hideResult = false;
    return true;
  }

  if ( $.cookie('{{{hide-cookie-name}}}') === 'hide' ) {
    mw.centralNotice.bannerData.hideResult = true;
    return false;
  } else {
    $('#{{{hide-cookie-div}}}').show();
    mw.centralNotice.bannerData.hideResult = false;
    return true;
  }
};
</script>