MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
||
| Строка 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); | ||
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); | ||
callback( true ); | |||
return; | return; | ||
} | } | ||
} | } | ||
} | } | ||
callback(false); | |||
callback( false ); | |||
}; | }; | ||
xhr.onerror=function(){callback(false);}; | |||
xhr.send(); | xhr.send(); | ||
} | } | ||
function replaceWithCanvas(imgEl){ | |||
function replaceWithCanvas( imgEl ) { | var wAttr=imgEl.getAttribute('width'), | ||
hAttr=imgEl.getAttribute('height'), | |||
w=wAttr?parseInt(wAttr,10):imgEl.naturalWidth||imgEl.width, | |||
h=hAttr?parseInt(hAttr,10):imgEl.naturalHeight||imgEl.height, | |||
canvas=document.createElement('canvas'); | |||
canvas.width=w; | |||
canvas.height=h; | |||
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){ | ||
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);}); | ||
} | } | ||
} | } | ||
} ); | }); | ||
} ); | }); | ||
} ); | }); | ||
} )( jQuery, mediaWiki ); | })(jQuery,mediaWiki); | ||