MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) м test Метка: отменено |
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
||
| Строка 1354: | Строка 1354: | ||
// test | // test | ||
( function () { | ( function ( $, mw ) { | ||
function detectAPNG( url, callback ) { | |||
var xhr = new XMLHttpRequest(); | |||
xhr.open( 'GET', url, true ); | |||
xhr.responseType = 'arraybuffer'; | |||
xhr.onload = function () { | |||
if ( xhr.status >= 200 && xhr.status < 300 ) { | |||
var bytes = new Uint8Array( xhr.response ); | |||
var acTL = [ 0x61, 0x63, 0x54, 0x4C ]; | |||
for ( var i = 0; i < bytes.length - 4; i++ ) { | |||
if ( | |||
bytes[i] === acTL[0] && | |||
bytes[i+1] === acTL[1] && | |||
bytes[i+2] === acTL[2] && | |||
bytes[i+3] === acTL[3] | |||
) { | |||
callback( true ); | |||
return; | |||
} | |||
} | } | ||
} | } | ||
callback( false ); | |||
}; | |||
xhr.onerror = function () { | |||
callback( false ); | |||
}; | |||
xhr.send(); | |||
} | |||
function replaceWithCanvas( imgEl ) { | |||
try { | |||
var canvas = document.createElement( 'canvas' ); | |||
canvas.width = imgEl.naturalWidth || imgEl.width; | |||
canvas.height = imgEl.naturalHeight || imgEl.height; | |||
canvas.getContext( '2d' ).drawImage( imgEl, 0, 0 ); | |||
imgEl.parentNode.replaceChild( canvas, imgEl ); | |||
} catch ( e ) { | } catch ( e ) { | ||
} | } | ||
} | } | ||
$( function () { | $( function () { | ||
$( 'img[src$=".png"]' ).each( | $( 'img[src$=".png"]' ).each( function () { | ||
var img = this; | |||
var url = img.src; | |||
detectAPNG( url, function ( isAPNG ) { | |||
if ( isAPNG ) { | |||
if ( img.complete && img.naturalWidth ) { | |||
replaceWithCanvas( img ); | |||
} else { | |||
img.addEventListener( 'load', function () { | |||
} | replaceWithCanvas( img ); | ||
} ); | |||
} | |||
} | |||
} ); | |||
} ); | } ); | ||
} ); | } ); | ||
} )(); | } )( jQuery, mediaWiki ); | ||