MediaWiki:Gadget-freezeAPNG.js: различия между версиями

Материал из Space Station 14 Вики
Новая страница: «(function($, mw){ function detectAndFreeze(img){ var xhr = new XMLHttpRequest(); xhr.open('GET', img.src, true); xhr.responseType = 'arraybuffer'; xhr.onload = function(){ if (xhr.status < 200 || xhr.status >= 300) return; var bytes = new Uint8Array(xhr.response); for (var i = 0; i < bytes.length - 4; i++){ if ( bytes[i] === 0x61 &&...»
 
м Pok переименовал страницу MediaWiki:Gadget-FreezeAPNG.js в MediaWiki:Gadget-freezeAPNG.js
(нет различий)

Версия от 16:39, 30 июня 2025

(function($, mw){
    function detectAndFreeze(img){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', img.src, true);
        xhr.responseType = 'arraybuffer';
        xhr.onload = function(){
            if (xhr.status < 200 || xhr.status >= 300) return;
            var bytes = new Uint8Array(xhr.response);
            for (var i = 0; i < bytes.length - 4; i++){
                if (
                    bytes[i]   === 0x61 &&
                    bytes[i+1] === 0x63 &&
                    bytes[i+2] === 0x54 &&
                    bytes[i+3] === 0x4C
                ){
                    freeze(img);
                    break;
                }
            }
        };
        xhr.send();
    }

    function freeze(img){
        var origW = img.getAttribute('width'),
            origH = img.getAttribute('height'),
            ow = img.naturalWidth,
            oh = img.naturalHeight,
            canvas = document.createElement('canvas');
        canvas.width  = ow;
        canvas.height = oh;
        canvas.getContext('2d').drawImage(img, 0, 0, ow, oh);
        try {
            img.src = canvas.toDataURL('image/png');
            if (origW) img.setAttribute('width', origW);
            if (origH) img.setAttribute('height', origH);
        } catch(e){}
    }

    $(function(){
        $('img[src$=".png"]').each(function(){
            if (this.complete) {
                detectAndFreeze(this);
            } else {
                this.addEventListener('load', function(){
                    detectAndFreeze(this);
                });
            }
        });
    });
})(jQuery, mediaWiki);