MediaWiki:Gadget-freezeAPNG.js: различия между версиями

Материал из Space Station 14 Вики
м Pok переименовал страницу MediaWiki:Gadget-FreezeAPNG.js в MediaWiki:Gadget-freezeAPNG.js
мНет описания правки
Строка 25: Строка 25:
         var origW = img.getAttribute('width'),
         var origW = img.getAttribute('width'),
             origH = img.getAttribute('height'),
             origH = img.getAttribute('height'),
             ow = img.naturalWidth,
             ow   = img.naturalWidth,
             oh = img.naturalHeight,
             oh   = img.naturalHeight,
             canvas = document.createElement('canvas');
             canvas = document.createElement('canvas');
         canvas.width  = ow;
         canvas.width  = ow;
Строка 39: Строка 39:


     $(function(){
     $(function(){
         $('img[src$=".png"]').each(function(){
         $('.freezeAPNG img[src$=".png"]').each(function(){
             if (this.complete) {
             if (this.complete) {
                 detectAndFreeze(this);
                 detectAndFreeze(this);

Версия от 16:42, 30 июня 2025

(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);