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

мНет описания правки
Метка: отменено
мНет описания правки
Метка: отменено
Строка 1355: Строка 1355:
// test
// test
(function($,mw){
(function($,mw){
     function detectAPNG(url,callback){
     function detectAndFreeze(imgEl){
         var xhr=new XMLHttpRequest();
        var url = imgEl.src;
         xhr.open('GET',url,true);
         var xhr = new XMLHttpRequest();
         xhr.responseType='arraybuffer';
         xhr.open('GET', url, true);
         xhr.onload=function(){
         xhr.responseType = 'arraybuffer';
             if(xhr.status>=200&&xhr.status<300){
         xhr.onload = function(){
                var bytes=new Uint8Array(xhr.response);
             if(xhr.status<200||xhr.status>=300) return;
                for(var i=0;i<bytes.length-4;i++){
            var bytes = new Uint8Array(xhr.response);
                    if(bytes[i]===0x61&&bytes[i+1]===0x63&&bytes[i+2]===0x54&&bytes[i+3]===0x4C){
            for(var i=0;i<bytes.length-4;i++){
                        callback(true);
                if(bytes[i]===0x61&&bytes[i+1]===0x63&&bytes[i+2]===0x54&&bytes[i+3]===0x4C){
                        return;
                    freeze(imgEl);
                    }
                    break;
                 }
                 }
             }
             }
            callback(false);
         };
         };
        xhr.onerror=function(){callback(false);};
         xhr.send();
         xhr.send();
     }
     }
     function replaceWithCanvas(imgEl){
     function freeze(imgEl){
         var wAttr=imgEl.getAttribute('width'),
         var canvas = document.createElement('canvas'),
             hAttr=imgEl.getAttribute('height'),
             w = imgEl.getAttribute('width') || imgEl.naturalWidth,
            w=wAttr?parseInt(wAttr,10):imgEl.naturalWidth||imgEl.width,
             h = imgEl.getAttribute('height') || imgEl.naturalHeight;
             h=hAttr?parseInt(hAttr,10):imgEl.naturalHeight||imgEl.height,
         canvas.width = w;
            canvas=document.createElement('canvas');
         canvas.height = h;
         canvas.width=w;
         canvas.getContext('2d').drawImage(imgEl, 0, 0, w, h);
         canvas.height=h;
         try {
         canvas.getContext('2d').drawImage(imgEl,0,0,w,h);
            imgEl.src = canvas.toDataURL('image/png');
         imgEl.parentNode.replaceChild(canvas,imgEl);
        } catch(e) {
        }
     }
     }
     $(function(){
     $(function(){
         $('img[src$=".png"]').each(function(){
         $('img[src$=".png"]').each(function(){
             var img=this, url=img.src;
             var img = this;
             detectAPNG(url,function(isAPNG){
             if(img.complete) {
                if(isAPNG){
                detectAndFreeze(img);
                    if(img.complete&&img.naturalWidth){
            } else {
                        replaceWithCanvas(img);
                img.addEventListener('load', function(){ detectAndFreeze(img); });
                    } else {
             }
                        img.addEventListener('load',function(){replaceWithCanvas(img);});
                    }
                }
             });
         });
         });
     });
     });
})(jQuery,mediaWiki);
})(jQuery,mediaWiki);