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

Материал из Space Station 14 Вики
мНет описания правки
Метка: отменено
мНет описания правки
Метка: ручная отмена
Строка 1: Строка 1:
( function ( $, mw ) {
(function($, mw){
     function parseChunks( buf ) {
     function detectAndFreeze(img){
         const dv = new DataView( buf );
         var xhr = new XMLHttpRequest();
         let pos = 8;
         xhr.open('GET', img.src, true);
         const chunks = [];
         xhr.responseType = 'arraybuffer';
         while ( pos < buf.byteLength ) {
         xhr.onload = function(){
             const length = dv.getUint32( pos );
             if (xhr.status < 200 || xhr.status >= 300) return;
             const typeBytes = new Uint8Array( buf, pos + 4, 4 );
             var bytes = new Uint8Array(xhr.response);
             const type = String.fromCharCode.apply( null, typeBytes );
             for (var i = 0; i < bytes.length - 4; i++){
            const dataStart = pos + 8;
                if (
            const dataEnd = dataStart + length;
                    bytes[i]  === 0x61 &&
            const crcEnd = dataEnd + 4;
                    bytes[i+1] === 0x63 &&
            chunks.push({
                    bytes[i+2] === 0x54 &&
                type,
                    bytes[i+3] === 0x4C
                start: pos,
                ){
                 end: crcEnd
                    freeze(img);
             });
                    break;
            pos = crcEnd;
                 }
         }
             }
         return chunks;
         };
         xhr.send();
     }
     }


     async function detectAndStrip( img ) {
     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 {
         try {
             const resp = await fetch( img.src );
             img.src = canvas.toDataURL('image/png');
            if ( !resp.ok ) return;
             if (origW) img.setAttribute('width', origW);
            const buf = await resp.arrayBuffer();
             if (origH) img.setAttribute('height', origH);
             const chunks = parseChunks( buf );
         } catch(e){}
 
            if ( !chunks.some( c => c.type === 'acTL' ) ) return;
 
            const keep = [];
            keep.push( buf.slice( 0, 8 ) );
             for ( const c of chunks ) {
                if ( c.type === 'acTL' || c.type === 'fcTL' ) continue;
                keep.push( buf.slice( c.start, c.end ) );
            }
 
            const blob = new Blob( keep, { type: 'image/png' } );
            const url  = URL.createObjectURL( blob );
 
            img.src = url;
         } catch ( e ) {
            console.error( 'freezeAPNG error:', e );
        }
     }
     }


     $( () => {
     $(function(){
         $( '.freezeAPNG img[src$=".png"]' ).each( ( _, img ) => {
         $('.freezeAPNG img[src$=".png"]').each(function(){
             if ( img.complete ) {
             if (this.complete) {
                 detectAndStrip( img );
                 detectAndFreeze(this);
             } else {
             } else {
                 img.addEventListener( 'load', () => detectAndStrip( img ), { once: true } );
                 this.addEventListener('load', function(){
                    detectAndFreeze(this);
                });
             }
             }
         } );
         });
     } );
     });
} )( jQuery, mediaWiki );
})(jQuery, mediaWiki);

Версия от 20:18, 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);