MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
||
| Строка 1355: | Строка 1355: | ||
// test | // test | ||
(function($,mw){ | (function($,mw){ | ||
function | 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 | xhr.onload = function(){ | ||
if(xhr.status<200||xhr.status>=300) return; | |||
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(imgEl); | |||
break; | |||
} | } | ||
} | } | ||
}; | }; | ||
xhr.send(); | xhr.send(); | ||
} | } | ||
function | function freeze(imgEl){ | ||
var | var canvas = document.createElement('canvas'), | ||
w = imgEl.getAttribute('width') || imgEl.naturalWidth, | |||
h = imgEl.getAttribute('height') || imgEl.naturalHeight; | |||
h= | canvas.width = w; | ||
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. | } catch(e) { | ||
} | |||
} | } | ||
$(function(){ | $(function(){ | ||
$('img[src$=".png"]').each(function(){ | $('img[src$=".png"]').each(function(){ | ||
var img=this | var img = this; | ||
if(img.complete) { | |||
detectAndFreeze(img); | |||
} else { | |||
img.addEventListener('load', function(){ detectAndFreeze(img); }); | |||
} | |||
} | |||
}); | }); | ||
}); | }); | ||
})(jQuery,mediaWiki); | })(jQuery,mediaWiki); | ||