Jump to content

MediaWiki:Common.js: Difference between revisions

From Fedoverse Wiki, the free encyclopedia
No edit summary
No edit summary
Line 1: Line 1:
/* =========================================================
/* =========================================================
   1. MINECRAFT SERVER STATUS (was missing: nothing ever
   1. MINECRAFT SERVER STATUS RETRIEVAL
      replaced "Checking…", so it stayed stuck forever)
   ========================================================= */
   ========================================================= */


/* =====================================================================
mw.hook('wikipage.content').add(function ($content) {
  10. MINECRAFT STATUS CARD
     var $cards = $content.find('.mc-status')
  ===================================================================== */
         .add($content.filter('.mc-status'));
 
.mc-status {
    --mc-pad: 14px;
    --ink: #262b26;
    --muted: #697369;
 
    display: grid;
    grid-template-columns: 64px max-content minmax(0, 1fr);
    grid-template-areas:
        "icon head players"
        "icon main main";
 
    align-items: center;
    justify-items: start;
    column-gap: 12px;
    row-gap: 8px;
 
    width: 100%;
    max-width: 680px;
    box-sizing: border-box;
    margin: 16px auto;
    padding: var(--mc-pad) 50px var(--mc-pad) var(--mc-pad);
 
    background: linear-gradient(135deg, #e8f5eb, #d3ead9);
     color: var(--ink);
    border: 1px solid #d1e2d3;
    border-radius: 16px;
    box-shadow: 0 2px 8px rgba(35, 55, 38, .07);
 
    font-family: var(--fedo-font);
    position: relative;
}
 
.mc-side {
    display: contents;
}
 
.mc-head {
    grid-area: head;
}
 
.mc-icon {
    grid-area: icon;
}
 
.mc-players {
    grid-area: players;
}
 
.mc-main {
    grid-area: main;
    justify-self: stretch;
    align-self: center;
}
 
/* Status and player pills */
 
.mc-head,
.mc-players {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
 
    padding: 6px 10px;
    border-radius: 999px;
 
    background: rgba(255, 255, 255, .65);
    border: 1px solid #d6e0d5;
 
    font-size: .78rem;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    cursor: default;
}
 
.mc-dot {
    width: 8px;
    height: 8px;
    flex-shrink: 0;
    border-radius: 50%;
    background: #a09b94;
}
 
.mc-status.is-online .mc-dot {
    background: #2fbf71;
    animation: mcpulse 2s infinite;
}
 
.mc-status.is-offline .mc-dot {
    background: #e5484d;
}
 
.mc-status.is-online .mc-head {
    background: #d9f2e1;
    border-color: #abdabb;
}
 
.mc-status.is-offline .mc-head {
    background: #f8dfdd;
    border-color: #e8bcb8;
}
 
.mc-players {
    font-weight: 700;
}
 
.mc-players::after {
    content: " players";
    font-weight: 500;
    color: var(--muted);
}
 
@keyframes mcpulse {
    0% {
         box-shadow: 0 0 0 0 rgba(47, 191, 113, .45);
    }
    100% {
        box-shadow: 0 0 0 7px rgba(47, 191, 113, 0);
    }
}
 
/* Compact server logo */
 
.mc-icon {
    width: 64px;
    height: 64px;
    box-sizing: border-box;
    border-radius: 14px;
    overflow: hidden;
 
    background: #ddd9d2;
    border: 1px solid #cfcac2;
    box-shadow: 0 2px 6px rgba(40, 50, 40, .08);
 
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
}
 
.mc-icon img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
    image-rendering: pixelated;
}
 
/* Server address and MOTD */
 
.mc-main {
    min-width: 0;
    container-type: inline-size;
 
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
 
    border-radius: 8px;
    outline-offset: 4px;
}
 
.mc-addr {
    font-family: var(--fedo-font);
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: -.025em;
    line-height: 1.1;
    white-space: nowrap;
 
    font-size: clamp(1.15rem, 5.5cqw, 2.2rem);
    color: var(--ink);
 
    transition: color .15s, transform .15s;
}
 
.mc-main:hover .mc-addr {
    color: #3f7a35;
}
 
.mc-main:active .mc-addr {
    transform: scale(.99);
}
 
