MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* ICE List – Anon donate banner (safe + scoped)
* - ONLY runs for anonymous users
* - ONLY styles when Anonnotice has content
* - Adds a close (×) button
* - Remembers dismissal in localStorage
*/
(function () {
(function () {
   'use strict';
   'use strict';
Line 11: Line 5:
   var DAYS = 14;
   var DAYS = 14;


  // Only for anonymous users
   if (!mw.user.isAnon()) return;
   if (!mw.user.isAnon()) return;


  // Respect dismissal window
   var until = parseInt(localStorage.getItem(KEY) || '0', 10);
   var until = parseInt(localStorage.getItem(KEY) || '0', 10);
   if (Date.now() < until) return;
   if (Date.now() < until) return;
Line 22: Line 14:
     if (!siteNotice) return;
     if (!siteNotice) return;


     // If Anonnotice is empty (or only whitespace), do nothing
     // If empty, do nothing
     var txt = (siteNotice.textContent || '').replace(/\s+/g, ' ').trim();
     var txt = (siteNotice.textContent || '').replace(/\s+/g, ' ').trim();
     if (!txt) return;
     if (!txt) return;


    // Add a class so CSS ONLY applies when we explicitly say so
     siteNotice.classList.add('icelist-anon-donate');
     siteNotice.classList.add('icelist-anon-donate');


    // Find the Donate link (prefer the span hook)
     // Add close button once
    var donateLink = siteNotice.querySelector('.icelist-donate-cta a');
    if (!donateLink) {
      // fallback: first link that points to the donate page
      donateLink = siteNotice.querySelector('a[href*="ICE_List_Wiki:Donate"]');
    }
    if (donateLink) donateLink.classList.add('icelist-donate-btn');
 
     // Add close button if it doesn't already exist
     if (!siteNotice.querySelector('.icelist-notice-close')) {
     if (!siteNotice.querySelector('.icelist-notice-close')) {
       var btn = document.createElement('button');
       var btn = document.createElement('button');
Line 44: Line 27:
       btn.setAttribute('aria-label', 'Close');
       btn.setAttribute('aria-label', 'Close');
       btn.textContent = '×';
       btn.textContent = '×';
       siteNotice.appendChild(btn);
       siteNotice.insertBefore(btn, siteNotice.firstChild);


       btn.addEventListener('click', function () {
       btn.addEventListener('click', function (e) {
        e.preventDefault();
         siteNotice.style.display = 'none';
         siteNotice.style.display = 'none';
         localStorage.setItem(KEY, String(Date.now() + DAYS * 24 * 60 * 60 * 1000));
         localStorage.setItem(KEY, String(Date.now() + DAYS * 24 * 60 * 60 * 1000));
Line 59: Line 43:
   }
   }
})();
})();
document.addEventListener('DOMContentLoaded', function () {
  var notice = document.getElementById('siteNotice');
  if (!notice) return;
  var closeBtn = document.createElement('button');
  closeBtn.textContent = '×';
  closeBtn.className = 'icelist-notice-close';
  closeBtn.setAttribute('aria-label','Close');
  notice.appendChild(closeBtn);
  closeBtn.addEventListener('click', function () {
    notice.style.display = 'none';
    localStorage.setItem('icelist_anon_donate_dismiss_until', String(Date.now() + 14*24*60*60*1000));
  });
});