MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* === ICE List Site Notice Close Button (Minimal, Safe) === */
(function () {
  'use strict';
 
  var KEY = 'icelist_anon_donate_dismiss_until';
  var DAYS = 14;


mw.hook('wikipage.content').add(function () {
   if (!mw.user.isAnon()) return;
   if (!mw.user.isAnon()) return;


   var notice = document.getElementById('siteNotice');
   var until = parseInt(localStorage.getItem(KEY) || '0', 10);
   if (!notice) return;
   if (Date.now() < until) return;


   // Already dismissed
   function mount() {
  if (localStorage.getItem('icelist_notice_closed') === '1') {
     var siteNotice = document.getElementById('siteNotice');
     notice.style.display = 'none';
     if (!siteNotice) return;
     return;
  }


  // Prevent duplicate buttons
    // If empty, do nothing
  if (notice.querySelector('.icelist-notice-close')) return;
    var txt = (siteNotice.textContent || '').replace(/\s+/g, ' ').trim();
    if (!txt) return;


  var btn = document.createElement('button');
    siteNotice.classList.add('icelist-anon-donate');
  btn.className = 'icelist-notice-close';
  btn.setAttribute('aria-label', 'Close');
  btn.textContent = '×';


  btn.onclick = function () {
    // Add close button once
    notice.style.display = 'none';
    if (!siteNotice.querySelector('.icelist-notice-close')) {
    localStorage.setItem('icelist_notice_closed', '1');
      var btn = document.createElement('button');
  };
      btn.type = 'button';
      btn.className = 'icelist-notice-close';
      btn.setAttribute('aria-label', 'Close');
      btn.textContent = '×';
      siteNotice.insertBefore(btn, siteNotice.firstChild);


   notice.appendChild(btn);
      btn.addEventListener('click', function (e) {
});
        e.preventDefault();
        siteNotice.style.display = 'none';
        localStorage.setItem(KEY, String(Date.now() + DAYS * 24 * 60 * 60 * 1000));
      });
    }
   }
 
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', mount);
  } else {
    mount();
  }
})();