.mc-main:focus-visible {
    outline: 2px solid #3f7a35;
}
 
.mc-motd {
    margin-top: 4px;
    font-size: .82rem;
    line-height: 1.35;
    color: var(--muted);
    white-space: pre-line;
    overflow-wrap: anywhere;
}
 
.mc-motd:empty {
    display: none;
}
 
/* External link icon */
 
.mc-open {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 2;
}
 
.mc-open a {
    display: flex;
    align-items: center;
    justify-content: center;


     width: 30px;
     $cards.each(function () {
    height: 30px;
        var card = this;
    border-radius: 9px;


    background: rgba(255, 255, 255, .65);
        if (card.dataset.mcStatusInit === '1') {
    border: 1px solid #d6e0d5;
            return;
    color: var(--muted);
        }
    text-decoration: none !important;


    transition: background .15s, color .15s;
        card.dataset.mcStatusInit = '1';
    -webkit-tap-highlight-color: transparent;
}


.mc-open a:hover,
        var head = card.querySelector('.mc-head');
.mc-open a:focus-visible {
        var players = card.querySelector('.mc-players');
    background: #fff;
        var iconBox = card.querySelector('.mc-icon');
    color: #3f7a35;
        var addressNode = card.querySelector('.mc-addr');
}
        var motd = card.querySelector('.mc-motd');


.mc-open-icon {
        var address =
    display: block;
            card.dataset.server ||
    width: 15px;
            (addressNode && addressNode.dataset.server) ||
    height: 15px;
            (addressNode && addressNode.textContent);


    background-color: currentColor;
        address = (address || '').trim().replace(/\s+/g, '');
    -webkit-mask: var(--mc-ext) center / contain no-repeat;
    mask: var(--mc-ext) center / contain no-repeat;


    --mc-ext: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M15 3h6v6'/%3E%3Cpath d='M10 14 21 3'/%3E%3Cpath d='M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6'/%3E%3C/svg%3E");
        function updateHead(label) {
}
            if (!head) return;


/* Mobile layout */
            var dot = head.querySelector('.mc-dot');


@media (max-width: 640px) {
            if (!dot) {
    .mc-status {
                dot = document.createElement('span');
        --mc-pad: 12px;
                dot.className = 'mc-dot';
            }


        grid-template-columns: 56px minmax(0, 1fr);
            var text = document.createElement('span');
        grid-template-areas:
             text.textContent = label;
            "icon head"
            "icon players"
             "main main";


        column-gap: 10px;
            head.replaceChildren(dot, text);
         row-gap: 6px;
         }


         margin: 12px auto;
         if (!address) {
        padding-right: 44px;
            updateHead('Unavailable');
        border-radius: 14px;
            if (players) players.textContent = '—';
    }
            return;
        }


    .mc-icon {
         var endpoint =
         width: 56px;
            'https://api.mcstatus.io/v2/status/java/' +
        height: 56px;
            encodeURIComponent(address) + '?query=false';
        border-radius: 12px;
    }


    .mc-head,
        fetch(endpoint)
    .mc-players {
            .then(function (response) {
        padding: 5px 8px;
                if (!response.ok) {
        font-size: .75rem;
                    throw new Error('Status request failed');
    }
                }


    .mc-main {
                return response.json();
        margin-top: 4px;
            })
    }
            .then(function (data) {
                card.classList.remove('is-online', 'is-offline');


    .mc-addr {
                if (data.online) {
        font-size: clamp(1.1rem, 6cqw, 1.8rem);
                    card.classList.add('is-online');
        white-space: normal;
                    updateHead('Online');
        overflow-wrap: anywhere;
    }


    .mc-motd {
                    if (players) {
        margin-top: 3px;
                        players.textContent = String(
        font-size: .78rem;
                            data.players ? data.players.online : 0
    }
                        );
                    }


    .mc-open {
                    if (motd) {
        top: 8px;
                        motd.textContent =
        right: 8px;
                            data.motd ? data.motd.clean || '' : '';
    }
                    }


    .mc-open a {
                    if (iconBox && data.icon) {
        width: 27px;
                        var img = iconBox.querySelector('img');
        height: 27px;
    }


    /* Preserve existing behavior on phones */
                        if (!img) {
    .mc-status:not(.is-offline) {
                            img = document.createElement('img');
        display: none;
                            img.alt = '';
    }
                            iconBox.replaceChildren(img);
}
                        }


/* Reduced motion */
                        img.src = data.icon;
                    }
                } else {
                    card.classList.add('is-offline');
                    updateHead('Offline');


@media (prefers-reduced-motion: reduce) {
                    if (players) {
    .mc-dot {
                        players.textContent = '0';
        animation: none !important;
                    }
    }


    .mc-addr,
                    if (motd) {
    .mc-open a {
                        motd.textContent = '';
        transition: none !important;
                    }
    }
                }
}
            })
            .catch(function () {
                card.classList.remove('is-online', 'is-offline');
                updateHead('Unavailable');


                if (players) {
                    players.textContent = '—';
                }
            });
    });
});





