MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 2: Line 2:
  * - ONLY runs for anonymous users
  * - ONLY runs for anonymous users
  * - ONLY styles when Anonnotice has content
  * - ONLY styles when Anonnotice has content
  * - Adds a close (×) button
  * - Adds a close (×) button (guaranteed visible)
  * - Remembers dismissal in localStorage
  * - Remembers dismissal in localStorage
  */
  */
Line 26: Line 26:
     if (!txt) return;
     if (!txt) return;


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


     // Find the Donate link (prefer the span hook)
    // Ensure positioning context for absolute close button (CSS can override, but this helps)
    if (!siteNotice.style.position) siteNotice.style.position = 'relative';
 
     // Find the Donate link (prefer a hook if present)
     var donateLink = siteNotice.querySelector('.icelist-donate-cta a');
     var donateLink = siteNotice.querySelector('.icelist-donate-cta a');
     if (!donateLink) {
     if (!donateLink) {
      // fallback: first link that points to the donate page
       donateLink = siteNotice.querySelector('a[href*="ICE_List_Wiki:Donate"]');
       donateLink = siteNotice.querySelector('a[href*="ICE_List_Wiki:Donate"]');
     }
     }
Line 44: Line 46:
       btn.setAttribute('aria-label', 'Close');
       btn.setAttribute('aria-label', 'Close');
       btn.textContent = '×';
       btn.textContent = '×';
      siteNotice.appendChild(btn);


       btn.addEventListener('click', function () {
      // Insert FIRST so it can't get pushed off-screen by table/flex layout
      siteNotice.insertBefore(btn, siteNotice.firstChild);
 
       btn.addEventListener('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
         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 53: Line 59:
   }
   }


   if (document.readyState === 'loading') {
   // Run after MediaWiki has built the DOM (safer than raw DOMContentLoaded)
  if (typeof mw !== 'undefined' && mw.hook) {
    mw.hook('wikipage.content').add(function () {
      mount();
    });
  } else if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', mount);
     document.addEventListener('DOMContentLoaded', mount);
   } else {
   } else {
Line 59: Line 70:
   }
   }
})();
})();
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));
  });
});