MediaWiki:Common.js: Difference between revisions

From ICE List Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/* ICE List Anon donate banner (safe + minimal)
/* ICE List Anon donate banner (safe + scoped)
  * - Styles are done in Common.css (targets #siteNotice.icelist-donate-notice)
  * - ONLY runs for anonymous users
  * - Adds an X close button
  * - ONLY styles when Anonnotice has content
  * - Hides empty #siteNotice for logged-in users (prevents the blank grey bar)
  * - Adds a close (×) button
  * - Remembers dismissal for 14 days (localStorage)
  * - Remembers dismissal in localStorage
  */
  */
(function () {
(function () {
   'use strict';
   'use strict';


   var KEY = 'icelist_donate_notice_until';
   var KEY = 'icelist_anon_donate_dismiss_until';
   var DAYS = 14;
   var DAYS = 14;


   function run() {
  // Only for anonymous users
  if (!mw.user.isAnon()) return;
 
  // Respect dismissal window
  var until = parseInt(localStorage.getItem(KEY) || '0', 10);
  if (Date.now() < until) return;
 
   function mount() {
     var siteNotice = document.getElementById('siteNotice');
     var siteNotice = document.getElementById('siteNotice');
     if (!siteNotice) return;
     if (!siteNotice) return;


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


     // Logged-in users: if the wrapper exists but there's no content, kill it.
     // Add a class so CSS ONLY applies when we explicitly say so
     if (!isAnon) {
     siteNotice.classList.add('icelist-anon-donate');
      if (((siteNotice.textContent || '').trim()) === '') {
        siteNotice.style.display = 'none';
      }
      return;
    }


     // Anonymous users: respect dismissal window
     // Find the Donate link (prefer the span hook)
     var until = parseInt(localStorage.getItem(KEY) || '0', 10);
     var donateLink = siteNotice.querySelector('.icelist-donate-cta a');
     if (Date.now() < until) {
     if (!donateLink) {
       siteNotice.style.display = 'none';
       // fallback: first link that points to the donate page
      return;
      donateLink = siteNotice.querySelector('a[href*="ICE_List_Wiki:Donate"]');
     }
     }
    if (donateLink) donateLink.classList.add('icelist-donate-btn');


     // Mark for CSS to style
     // Add close button if it doesn't already exist
     siteNotice.classList.add('icelist-donate-notice');
     if (!siteNotice.querySelector('.icelist-notice-close')) {
      var btn = document.createElement('button');
      btn.type = 'button';
      btn.className = 'icelist-notice-close';
      btn.setAttribute('aria-label', 'Close');
      btn.textContent = '×';
      siteNotice.appendChild(btn);


    // Add close button if missing
      btn.addEventListener('click', function () {
    var closeBtn = siteNotice.querySelector('.icelist-notice-close');
        siteNotice.style.display = 'none';
    if (!closeBtn) {
        localStorage.setItem(KEY, String(Date.now() + DAYS * 24 * 60 * 60 * 1000));
      closeBtn = document.createElement('button');
       });
      closeBtn.type = 'button';
      closeBtn.className = 'icelist-notice-close';
      closeBtn.setAttribute('aria-label', 'Close');
       closeBtn.innerHTML = '&times;';
      siteNotice.appendChild(closeBtn);
     }
     }
    closeBtn.addEventListener('click', function () {
      siteNotice.style.display = 'none';
      localStorage.setItem(KEY, String(Date.now() + DAYS * 24 * 60 * 60 * 1000));
    });
   }
   }


  // Run on initial load + when MW swaps content
  if (window.mw && mw.hook) {
    mw.hook('wikipage.content').add(run);
  }
   if (document.readyState === 'loading') {
   if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', run);
     document.addEventListener('DOMContentLoaded', mount);
   } else {
   } else {
     run();
     mount();
   }
   }
})();
})();

Revision as of 21:27, 20 December 2025

/* 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 () {
  'use strict';

  var KEY = 'icelist_anon_donate_dismiss_until';
  var DAYS = 14;

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

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

  function mount() {
    var siteNotice = document.getElementById('siteNotice');
    if (!siteNotice) return;

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

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

    // Find the Donate link (prefer the span hook)
    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')) {
      var btn = document.createElement('button');
      btn.type = 'button';
      btn.className = 'icelist-notice-close';
      btn.setAttribute('aria-label', 'Close');
      btn.textContent = '×';
      siteNotice.appendChild(btn);

      btn.addEventListener('click', function () {
        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();
  }
})();