MediaWiki:Gadget-freezeAPNG.js
Материал из Space Station 14 Вики
Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.
- Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
- Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
- Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
- Opera: Нажмите Ctrl+F5.
(function($, mw){
function detectAndFreeze(img){
var xhr = new XMLHttpRequest();
xhr.open('GET', img.src, true);
xhr.responseType = 'arraybuffer';
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(img);
break;
}
}
};
xhr.send();
}
function freeze(img){
var origW = img.getAttribute('width'),
origH = img.getAttribute('height'),
ow = img.naturalWidth,
oh = img.naturalHeight,
canvas = document.createElement('canvas');
canvas.width = ow;
canvas.height = oh;
canvas.getContext('2d').drawImage(img, 0, 0, ow, oh);
try {
img.src = canvas.toDataURL('image/png');
if (origW) img.setAttribute('width', origW);
if (origH) img.setAttribute('height', origH);
} catch(e){}
}
$(function(){
$('.freezeAPNG img[src$=".png"]').each(function(){
if (this.complete) {
detectAndFreeze(this);
} else {
this.addEventListener('load', function(){
detectAndFreeze(this);
});
}
});
});
})(jQuery, mediaWiki);