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

мНет описания правки
Метка: отменено
мНет описания правки
Метка: отменено
Строка 1354: Строка 1354:


// test
// test
( function ( $, mw ) {
(function($,mw){
     function detectAPNG( url, callback ) {
     function detectAPNG(url,callback){
         var xhr = new XMLHttpRequest();
         var xhr=new XMLHttpRequest();
         xhr.open( 'GET', url, true );
         xhr.open('GET',url,true);
         xhr.responseType = 'arraybuffer';
         xhr.responseType='arraybuffer';
         xhr.onload = function () {
         xhr.onload=function(){
             if ( xhr.status >= 200 && xhr.status < 300 ) {
             if(xhr.status>=200&&xhr.status<300){
                 var bytes = new Uint8Array( xhr.response );
                 var bytes=new Uint8Array(xhr.response);
                var acTL = [ 0x61, 0x63, 0x54, 0x4C ];
                 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 (
                         callback(true);
                        bytes[i]   === acTL[0] &&
                        bytes[i+1] === acTL[1] &&
                        bytes[i+2] === acTL[2] &&
                        bytes[i+3] === acTL[3]
                    ) {
                         callback( true );
                         return;
                         return;
                     }
                     }
                 }
                 }
             }
             }
            callback( false );
             callback(false);
        };
        xhr.onerror = function () {
             callback( false );
         };
         };
        xhr.onerror=function(){callback(false);};
         xhr.send();
         xhr.send();
     }
     }
 
     function replaceWithCanvas(imgEl){
     function replaceWithCanvas( imgEl ) {
         var wAttr=imgEl.getAttribute('width'),
         try {
             hAttr=imgEl.getAttribute('height'),
             var canvas = document.createElement( 'canvas' );
             w=wAttr?parseInt(wAttr,10):imgEl.naturalWidth||imgEl.width,
             canvas.width  = imgEl.naturalWidth || imgEl.width;
             h=hAttr?parseInt(hAttr,10):imgEl.naturalHeight||imgEl.height,
             canvas.height = imgEl.naturalHeight || imgEl.height;
            canvas=document.createElement('canvas');
            canvas.getContext( '2d' ).drawImage( imgEl, 0, 0 );
        canvas.width=w;
            imgEl.parentNode.replaceChild( canvas, imgEl );
        canvas.height=h;
        } catch ( e ) {
        canvas.getContext('2d').drawImage(imgEl,0,0,w,h);
        }
        imgEl.parentNode.replaceChild(canvas,imgEl);
     }
     }
 
     $(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){
            var url = img.src;
                 if(isAPNG){
 
                     if(img.complete&&img.naturalWidth){
             detectAPNG( url, function ( isAPNG ) {
                         replaceWithCanvas(img);
                 if ( isAPNG ) {
                     if ( img.complete && img.naturalWidth ) {
                         replaceWithCanvas( img );
                     } else {
                     } else {
                         img.addEventListener( 'load', function () {
                         img.addEventListener('load',function(){replaceWithCanvas(img);});
                            replaceWithCanvas( img );
                        } );
                     }
                     }
                 }
                 }
             } );
             });
         } );
         });
     } );
     });
} )( jQuery, mediaWiki );
})(jQuery,mediaWiki);