-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.cacheImages.min.js
1 lines (1 loc) · 9.17 KB
/
jquery.cacheImages.min.js
1
!function(a){a.fn.cacheImages=function(b){this.cacheImagesConfig=a.extend({},a.fn.cacheImages.defaults,b),this.cacheImagesConfig.encodeOnCanvas=void 0!=typeof HTMLCanvasElement&&this.cacheImagesConfig.encodeOnCanvas,"boolean"!=typeof this.cacheImagesConfig.forceSave&&(this.cacheImagesConfig.forceSave=!1);var c=this;return"function"==typeof this.cacheImagesConfig.start&&c.cacheImagesConfig.start(this),a.fn.cacheImages.testOutput(this.cacheImagesConfig.defaultImage,!0)===!1&&(this.cacheImagesConfig.defaultSrc=this.cacheImagesConfig.defaultImage),this.each(function(b,d){a.fn.cacheImages.storageAvailable(a(d),b,d,function(b,d){var f,e=a(d);if("IMG"===e.prop("tagName")){e.data("cachedImageType","src");var f=e.prop("src")||e.data("cachedImageSrc");null!==c.cacheImagesConfig.url&&(f=c.cacheImagesConfig.url,e.prop("src",""))}else{e.data("cachedImageType","css");var f=e.css("background-image").replace(/"/g,"").replace(/url\(|\)$/gi,"")||e.data("cachedImageSrc");null!==c.cacheImagesConfig.url&&(f=c.cacheImagesConfig.url,e.css("background-image","url()"))}if("undefined"==typeof f)return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: Error - no URI to load"),c.cacheImagesConfig.fail.call(this),c.cacheImagesConfig.always.call(this),!0;if("undefined"!=typeof e.prop("src")&&a.fn.cacheImages.testOutput(e.prop("src"),!0))return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: already displaying cached image"),c.cacheImagesConfig.done.call(this),c.cacheImagesConfig.always.call(this),!0;var g=c.cacheImagesConfig.storagePrefix+":"+f;a.fn.cacheImages.get(e,g,function(b,g){if(0==c.cacheImagesConfig.forceSave&&g&&a.fn.cacheImages.testOutput(g,!0))return this.data("cachedImageSrc",f),"src"==this.data("cachedImageType")?this.prop("src",g):this.css("background-image","url("+g+")"),a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: Already Encoded"),c.cacheImagesConfig.done.call(this,g),void c.cacheImagesConfig.always.call(this);if("pending"===g)return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: Caching in Progress - "+f),c.cacheImagesConfig.fail.call(this),void c.cacheImagesConfig.always.call(this);"src"==this.data("cachedImageType")?this.prop("src",""):this.css("background-image","url()");var h=f.match(/\.(jpg|jpeg|png|gif)$/i);if(h&&h.length&&(h="jpg"==h[1].toLowerCase()?"jpeg":h[1].toLowerCase()),"undefined"==typeof h)return c.cacheImagesConfig.fail.call(this),void c.cacheImagesConfig.always.call(this);if(this.data("cachedImageSrc",f),a.fn.cacheImages.set(this,b,"pending",function(a,b){}),c.cacheImagesConfig.encodeOnCanvas&&"gif"!==h)a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: Preparing to Cache : canvas - "+f),e.load(function(){newSrc=a.fn.cacheImages.base64EncodeCanvas(d),a.fn.cacheImages.set(this,b,newSrc),a.fn.cacheImages.testOutput(newSrc,!0)?("src"==this.data("cachedImageType")?this.prop("src",newSrc):this.css("background-image","url("+newSrc+")"),this.is(".cacheImagesRemove")&&this.remove(),c.cacheImagesConfig.done.call(this,newSrc)):c.cacheImagesConfig.fail.call(this),c.cacheImagesConfig.always.call(this)});else{a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: Preparing to Cache : XHR ArrayBuffer - "+f);var i=new XMLHttpRequest,j=this;i.open("GET",f,!0),i.responseType="arraybuffer",i.onload=function(d){newSrc="",200==this.status&&(newSrc="data:image/"+h+";base64,"+a.fn.cacheImages.base64EncodeResponse(this.response)),a.fn.cacheImages.set(j,b,newSrc,function(b,d){return a.fn.cacheImages.testOutput(d,!0)?("src"==this.data("cachedImageType")?this.prop("src",d):this.css("background-image","url("+d+")"),this.is(".cacheImagesRemove")&&this.remove(),c.cacheImagesConfig.done.call(this,d),void c.cacheImagesConfig.always.call(this)):void a.fn.cacheImages.set(this,b,"error",function(a,b){return"undefined"!=typeof c.cacheImagesConfig.defaultSrc?(c.cacheImagesConfig.url=c.cacheImagesConfig.defaultImage,this.cacheImages(c.cacheImagesConfig),c.cacheImagesConfig.fail.call(this),void c.cacheImagesConfig.always.call(this)):("src"==this.data("cachedImageType")?this.prop("src",c.cacheImagesConfig.defaultImage):this.css("background-image","url("+c.cacheImagesConfig.defaultImage+")"),c.cacheImagesConfig.fail.call(this),c.cacheImagesConfig.always.call(this),void 0)})})},i.send()}})})})},a.fn.cacheImages.defaults={always:function(){},debug:!1,defaultImage:"data:image/png;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAHgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQAEAsLCwwLEAwMEBcPDQ8XGxQQEBQbHxcXFxcXHx4XGhoaGhceHiMlJyUjHi8vMzMvL0BAQEBAQEBAQEBAQEBAQAERDw8RExEVEhIVFBEUERQaFBYWFBomGhocGhomMCMeHh4eIzArLicnJy4rNTUwMDU1QEA/QEBAQEBAQEBAQEBA/8AAEQgAZABkAwEiAAIRAQMRAf/EAEsAAQEAAAAAAAAAAAAAAAAAAAAFAQEAAAAAAAAAAAAAAAAAAAAAEAEAAAAAAAAAAAAAAAAAAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//2Q==",done:function(a){},encodeOnCanvas:!1,fail:function(){},forceSave:!1,ready:!0,start:function(){},storageDB:"localStorage",storagePrefix:"cached",url:null},a.fn.cacheImages.storageAvailable=function(b,c,d,e){return"object"==typeof localStorage?void e.call(b,c,d):void(a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage: Error - localStorage not available"))},a.fn.cacheImages.set=function(a,b,c,d){localStorage[b]=c,"function"==typeof d&&d.call(a,b,c)},a.fn.cacheImages.get=function(a,b,c){var d=null;"undefined"!=typeof localStorage[b]&&(d=localStorage[b]),"function"==typeof c&&c.call(a,b,d)},a.fn.cacheImages.base64EncodeCanvas=function(b){try{var c=document.createElement("canvas");c.width=b.width,c.height=b.height;var d=c.getContext("2d");d.drawImage(b,0,0);var e=b.src.match(/\.(jpg|jpeg|png)$/i);if(!e||!e.length)throw"Invalid image type for canvas encoder: "+b.src;return e="jpg"==e[1].toLowerCase()?"jpeg":e[1].toLowerCase(),c.toDataURL("image/"+e)}catch(b){return a.fn.cacheImages.defaults.debug&&console&&console.log("FV.cacheImage.base64EncodeCanvas: Error while Encoding",b),"error"}},a.fn.cacheImages.base64EncodeResponse=function(a){for(var h,i,j,k,l,b="",c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",d=new Uint8Array(a),e=d.byteLength,f=e%3,g=e-f,m=0;m<g;m+=3)l=d[m]<<16|d[m+1]<<8|d[m+2],h=(16515072&l)>>18,i=(258048&l)>>12,j=(4032&l)>>6,k=63&l,b+=c[h]+c[i]+c[j]+c[k];return 1===f?(l=d[g],h=(252&l)>>2,i=(3&l)<<4,b+=c[h]+c[i]+"=="):2===f&&(l=d[g]<<8|d[g+1],h=(16128&l)>>8,i=(1008&l)>>4,j=(15&l)<<2,b+=c[h]+c[i]+c[j]+"="),b},a.fn.cacheImages.fetchURL=function(b,c){var b;a("body").append(a('<img style="display: none;" />').addClass("cacheImagesRemove").cacheImages({url:b,done:function(a){"function"==typeof c&&c.call(this,b,a)}}))},a.fn.cacheImages.Output=function(b,c,d,e){"undefined"==typeof d&&(d=a.fn.cacheImages.defaults.storagePrefix),"boolean"!=typeof e&&(e=!1);var g,f=d+":"+b;return null!=window.localStorage.getItem(f)&&(g=window.localStorage.getItem(f),0==a.fn.cacheImages.testOutput(g,!0)&&(delete g,0==e))?void a("body").append(a('<img style="display: none;" />').addClass("cacheImagesRemove").cacheImages({url:b,forceSave:!0,storagePrefix:d,done:function(e){"function"==typeof c&&a.fn.cacheImages.Output(b,c,d,!0)}})):(0==a.fn.cacheImages.testOutput(g,!0)&&(a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.Output: Failed to load image "+b),a.fn.cacheImages.testOutput(a.fn.cacheImages.defaults.defaultImage,!0)?g=a.fn.cacheImages.defaults.defaultImage:(f=d+":"+a.fn.cacheImages.defaults.defaultImage,null!=window.localStorage.getItem(f)&&(g=window.localStorage.getItem(f)))),"function"==typeof c?void c.call(this,g):g)},a.fn.cacheImages.drop=function(b,c,d){var e=[];for("undefined"==typeof d&&(d=a.fn.cacheImages.defaults.storagePrefix),"undefined"==typeof b&&(b=null),i=0;i<window.localStorage.length;i++)window.localStorage.key(i).substr(0,d.length+1)===d+":"&&(null!==b&&window.localStorage.key(i)!==d+":"+b||e.push(window.localStorage.key(i)));if(0===e.length)return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.drop: No Images to Drop"),"function"==typeof c&&c.call(this,b),b;for(i=0;i<e.length;i++)a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.drop: Dropping localStorage Key:",e[i]),window.localStorage.removeItem(e[i]);return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.drop: Dropped "+e.length+" images from storage"),"function"==typeof c&&c.call(this,b),b},a.fn.cacheImages.testOutput=function(b,c){if("undefined"==typeof c&&(c=!1),0==b.length||"pending"==b||"error"==b||1==/^http/.test(b))return!1;if(c){if(/^data:image/.test(b)===!1)return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.textOutput: Er1 - Missing data image prefix"),!1;if(/;base64,/.test(b)===!1)return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.textOutput: Er2 - Missing ;base64, prefix"),!1;b=b.substr(b.indexOf(";base64,")+8)}if(0==b.length)return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.textOutput: Er3 - No encoded value"),!1;try{return btoa(atob(b))==b||(a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.textOutput: Er5 - Improperly encoded base64 value"),!1)}catch(b){return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.textOutput: Er4 - Invalide base64 value"),!1}return a.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.textOutput: Er6 - You reached an unreachable point"),!1}}(jQuery);