User:Jgottula/common.js

// purpose: rewrite all MediaWiki element URL's: // - if they already have a 'format=' query parameter, leave them alone // - otherwise: add 'format=original' // this ensures that Chrome's fucking stupid 'accept:' header // won't result in MediaWiki converting nice PNG's into ugly WebP's const re_pathname = /^\/deadcells_gamepedia_en\/images\/[0-9a-f]{1}\/[0-9a-f]{2}\/.+$/; function FixImageURL(elem) {   if (elem.tagName != 'IMG') return;

try { var before = elem.src;

var url = new URL(elem.src); if (!re_pathname.test(url.pathname)) return;

var query = new URLSearchParams(url.search); if (query.has('format')) return;

query.append('format', 'original'); url.search = query.toString; elem.src = url.toString;

console.log('FixImageURL:'); console.log('Before: ' + before); console.log('After: ' + elem.src); } catch (ex) { return; } }

function FixAllImagesNow {   document.getElementsByTagName('img').forEach((elem) => {        FixImageURL(elem);    }); }

// immediate fixup for all elements that already exist FixAllImagesNow;

// delayed fixup for all elements existing at the time of DOMContentLoaded event document.addEventListener('DomContentLoaded', (event) => { FixAllImagesNow; });

// ongoing monitoring for any elements that may be added to the DOM later let observer = new MutationObserver((mutations, observer) => {   for (const mutation of mutations) {        if (mutation.type === 'childList') {            for (let elem of mutation.addedNodes) {                FixImageURL(elem);            }        } else if (mutation.type === 'attributes') {            if (mutation.target.tagName == 'IMG' && mutation.attributeName == 'src') {                FixImageURL(elem);            }        }    } }); observer.observe(document, {   subtree: true,    childList: true,    attributes: true });