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:52, 11 September 2017 (refactor). 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: does not check for browser SVG support;
//          Also downloads SVG files in addition to PNGs, so shouldn't be used
//          by the bandwidth-conscious 

// Extra code mostly to not clash with my own script lastEdit.js which also bulk edits the whole HTML instead of DOM editing
function observe() {
    if ($("#siteSub").length) {
        return;
    }

    var target = document.querySelector('#siteSub');
    var observer = new MutationObserver(function(mutations) {
         if ($("#lastEdit").length) {
            alert("observed");
            observer.disconnect();
        }
    });
    observer.observe(target, {childList: true});
}

function replaceSvg() {
    var curInnerHTML = document.getElementById('bodyContent').innerHTML;
    // Regex will break if thumbnail URL structure changes
    curInnerHTML = curInnerHTML.replace(/(\/\/.*?)\/thumb(.*?.svg).*?.svg.png/g, "$1$2");
    document.getElementById('bodyContent').innerHTML = curInnerHTML;
}

$(observe);