spy-client
Version:
spy client
2 lines (1 loc) • 7.49 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SpyLocalCache=e()}(this,function(){"use strict";var r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])})(t,e)};function t(t,e){function o(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var l=(e.prototype.preOrder=function(t){t.push(this),this.left&&this.left.preOrder(t),this.right&&this.right.preOrder(t)},e);function e(t,e){this.data=t,this.weight=e}function n(t){var e=function(t){if(null==t)return null;var i={};return function t(e,o,r){var n=[].concat(r);n.push(o),null!=e&&(null==e.data?(t(e.left,"0",n),t(e.right,"1",n)):i[e.data]=n.join(""))}(t,"",[]),i}(function(t){for(var e=function(t,e){return t.weight-e.weight};1<t.length;){t.sort(e);var o=t.shift(),r=t.shift();if(o&&r){var n=new l(null,o.weight+r.weight);n.left=o,n.right=r,t.unshift(n)}}return t.shift()}(function(t){for(var e=[],o={},r=0,n=t;r<n.length;r++){var i=n[r];null==o[i]?o[i]=1:o[i]++}for(var a=0,s=Object.entries(o);a<s.length;a++){var u=s[a],h=u[0],c=u[1];e.push(new l(h,c))}return e}(t)));return{result:function(t){for(var e=t.join(""),o=Math.ceil(e.length/8),r=new Array(o+1),n=0,i="",a=0;a<e.length;a+=8)i=e.substring(a,a+8),r[n]=parseInt(i,2),n++;return r[n]=i.length,r}(function(t,e){for(var o=[],r=0,n=t;r<n.length;r++){var i=n[r];o.push(e[i])}return o}(t,e)),codes:e}}function y(t,e){t&&(e|=256);var o=Number(e).toString(2);return t?o.substring(o.length-8):o}function i(t){for(var e=[],o=0;o<t.length;o++)e.push(t.charCodeAt(o));return e}function a(t){for(var e="",o=0,r=t;o<r.length;o++){var n=r[o];e+=String.fromCharCode(n)}return e}function o(t,e){return a(function(t,e){for(var o=[],r=0;r<e.length-1;r++){var n=r!==e.length-2;o.push(y(n,e[r]))}var i=o[o.length-1],a=e[e.length-1];i="00000000".substring(8-(a-i.length))+i,o[o.length-1]=i;for(var s={},u=0,h=Object.entries(t);u<h.length;u++){var c=h[u],l=c[0];s[c[1]]=l}var p=o.join(""),f=[],d=p.length;for(r=0;r<d;){for(var g=1,v=(n=!0,null);n&&r+g<=d;){if(""===(l=p.substring(r,r+g)))break;(v=s[l])?n=!1:g++}f.push(parseInt(v,10)),r+=g}return f}(t,i(e)))}var s=(u.prototype.set=function(t,e){},u.prototype.get=function(t,e){},u.prototype.rm=function(t){},u);function u(){}var h,c=(t(p,h=s),p.isSupport=function(){return!!window.localStorage},p.prototype.set=function(t,e){try{localStorage.setItem(t,e)}catch(t){console.error(t)}},p.prototype.get=function(t,e){var o=null;try{o=localStorage.getItem(t)}catch(t){console.error(t)}e&&e(o)},p.prototype.rm=function(t){try{localStorage.removeItem(t)}catch(t){console.error(t)}},p);function p(){return null!==h&&h.apply(this,arguments)||this}var f,d=(t(g,f=s),g.isSupport=function(){return!!window.indexedDB},g.prototype.set=function(t,e){this.db?this.db.transaction([this.databaseName],"readwrite").objectStore(this.databaseName).add({key:t,value:e}):this.setQueue.push({key:t,value:e})},g.prototype.get=function(t,e){if(this.db){var o=this.db.transaction([this.databaseName]).objectStore(this.databaseName).get(t);o.onsuccess=function(){var t=o.result?o.result:{value:""};e(t.value)}}else this.getQueue.push({key:t,cb:e})},g.prototype.runQueueTask=function(){var e=this;this.setQueue.forEach(function(t){e.set(t.key,t.value)}),this.getQueue.forEach(function(t){e.get(t.key,t.cb)})},g);function g(){var e=f.call(this)||this;e.databaseName="spyLC",e.db=null,e.setQueue=[],e.getQueue=[];var t=window.indexedDB.open(e.databaseName);return t.onupgradeneeded=function(t){e.db=t.target&&t.target.result,e.db&&!e.db.objectStoreNames.contains(e.databaseName)&&e.db.createObjectStore(e.databaseName,{keyPath:"key"}),e.runQueueTask()},t.onsuccess=function(){e.db=t.result,e.runQueueTask()},e}function v(t,e){if(void 0===e&&(e=!1),!t)return"";e||(t=function(t){for(var e="",o=0;o<t.length;o++){var r=t.charCodeAt(o);r<128?e+=String.fromCharCode(r):(127<r&&r<2048?e+=String.fromCharCode(r>>6|192):(e+=String.fromCharCode(r>>12|224),e+=String.fromCharCode(r>>6&63|128)),e+=String.fromCharCode(63&r|128))}return e}(t));function o(){s.push(1<u.length?String.fromCharCode(a[u]):u)}var r,n,i,a={},s=[],u=t.charAt(0),h=u,c=h,l=256;for(e&&s.push(u),r=1;r<t.length;r++)n=t.charAt(r),e?(i=t.charCodeAt(r),u=i<256?n:a[i]||u+h,s.push(u),h=u.charAt(0),a[l++]=c+h,c=u):a.hasOwnProperty(u+n)?u+=n:(o(),a[u+n]=l++,u=n);e||o();var p=s.join("");return e&&(p=function(t){for(var e="",o=0,r=0,n=0,i=0;o<t.length;)(r=t.charCodeAt(o))<128?(e+=String.fromCharCode(r),o++):191<r&&r<224?(n=t.charCodeAt(o+1),e+=String.fromCharCode((31&r)<<6|63&n),o+=2):(n=t.charCodeAt(o+1),i=t.charCodeAt(o+2),e+=String.fromCharCode((15&r)<<12|(63&n)<<6|63&i),o+=3);return e}(p)),p}function m(t){void 0===t&&(t={}),this.tmpList=[],this.option=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e=void 0,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,t)}({defaultTrigger:!0,compress:"lzw",key:"SpyLocalCache",interval:500,maxRecordLen:30,onFlush:function(){},onSave:function(){},onAdd:function(){return!0},storage:d.isSupport()?"indexedDB":c.isSupport()?"localstorage":"empty"},t),this.load=this.load.bind(this),this.init()}return m.prototype.init=function(){"indexedDB"===this.option.storage?this.storage=new d:"localstorage"===this.option.storage?this.storage=new c:this.storage=new s,"complete"===document.readyState?this.load():window.addEventListener("load",this.load)},m.prototype.load=function(){-1<location.search.indexOf("_FlushLogLocalCache=1")&&this.option.defaultTrigger&&this.flushLog()},m.prototype.addLog=function(t){var e=this;this.option.onAdd&&!this.option.onAdd(t)||(t=JSON.stringify(t),this.tmpList.push(t),this.timer&&clearTimeout(this.timer),this.timer=setTimeout(function(){e.save()},this.option.interval))},m.prototype.getData=function(r){var n=this;try{this.storage.get(this.option.key,function(o){o?n.storage.get(n.option.key+"Codes",function(t){var e=[];try{t=t&&JSON.parse(t),e=n.unzip(o,t).split("\n")}catch(t){console.error(t)}r(e)}):r([])})}catch(t){console.error(t),r([])}},m.prototype.save=function(){var u=this,h=Date.now();this.getData(function(t){var e=u.option.maxRecordLen-1,o=(t.length>e?t.slice(t.length-e,t.length):t).concat(u.tmpList),r=o.join("\n"),n=null,i=0;try{var a=u.zip(r),s="";a.codes?(s=JSON.stringify(a.codes),u.storage.set(u.option.key+"Codes",s)):u.storage.rm(u.option.key+"Codes"),u.storage.set(u.option.key,a.result),i=a.result.length}catch(t){n=t,console.error(t)}u.tmpList=[],u.option.onSave&&u.option.onSave({cost:Date.now()-h,length:i/1024,list:o,error:n})})},m.prototype.flushLog=function(){var o=this;this.getData(function(e){try{for(var t=0;t<e.length;t++)e[t]=JSON.parse(e[t])}catch(t){e=[],o.storage.rm(o.option.key),console.error(t)}for(t=0;t<o.tmpList.length;t++)e.push(JSON.parse(o.tmpList[t]));o.option.onFlush&&o.option.onFlush(e)})},m.prototype.zip=function(t){return"lzw"===this.option.compress?{codes:null,result:v(t)}:"huffman"===this.option.compress?function(t){var e=n(i(t)),o=e.result;return{codes:e.codes,result:a(o)}}(t):{codes:null,result:t}},m.prototype.unzip=function(t,e){return"lzw"===this.option.compress?v(t,!0):"huffman"===this.option.compress?o(e,t):t},m});