MediaWiki:Common.js

From ICE List Wiki
Revision as of 22:08, 20 December 2025 by ICEListAdmin6 (talk | contribs)
Jump to navigation Jump to search

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* ICE List – Anon donate banner (safe + scoped)
 * - ONLY runs for anonymous users
 * - ONLY styles when Anonnotice has content
 * - Adds a close (×) button (guaranteed visible)
 * - 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;

    // Mark for scoped CSS
    siteNotice.classList.add('icelist-anon-donate');

    // 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');
    if (!donateLink) {
      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 = '×';

      // 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';
        localStorage.setItem(KEY, String(Date.now() + DAYS * 24 * 60 * 60 * 1000));
      });
    }
  }

  // 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);
  } else {
    mount();
  }
})();