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

мНет описания правки
Метка: отменено
мНет описания правки
Метка: отменено
Строка 1355: Строка 1355:
// test
// test
(function($,mw){
(function($,mw){
     function detectAndFreeze(imgEl){
     function detectAndFreeze(img){
        var url = imgEl.src;
         var xhr = new XMLHttpRequest();
         var xhr = new XMLHttpRequest();
         xhr.open('GET', url, true);
         xhr.open('GET', img.src, true);
         xhr.responseType = 'arraybuffer';
         xhr.responseType = 'arraybuffer';
         xhr.onload = function(){
         xhr.onload = function(){
             if(xhr.status<200||xhr.status>=300) return;
             if (xhr.status >= 200 && xhr.status < 300) {
            var bytes = new Uint8Array(xhr.response);
                var bytes = new Uint8Array(xhr.response);
            for(var i=0;i<bytes.length-4;i++){
                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){
                    if (
                    freeze(imgEl);
                        bytes[i]   === 0x61 &&
                    break;
                        bytes[i+1] === 0x63 &&
                        bytes[i+2] === 0x54 &&
                        bytes[i+3] === 0x4C
                    ) {
                        freeze(img);
                        break;
                    }
                 }
                 }
             }
             }
Строка 1372: Строка 1377:
         xhr.send();
         xhr.send();
     }
     }
     function freeze(imgEl){
 
         var canvas = document.createElement('canvas'),
     function freeze(img){
             w = imgEl.getAttribute('width') || imgEl.naturalWidth,
         var ow = img.naturalWidth,
             h = imgEl.getAttribute('height') || imgEl.naturalHeight;
             oh = img.naturalHeight,
         canvas.width = w;
             canvas = document.createElement('canvas');
         canvas.height = h;
         canvas.width = ow;
         canvas.getContext('2d').drawImage(imgEl, 0, 0, w, h);
         canvas.height = oh;
         canvas.getContext('2d').drawImage(img, 0, 0, ow, oh);
         try {
         try {
             imgEl.src = canvas.toDataURL('image/png');
             var dataUrl = canvas.toDataURL('image/png');
         } catch(e) {
            img.src = dataUrl;
        }
            var newH = 64;
            var newW = Math.round( ow * ( newH / oh ) );
            img.setAttribute('width', newW);
            img.setAttribute('height', newH);
         } catch(e) {}
     }
     }
     $(function(){
     $(function(){
         $('img[src$=".png"]').each(function(){
         $('img[src$=".png"]').each(function(){
            var img = this;
             if (this.complete) {
             if(img.complete) {
                 detectAndFreeze(this);
                 detectAndFreeze(img);
             } else {
             } else {
                 img.addEventListener('load', function(){ detectAndFreeze(img); });
                 this.addEventListener('load', function(){
                    detectAndFreeze(this);
                });
             }
             }
         });
         });
     });
     });
})(jQuery,mediaWiki);
})(jQuery,mediaWiki);