Jump to content

User:Opencooper/svgReplace.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Opencooper (talk | contribs) at 07:21, 5 July 2020 (probably not needed to change the srcset). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Crudely replaces all PNG thumbnails for SVG files with their actual SVGs
// Warning: Downloads SVG files in addition to PNGs, so shouldn't be used
//          by the bandwidth-conscious 
// License: CC0
function replaceSvg() {
    if (!(mw.config.get('wgAction') === 'view' && mw.config.get('wgIsArticle'))) {
        return;
    }

    $("#bodyContent img[src$='.svg.png']").each(function() {
        var src = $(this).attr("src");
        var srcset = $(this).attr("srcset");
        
        // Regex will break if thumbnail URL structure changes
        src = src.replace(/(\/\/.*)\/thumb(.*.svg).*.svg.png/, "$1$2");
        $(this).attr("src", src);
        // srcset = srcset.replace(/(\/\/.*?)\/thumb(.*?.svg).*?.svg.png/g, "$1$2");
        // $(this).attr("srcset", srcset);
    });
}

$(replaceSvg);