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

мНет описания правки
Метка: отменено
мНет описания правки
Метка: ручная отмена
Строка 1352: Строка 1352:
     });
     });
}(jQuery, mediaWiki));
}(jQuery, mediaWiki));
// test
(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) {
                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 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 {
            var dataUrl = canvas.toDataURL('image/png');
            img.src = dataUrl;
            var newH = 64;
            var newW = Math.round( ow * ( newH / oh ) );
            img.setAttribute('width', newW);
            img.setAttribute('height', newH);
        } catch(e) {}
    }
    $(function(){
        $('img[src$=".png"]').each(function(){
            if (this.complete) {
                detectAndFreeze(this);
            } else {
                this.addEventListener('load', function(){
                    detectAndFreeze(this);
                });
            }
        });
    });
})(jQuery,mediaWiki);