Revision as of 17:00, 23 October 2026


/* =========================================================
   1. MINECRAFT SERVER STATUS RETRIEVAL
   ========================================================= */

mw.hook('wikipage.content').add(function ($content) {
    var $cards = $content.find('.mc-status')
        .add($content.filter('.mc-status'));

    $cards.each(function () {
        var card = this;

        if (card.dataset.mcStatusInit === '1') {
            return;
        }

        card.dataset.mcStatusInit = '1';

        var head = card.querySelector('.mc-head');
        var players = card.querySelector('.mc-players');
        var iconBox = card.querySelector('.mc-icon');
        var addressNode = card.querySelector('.mc-addr');
        var motd = card.querySelector('.mc-motd');

        var address =
            card.dataset.server ||
            (addressNode && addressNode.dataset.server) ||
            (addressNode && addressNode.textContent);

        address = (address || '').trim().replace(/\s+/g, '');

        function updateHead(label) {
            if (!head) return;

            var dot = head.querySelector('.mc-dot');

            if (!dot) {
                dot = document.createElement('span');
                dot.className = 'mc-dot';
            }

            var text = document.createElement('span');
            text.textContent = label;

            head.replaceChildren(dot, text);
        }

        if (!address) {
            updateHead('Unavailable');
            if (players) players.textContent = '—';
            return;
        }

        var endpoint =
            'https://api.mcstatus.io/v2/status/java/' +
            encodeURIComponent(address) + '?query=false';

        fetch(endpoint)
            .then(function (response) {
                if (!response.ok) {
                    throw new Error('Status request failed');
                }

                return response.json();
            })
            .then(function (data) {
                card.classList.remove('is-online', 'is-offline');

                if (data.online) {
                    card.classList.add('is-online');
                    updateHead('Online');

                    if (players) {
                        players.textContent = String(
                            data.players ? data.players.online : 0
                        );
                    }

                    if (motd) {
                        motd.textContent =
                            data.motd ? data.motd.clean || '' : '';
                    }

                    if (iconBox && data.icon) {
                        var img = iconBox.querySelector('img');

                        if (!img) {
                            img = document.createElement('img');
                            img.alt = '';
                            iconBox.replaceChildren(img);
                        }

                        img.src = data.icon;
                    }
                } else {
                    card.classList.add('is-offline');
                    updateHead('Offline');

                    if (players) {
                        players.textContent = '0';
                    }

                    if (motd) {
                        motd.textContent = '';
                    }
                }
            })
            .catch(function () {
                card.classList.remove('is-online', 'is-offline');
                updateHead('Unavailable');

                if (players) {
                    players.textContent = '—';
                }
            });
    });
});


/* =========================================================
   2. PODCAST DECK
   Changes: runs inside mw.hook (DOM guaranteed), tries several
   sources in order, falls through when a source returns an
   empty list, and shows a link instead of staying blank.
   ========================================================= */
