User:Jgottula/common.js
Jump to navigation
Jump to search
In other languages: Español • Polski • Русский • Українська
CSS and Javascript changes must comply with the wiki design rules.
Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
'use strict';
// NOTE: MediaWiki concatenates [user common.js + user hydradark.js]
// and then inserts that processed blob into a temporary <script> element
// at some point during the loading process (after DOMContentLoaded, IIRC)
// NOTE: MediaWiki is retarded and does not support any JS syntax newer than ES5,
// nor provide any mechanism for translating ES6 to ES5 (e.g. Babel),
// probably because their shitty backend is written in PHP (lol)
// https://phabricator.wikimedia.org/T178356
dbg('jgottula|common.js', 'pre-script');
// get rid of stupid vertical-space-wasting site notice regarding mobile version shit
// (we already hide it in CSS; here, we constantly set the cookie, so it'll GTFO even w/o user CSS enabled)
$.cookie('dismissSiteNotice', mw.config.get('wgSiteNoticeId'), { path: '/', expires: 365 * 1000 });
// get rid of the needlessly-CPU-churning animated image slideshows on the main page
$('body.page-Dead_Cells_Wiki div.fpbox:has(> div:contains("Featured images")):has(div[id^="sbls"])').remove();
// purpose: rewrite all MediaWiki <img> 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_fiximageurl = /^\/deadcells_gamepedia_en\/images\/[0-9a-f]{1}\/[0-9a-f]{2}\/.+$/;
function FixImageURL(elem)
{
if (elem.tagName.toLowerCase() != 'img') return 0;
try {
var before = elem.src;
var url = new URL(elem.src);
if (!re_fiximageurl.test(url.pathname)) return 0;
// get rid of /scale-to-width-down/<width> URL component
if (elem.hasAttribute('width') && elem.hasAttribute('data-file-width')) {
var width = elem.getAttribute('width');
var dfwidth = elem.getAttribute('data-file-width');
if (width == dfwidth) {
url.pathname = url.pathname.replace('/scale-to-width-down/' + width, '');
}
}
// always put format=original unless there's already a format= value
var query = new URLSearchParams(url.search);
if (!query.has('format')) {
query.append('format', 'original');
url.search = query.toString();
}
elem.src = url.toString();
//console.log('[jgottula|common.js] FixImageURL: Before: ' + before);
//console.log('[jgottula|common.js] FixImageURL: After: ' + elem.src);
} catch (ex) {
return 0;
}
return 1;
}
function FixAllImagesNow()
{
var elems = document.getElementsByTagName('img');
var n = 0;
for (var i = 0; i < elems.length; ++i) {
n += FixImageURL(elems[i]);
}
console.log('[jgottula|common.js] FixAllImagesNow: modified '+n+'/'+elems.length+' <img> element URLs');
}
// immediate fixup for all <img> elements that already exist
FixAllImagesNow();
// delayed fixup for all <img> elements existing at the time of DOMContentLoaded event
//document.addEventListener('DomContentLoaded', FixAllImagesNow);
// ongoing monitoring for any <img> 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
});*/
// this only needs to be declared once, since common.js and hydradark.js are concatenated together
function dbg(where, why)
{
// continually re-check the fragment and query params in case those
// happen to change later on and we're debugging persistent/latent code
var url = new URL(window.location.href);
if (url.hash.includes('jgdebug') || url.searchParams.has('jgdebug')) {
why = (typeof why !== 'undefined' ? ': ' + why : '');
console.error('[' + where + ']: jsdebug dynamic breakpoint invoked' + why)
debugger;
}
}