Skip to content

Instantly share code, notes, and snippets.

@peponi
Last active August 17, 2021 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peponi/75b76d0761520f2ef0eb3991d240c8f8 to your computer and use it in GitHub Desktop.
Save peponi/75b76d0761520f2ef0eb3991d240c8f8 to your computer and use it in GitHub Desktop.
deactivate all animated GIFs on the page after 5 sec
// needs https://github.com/liddiard/preview-gif
const GifAnimationHandler = function({timeoutInSec = 5000} = {}) {
const self = this;
const allImgs = document.querySelectorAll("img");
self.deactivateAnimatedGifs = (gifNodeList) => {
gifNodeList.forEach(gif => {
PreviewGIF(gif.dataset.src, (err, imageData) => {
if (err) console.error(err);
else gif.src = imageData;
});
});
}
self.init = () => {
if(typeof PreviewGIF !== "undefined") {
console.warn('PreviewGIF is not available in GifAnimationhandler, can\'t deactivated animated GIFs after 5 sec');
return;
}
if(allImgs) {
const pattern = /\.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$/gmi;
const gifNodeList = Array.from(allImgs).filter(img => img.src.match(pattern)[0] === ".gif");
setTimeout(() => {
self.deactivateAnimatedGifs(gifNodeList);
}, timeoutInSec)
}
}
self.init();
}
new GifAnimationHandler();
var blobURL=URL.createObjectURL(new Blob(["(",function(){var e=function(e,r){return 255&e.charCodeAt(r)},r=function(e){"undefined"!=typeof console&&console.log&&console.log(e)},t=function(e){for(var r,t,a,o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n="",i=0,s=e.length;i<s;){if(r=255&e.charCodeAt(i++),i==s){n+=o.charAt(r>>2),n+=o.charAt((3&r)<<4),n+="==";break}if(t=e.charCodeAt(i++),i==s){n+=o.charAt(r>>2),n+=o.charAt((3&r)<<4|(240&t)>>4),n+=o.charAt((15&t)<<2),n+="=";break}a=e.charCodeAt(i++),n+=o.charAt(r>>2),n+=o.charAt((3&r)<<4|(240&t)>>4),n+=o.charAt((15&t)<<2|(192&a)>>6),n+=o.charAt(63&a)}return n};onmessage=function(a){var o=a.data.buffer,n=a.data.limit;try{var i=function(a,o){var n=0;if(71!==e(a,n++)||73!==e(a,n++)||70!==e(a,n++)||56!==e(a,n++)||57!==e(a,n++)||97!==e(a,n++))throw"Invalid GIF 89a header.";e(a,n++),e(a,n++),e(a,n++),e(a,n++);var i=e(a,n++),s=i>>7,c=1<<1+(7&i);e(a,n++);e(a,n++);for(s&&(n+=3*c);n<a.length;){var f=e(a,n++);if(33==f)switch(e(a,n++)){case 255:if(11!==e(a,n)||78==e(a,n+1)&&69==e(a,n+2)&&84==e(a,n+2)&&83==e(a,n+4)&&67==e(a,n+5)&&65==e(a,n+6)&&80==e(a,n+7)&&69==e(a,n+8)&&50==e(a,n+9)&&46==e(a,n+10)&&48==e(a,n+11)&&3==e(a,n+12)&&1==e(a,n+13)&&0==e(a,n+16))n+=14,n++;else for(n+=12;0!==(l=e(a,n++));)n+=l;break;case 249:if(4!==e(a,n++)||0!==e(a,n+4))throw"Invalid graphics extension block.";var d=e(a,n++);delay=e(a,n++)|e(a,n++)<<8,transparent_index=e(a,n++),0==(1&d)&&(transparent_index=null),disposal=d>>2&7,n++;break;case 254:for(;0!==(l=e(a,n++));)n+=l;break;default:throw"Unknown graphic control label: 0x"+e(a,n-1).toString(16)}else if(44==f)break}for(n+=10;;){var l;if(0===(l=e(a,n++))&&n<=o)return r(" [Worker] found frame offset at byte "+n),"data:image/gif;base64,"+t(a.substring(0,n));if((n+=l)>=o)return r(" Worker needs to fetch more bytes "+n),-1}}(o,n);postMessage(i)}catch(a){postMessage(-2)}}}.toString(),")()"],{type:"application/javascript"}));function PreviewGIF(e,r){var t=function(){return"?cb="+Math.floor(1e6*Math.random())};!function(e,r){var a="",o=0,n=1e5,i=new Worker(blobURL),s=new XMLHttpRequest;s.open("GET",e+t()),s.setRequestHeader("Range","bytes="+o+"-"+n),s.overrideMimeType&&s.overrideMimeType("text/plain; charset=x-user-defined"),s.onload=function(){a+=s.responseText,i.onmessage=function(a){if(-1==a.data)o=n+1,n+=25e3,s.abort(),s.open("GET",e+t()),s.setRequestHeader("Range","bytes="+o+"-"+n),s.send();else if(-2==a.data)r({type:"DECODE_ERROR",message:"Error reading image from "+e}),URL.revokeObjectURL(blobURL);else{var i=a.data;r(null,i),URL.revokeObjectURL(blobURL)}},i.postMessage({buffer:a,limit:n})},s.onerror=function(){r({type:"REQUEST_ERROR",message:"Error requesting image from "+e})},s.send()}(e,r)}"undefined"!=typeof module&&(module.exports=PreviewGIF);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment