Jump to content

MediaWiki:Common.js: Difference between revisions

From ICE List Wiki
Fix login banner: remove dead SPA loader, fail-safe anon-only class, harden jQuery call
Homepage: remove native link tooltips on tile links
Line 121: Line 121:
   });
   });
}
}
/* Homepage: strip native title tooltips on interactive tiles (they duplicate/contradict the visible labels) */
$(function () {
  if (mw.config.get('wgPageName') === 'Main_Page') {
    $('.ic-scenarios a, .ic-stat a, .ic-hero__actions a').removeAttr('title');
  }
});

Revision as of 03:30, 9 July 2026

/* Load CRUST brand fonts — ResourceLoader strips @import in Common.css, so inject the link here */
(function(){
  var l=document.createElement('link'); l.rel='stylesheet';
  l.href='https://fonts.googleapis.com/css2?family=Anton&family=Libre+Franklin:wght@400;500;600;700;800;900&family=Spline+Sans+Mono:wght@400;500;600&display=swap';
  document.head.appendChild(l);
})();

(function () {
  'use strict';

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

  if (!mw.user.isAnon()) return;

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

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

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

    siteNotice.classList.add('icelist-anon-donate');

    // Add close button once
    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.insertBefore(btn, siteNotice.firstChild);

      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();
  }
})();

mw.loader.using(['jquery']).then(function () {
    var s = document.createElement('script');
    s.src = 'https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js';
    s.setAttribute('data-name', 'BMC-Widget');
    s.setAttribute('data-cfasync', 'false');
    s.setAttribute('data-id', 'crustnews');
    s.setAttribute('data-description', 'Support me on Buy me a coffee!');
    s.setAttribute('data-message', '');
    s.setAttribute('data-color', '#FF5F5F');
    s.setAttribute('data-position', 'Right');
    s.setAttribute('data-x_margin', '18');
    s.setAttribute('data-y_margin', '18');
    document.body.appendChild(s);
});
// Tag agent pages (any page showing an agent card) so the duplicate title/heading can be hidden
if ( document.querySelector( '.ic-agent-card' ) ) {
    document.body.classList.add( 'ic-agent-page' );
}

/* ---- Mark anonymous users so anon-only banners can show (fail-safe: hidden by default) ---- */
if (mw.user.isAnon()) { document.documentElement.classList.add('vol-anon'); }

/* ---- Incident candidates page: logged-in-only + per-row dismiss ---- */
if (/Incident_candidates$/.test(mw.config.get('wgPageName'))) {
  mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
    var content = document.querySelector('#mw-content-text .mw-parser-output');
    if (!content) return;
    if (mw.user.isAnon()) {
      content.innerHTML = '<div class="vol-login">This page is for logged-in ICE List volunteers. ' +
        '<a href="' + mw.util.getUrl('Special:UserLogin', {returnto: mw.config.get('wgPageName')}) +
        '">Log in</a> to review incident candidates.</div>';
      return;
    }
    var api = new mw.Api();
    content.querySelectorAll('li').forEach(function (li) {
      var create = Array.prototype.find.call(li.querySelectorAll('a'), function (a) {
        return /FormEdit\/Incident/.test(a.getAttribute('href') || '');
      });
      if (!create) return;
      var art = Array.prototype.find.call(li.querySelectorAll('a[href^="http"]'), function (a) {
        return a !== create && !/FormEdit\/Incident/.test(a.getAttribute('href') || '');
      });
      var url = art ? art.href : null;
      var btn = document.createElement('button');
      btn.textContent = '✕ dismiss';
      btn.className = 'vol-dismiss';
      btn.onclick = function () {
        if (!url) { li.remove(); return; }
        btn.disabled = true; btn.textContent = '…';
        api.get({action: 'parse', page: mw.config.get('wgPageName'), prop: 'wikitext', formatversion: 2})
          .then(function (r) {
            var lines = r.parse.wikitext.split('\n'), out = [], removed = false;
            for (var i = 0; i < lines.length; i++) {
              if (!removed && lines[i].indexOf(url) !== -1) {
                if (out.length && /^<!-- cand:/.test(out[out.length - 1])) out.pop();
                removed = true; continue;
              }
              out.push(lines[i]);
            }
            return api.postWithToken('csrf', {action: 'edit', title: mw.config.get('wgPageName'),
              text: out.join('\n'), summary: 'Dismiss incident candidate'});
          })
          .then(function () { li.remove(); })
          .catch(function () { btn.disabled = false; btn.textContent = '✕ dismiss'; });
      };
      li.appendChild(document.createTextNode(' '));
      li.appendChild(btn);
    });
  });
}

/* Homepage: strip native title tooltips on interactive tiles (they duplicate/contradict the visible labels) */
$(function () {
  if (mw.config.get('wgPageName') === 'Main_Page') {
    $('.ic-scenarios a, .ic-stat a, .ic-hero__actions a').removeAttr('title');
  }
});