mw.hook('wikipage.content').add(function ($content) {
  var root = $content.find('#pc-podcast')[0];
  if (!root || root.dataset.pcInit === '1') return;
  root.dataset.pcInit = '1';

  var feed = root.dataset.feed, page = root.dataset.page;
  var deck = root.querySelector('.pc-deck'), nav = root.querySelector('.pc-nav');
  var cards = [], dots = [], active = 0;

  function el(tag, cls, text) {
    var e = document.createElement(tag);
    if (cls) e.className = cls;
    if (text) e.textContent = text;
    return e;
  }

  function safeUrl(u) { return /^https?:\/\//i.test(u || '') ? u : ''; }
  function txt(node, tag) { var n = node.getElementsByTagName(tag)[0]; return n ? n.textContent.trim() : ''; }
  function plain(html) {
    return new DOMParser().parseFromString(html || '', 'text/html').body.textContent.replace(/\s+/g, ' ').trim();
  }
  function parseDate(s) {
    var d = new Date(s);
    if (isNaN(d)) d = new Date(String(s).replace(' ', 'T') + 'Z');
    return d;
  }
  function fmtDate(s) {
    var d = parseDate(s);
    return isNaN(d) ? '' : d.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
  }
  function fmtDur(v) {
    if (!v) return '';
    if (v.indexOf(':') > -1) return v.replace(/^00:/, '').replace(/^0(\d:)/, '$1');
    var s = parseInt(v, 10);
    if (!s) return '';
    var h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60), sec = s % 60;
    return (h ? h + ':' + ('0' + m).slice(-2) : m) + ':' + ('0' + sec).slice(-2);
  }

  function fromXml(xml) {
    var doc = new DOMParser().parseFromString(xml, 'application/xml');
    if (doc.getElementsByTagName('parsererror').length) throw new Error('bad xml');
    var chImg = doc.getElementsByTagName('itunes:image')[0];
    var chCover = chImg ? chImg.getAttribute('href') : '';

    return Array.prototype.map.call(doc.getElementsByTagName('item'), function (it) {
      var img = it.getElementsByTagName('itunes:image')[0];
      var s = txt(it, 'itunes:season'), e = txt(it, 'itunes:episode');

      return {
        title: txt(it, 'title'),
        link: txt(it, 'link'),
        date: txt(it, 'pubDate'),
        dur: fmtDur(txt(it, 'itunes:duration')),
        tag: e ? (s ? 'S' + s + ' · ' : '') + 'E' + e : '',
        desc: plain(txt(it, 'description')),
        cover: (img && img.getAttribute('href')) || chCover
      };
    });
  }

  function fromJson(j) {
    var chCover = j.feed && j.feed.image;
    return (j.items || []).map(function (it) {
      return {
        title: it.title,
        link: it.link,
        date: it.pubDate,
        dur: '',
        tag: '',
        desc: plain(it.description),
        cover: it.thumbnail || chCover
      };
    });
  }

  function okText(r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.text(); }
  function okJson(r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); }

  function loadEpisodes() {
    var enc = encodeURIComponent(feed);
    var sources = [
      function () { return fetch(feed).then(okText).then(fromXml); },
      function () { return fetch('https://api.allorigins.win/raw?url=' + enc).then(okText).then(fromXml); },
      function () { return fetch('https://api.rss2json.com/v1/api.json?rss_url=' + enc).then(okJson).then(fromJson); },
      function () { return fetch('https://corsproxy.io/?url=' + enc).then(okText).then(fromXml); }
    ];

    // Try each source in turn; an error OR an empty list moves on to the next
    return sources.reduce(function (p, src) {
      return p.catch(function () {
        return src().then(function (list) {
          list = list.filter(function (e) { return e.title; });
          if (!list.length) throw new Error('empty');
          return list;
        });
      });
    }, Promise.reject(new Error('start')));
  }

  function buildCard(ep) {
    var c = el('article', 'pc-card');
    var cover = el('div', 'pc-cover');

    if (safeUrl(ep.cover)) {
      var img = el('img');
      img.alt = '';
      img.loading = 'lazy';
      img.onerror = function () { cover.textContent = '🎙️'; };
      img.src = ep.cover;
      cover.appendChild(img);
    } else {
      cover.textContent = '🎙️';
    }

    var body = el('div', 'pc-body');
    var meta = el('div', 'pc-meta');

    if (ep.tag) meta.appendChild(el('span', 'pc-pill pc-pill-strong', ep.tag));
    var ds = fmtDate(ep.date);
    if (ds) meta.appendChild(el('span', 'pc-pill', ds));
    if (ep.dur) meta.appendChild(el('span', 'pc-pill', ep.dur));
    if (meta.children.length) body.appendChild(meta);

    var href = safeUrl(ep.link) || safeUrl(page);
    var title = el('div', 'pc-title');
    title.setAttribute('role', 'heading');
    title.setAttribute('aria-level', '3');

    var a = el('a', '', ep.title || 'Untitled episode');
    if (href) {
      a.href = href;
      a.target = '_blank';
      a.rel = 'noopener';
    }
    title.appendChild(a);
    body.appendChild(title);
    body.appendChild(el('p', 'pc-desc', ep.desc));

    c.appendChild(cover);
    c.appendChild(body);

    if (href) {
      var o = el('div', 'mc-open');
      var oa = el('a');
      oa.href = href;
      oa.target = '_blank';
      oa.rel = 'noopener';
      oa.title = 'Open episode';
      oa.setAttribute('aria-label', 'Open episode');
      oa.appendChild(el('span', 'mc-open-icon'));
      o.appendChild(oa);
      c.appendChild(o);
    }

    return c;
  }

  function layout() {
    var n = cards.length;
    if (!n) return;

    cards.forEach(function (c, i) {
      var pos = (i - active + n) % n;
      c.dataset.pos = pos;
      c.inert = pos !== 0;
      c.setAttribute('aria-hidden', pos ? 'true' : 'false');
    });

    dots.forEach(function (d, i) {
      d.setAttribute('aria-current', i === active ? 'true' : 'false');
    });
  }

  function go(delta) {
    active = (active + delta + cards.length) % cards.length;
    layout();
  }

  function arrow(label, path, delta) {
    var b = el('button', 'pc-btn');
    b.type = 'button';
    b.setAttribute('aria-label', label);
    b.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="' + path + '"/></svg>';
    b.addEventListener('click', function () { go(delta); });
    return b;
  }

  function render(list) {
    list = list
      .sort(function (a, b) { return parseDate(b.date) - parseDate(a.date); })
      .slice(0, 3);

    deck.innerHTML = '';
    nav.innerHTML = '';
    cards = [];
    dots = [];
    active = 0;

    list.forEach(function (ep) {
      var card = buildCard(ep);
      cards.push(card);
      deck.appendChild(card);
    });

    for (var k = cards.length; k < 3; k++) {
      var g = el('div', 'pc-card pc-ghost');
      g.dataset.pos = k;
      g.setAttribute('aria-hidden', 'true');
      deck.appendChild(g);
    }

    root.classList.add('is-loaded');

    if (cards.length > 1) {
      var wrap = el('div', 'pc-dots');

      cards.forEach(function (_, i) {
        var d = el('button', 'pc-dot');
        d.type = 'button';
        d.setAttribute('aria-label', 'Episode ' + (i + 1));
        d.addEventListener('click', function () {
          active = i;
          layout();
        });
        dots.push(d);
        wrap.appendChild(d);
      });

      nav.appendChild(arrow('Previous episode', 'm15 18-6-6 6-6', -1));
      nav.appendChild(wrap);
      nav.appendChild(arrow('Next episode', 'm9 18 6-6-6-6', 1));

      var sx = null;
      deck.addEventListener('pointerdown', function (e) { sx = e.clientX; });
      deck.addEventListener('pointerup', function (e) {
        if (sx === null) return;
        var dx = e.clientX - sx;
        sx = null;
        if (Math.abs(dx) > 45) go(dx < 0 ? 1 : -1);
      });
    }

    layout();
  }

  function renderError() {
    root.classList.add('is-error');
    deck.innerHTML = '';
    var p = el('p', 'pc-error', 'Couldn\u2019t load episodes right now. ');
    var href = safeUrl(page);
    if (href) {
      var a = el('a', '', 'Listen on RSS.com');
      a.href = href;
      a.target = '_blank';
      a.rel = 'noopener';
      p.appendChild(a);
    }
    deck.appendChild(p);
  }

  loadEpisodes().then(render, renderError);
});


