MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
||
| Строка 1355: | Строка 1355: | ||
// test | // test | ||
(function($,mw){ | (function($,mw){ | ||
function detectAndFreeze( | function detectAndFreeze(img){ | ||
var xhr = new XMLHttpRequest(); | var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', | xhr.open('GET', img.src, true); | ||
xhr.responseType = 'arraybuffer'; | xhr.responseType = 'arraybuffer'; | ||
xhr.onload = function(){ | xhr.onload = function(){ | ||
if(xhr.status | if (xhr.status >= 200 && xhr.status < 300) { | ||
var bytes = new Uint8Array(xhr.response); | |||
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 | |||
) { | |||
freeze(img); | |||
break; | |||
} | |||
} | } | ||
} | } | ||
| Строка 1372: | Строка 1377: | ||
xhr.send(); | xhr.send(); | ||
} | } | ||
function freeze( | |||
var | function freeze(img){ | ||
var ow = img.naturalWidth, | |||
oh = img.naturalHeight, | |||
canvas.width = | canvas = document.createElement('canvas'); | ||
canvas.height = | canvas.width = ow; | ||
canvas.getContext('2d').drawImage( | canvas.height = oh; | ||
canvas.getContext('2d').drawImage(img, 0, 0, ow, oh); | |||
try { | try { | ||
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(){ | ||
if (this.complete) { | |||
if( | detectAndFreeze(this); | ||
detectAndFreeze( | |||
} else { | } else { | ||
this.addEventListener('load', function(){ | |||
detectAndFreeze(this); | |||
}); | |||
} | } | ||
}); | }); | ||
}); | }); | ||
})(jQuery,mediaWiki); | })(jQuery,mediaWiki); | ||