MediaWiki:Common.js

MediaWiki interface page
Revision as of 22:18, 29 April 2023 by eunakria (Discord-317484070127009793) (talk | contribs) (whoops I'm an idiot (ensure tagline is initialized))

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
const $ = x => document.querySelector(x)
const $$ = x => document.querySelectorAll(x)

let tagline
window.addEventListener('load', () => {
    tagline = $('[data-footer-tagline]')
    tagline.addEventListener('click', () => handleTagline())
    handleTagline()
})

function handleTagline() {
    if (tagline === undefined) {
        return
    }
    let optChs = [...tagline.dataset.footerTagline]
    let opts = []
    let opt = null
    while (optChs.length > 0) {
        let ch = optChs.shift()
        if (ch === '\\') {
            ch = optChs.shift()
        } else if (ch === ';') {
            opts.push(opt)
            opt = null
            continue
        }
        opt ??= ''
        opt += ch
    }
    if (opt !== null) {
        opts.push(opt ?? '')
    }
    opts = opts
        .map(i => i.trim())
        .filter(i => i !== '')
    let oldOpts = opts
    opts = opts.filter(i => tagline.textContent !== i)
    if (opts.length === 0) {
        if (oldOpts.length === 0) {
            console.warn('No footer tagline options specified')
        } else {
            console.warn('All footer tagline options are identical')
        }
        return
    }
    let chosen = opts[Math.floor(Math.random() * opts.length)]
    tagline.textContent = chosen
}