/* =========================================================
   3. NATIVE AUDIO PLAYER (unchanged)
   ========================================================= */
mw.hook('wikipage.content').add(function ($content) {
    $content.find('.infobox-native-audio').each(function () {
        if (this.dataset.audioInitialized === '1') {
            return;
        }

        const src = this.dataset.src;

        if (!src) {
            return;
        }

        const audio = document.createElement('audio');
        audio.preload = 'metadata';
        audio.src = src;

        const player = document.createElement('div');
        player.className = 'custom-audio-player';

        player.innerHTML = `
            <button class="audio-play" type="button" aria-label="Play">▶</button>

            <div class="audio-progress-container">
                <input class="audio-progress" type="range" min="0" max="100" value="0">
            </div>

            <span class="audio-time">0:00 / 0:00</span>

            <button class="audio-volume" type="button" aria-label="Mute">🔊</button>
        `;

        const playButton = player.querySelector('.audio-play');
        const progress = player.querySelector('.audio-progress');
        const time = player.querySelector('.audio-time');
        const volumeButton = player.querySelector('.audio-volume');

        function formatTime(seconds) {
            if (!isFinite(seconds)) {
                return '0:00';
            }

            const minutes = Math.floor(seconds / 60);
            const secs = Math.floor(seconds % 60)
                .toString()
                .padStart(2, '0');

            return `${minutes}:${secs}`;
        }

        function updateTime() {
            progress.value = audio.duration
                ? (audio.currentTime / audio.duration) * 100
                : 0;

            time.textContent =
                `${formatTime(audio.currentTime)} / ${formatTime(audio.duration)}`;
        }

        playButton.addEventListener('click', function () {
            if (audio.paused) {
                audio.play();
            } else {
                audio.pause();
            }
        });

        audio.addEventListener('play', function () {
            playButton.textContent = '❚❚';
            playButton.setAttribute('aria-label', 'Pause');
        });

        audio.addEventListener('pause', function () {
            playButton.textContent = '▶';
            playButton.setAttribute('aria-label', 'Play');
        });

        audio.addEventListener('timeupdate', updateTime);
        audio.addEventListener('loadedmetadata', updateTime);

        progress.addEventListener('input', function () {
            if (audio.duration) {
                audio.currentTime =
                    (progress.value / 100) * audio.duration;
            }
        });

        volumeButton.addEventListener('click', function () {
            audio.muted = !audio.muted;
            volumeButton.textContent = audio.muted ? '🔇' : '🔊';
        });

        audio.addEventListener('ended', function () {
            playButton.textContent = '▶';
            progress.value = 0;
        });

        this.replaceChildren(audio, player);
        this.dataset.audioInitialized = '1';
    });
});

/* =========================================================
   FEDOVERSE RANDOM HOMEPAGE GIF
   ========================================================= */

$(function () {
    var picker = document.getElementById('random-gif-picker');

    // Do nothing on pages without the GIF picker.
    if (!picker) {
        return;
    }

    // CHANGE THIS to your final number of uploaded GIFs.
    var maxGif = 42;

    if (!Number.isInteger(maxGif) || maxGif < 1) {
        return;
    }

    var storageKey = 'fedoverse-random-gif-last';
    var lastGif = 0;

    // Remember the previous GIF to avoid an immediate repeat.
    try {
        lastGif = Number(localStorage.getItem(storageKey)) || 0;
    } catch (e) {}

    // Select a random GIF from 1.gif to maxGif.gif.
    var gifNumber = Math.floor(Math.random() * maxGif) + 1;

    // Choose a different GIF from the previous visit.
    if (maxGif > 1 && gifNumber === lastGif) {
        gifNumber = (gifNumber % maxGif) + 1;
    }

    try {
        localStorage.setItem(storageKey, String(gifNumber));
    } catch (e) {}

    // Create the image.
    var img = document.createElement('img');

    img.alt = '';
    img.width = 850;
    img.loading = 'eager';
    img.decoding = 'async';

img.style.display = 'block';
img.style.width = '100%';
img.style.height = '100%';
img.style.maxWidth = '100%';
img.style.maxHeight = '100%';
img.style.objectFit = 'cover';
img.style.objectPosition = 'center';

    // Load the numbered GIF from the wiki.
    img.src = mw.util.getUrl(
        'Special:Redirect/file/' + gifNumber + '.gif',
        { width: 850 }
    );

    picker.appendChild(img);
});