-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStorage.min.js
More file actions
9 lines (9 loc) · 5.29 KB
/
Storage.min.js
File metadata and controls
9 lines (9 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
/**
* Data Storage - UserData(IE),Cookie,LocalStorage
* The MIT License - Author: <abcrun@gmail.com>
* Repository - https://github.com/abcrun/Storage.git
* Required json2.js@https://github.com/douglascrockford/JSON-js.git
* Version - 0.3.1
*/
/*json2.js@https://github.com/douglascrockford/JSON-js.git*/
if(typeof JSON!=="object"){JSON={}}(function(){"use strict";function f(e){return e<10?"0"+e:e}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()}}var cx,escapable,gap,indent,meta,rep;function quote(e){escapable.lastIndex=0;return escapable.test(e)?'"'+e.replace(escapable,function(e){var t=meta[e];return typeof t==="string"?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'}function str(e,t){var r,n,o,i,f=gap,a,u=t[e];if(u&&typeof u==="object"&&typeof u.toJSON==="function"){u=u.toJSON(e)}if(typeof rep==="function"){u=rep.call(t,e,u)}switch(typeof u){case"string":return quote(u);case"number":return isFinite(u)?String(u):"null";case"boolean":case"null":return String(u);case"object":if(!u){return"null"}gap+=indent;a=[];if(Object.prototype.toString.apply(u)==="[object Array]"){i=u.length;for(r=0;r<i;r+=1){a[r]=str(r,u)||"null"}o=a.length===0?"[]":gap?"[\n"+gap+a.join(",\n"+gap)+"\n"+f+"]":"["+a.join(",")+"]";gap=f;return o}if(rep&&typeof rep==="object"){i=rep.length;for(r=0;r<i;r+=1){if(typeof rep[r]==="string"){n=rep[r];o=str(n,u);if(o){a.push(quote(n)+(gap?": ":":")+o)}}}}else{for(n in u){if(Object.prototype.hasOwnProperty.call(u,n)){o=str(n,u);if(o){a.push(quote(n)+(gap?": ":":")+o)}}}}o=a.length===0?"{}":gap?"{\n"+gap+a.join(",\n"+gap)+"\n"+f+"}":"{"+a.join(",")+"}";gap=f;return o}}if(typeof JSON.stringify!=="function"){escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;meta={"\b":"\\b"," ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};JSON.stringify=function(e,t,r){var n;gap="";indent="";if(typeof r==="number"){for(n=0;n<r;n+=1){indent+=" "}}else if(typeof r==="string"){indent=r}rep=t;if(t&&typeof t!=="function"&&(typeof t!=="object"||typeof t.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":e})}}if(typeof JSON.parse!=="function"){cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;JSON.parse=function(text,reviver){var j;function walk(e,t){var r,n,o=e[t];if(o&&typeof o==="object"){for(r in o){if(Object.prototype.hasOwnProperty.call(o,r)){n=walk(o,r);if(n!==undefined){o[r]=n}else{delete o[r]}}}}return reviver.call(e,t,o)}text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(e){return"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})();(function(e,t){if(typeof define==="function"&&define.amd)define(t);else if(typeof module==="object"&&module.exports)module.exports=t();else this[e]=t()})("Storage",function(){var e=function(){var e;try{var t=new ActiveXObject("htmlfile"),r,e;t.open();t.write('<iframe src="/favicon.ico"></iframe>');t.close();r=t.frames[0].document;e=r.createElement("div");r.appendChild(e);e.addBehavior("#default#userData")}catch(n){}return{get:function(t){var r;e.load(t);r=e.getAttribute(t);try{r=JSON.parse(r)}catch(n){}return r},set:function(t,r,n){if(n){var o=new Date;o.setTime(o.getTime()+n*1e3);e.expires=o.toUTCString()}e.setAttribute(t,JSON.stringify(r));e.save(t)},remove:function(t){e.removeAttribute(t);e.save(t)}}};var t=function(){var e=(new Date).getTime();for(key in localStorage){var t=localStorage.getItem(key);try{t=JSON.parse(t)}catch(r){}if(Object.prototype.toString.call(t).toLowerCase().indexOf("array")>0){var n=t[0].expires;if(n&&/^\d{13,}$/.test(n)&&n<=e)localStorage.removeItem(key)}}return{get:function(e){var t=localStorage.getItem(e);if(!t)return null;try{t=JSON.parse(t)}catch(r){}if(typeof t!="object")return t;var n=t[0].expires;if(n&&/^\d{13,}$/.test(n)){var o=(new Date).getTime();if(n<=o){localStorage.removeItem(e);return null}t.shift()}return t[0]},set:function(e,t,r){var n=[];if(r){var o=(new Date).getTime();n.push({expires:o+r*1e3})}n.push(t);localStorage.setItem(e,JSON.stringify(n))},remove:function(e){localStorage.removeItem(e)}}};var r={get:function(e){var t=document.cookie,r;var n=t.indexOf(e+"="),o=t.indexOf(";",n);if(o==-1)o=t.length;if(n>-1){r=t.substring(n+e.length+1,o);try{r=JSON.parse(r)}catch(i){}return r}else{return null}},set:function(e,t,r,n,o){var n=n||"/",i="";if(r){if(window.ActiveXObject){var f=new Date;f.setTime(f.getTime()+r*1e3);i="expires="+f.toGMTString()}else{i="max-age="+r}}document.cookie=e+"="+JSON.stringify(t)+";"+i+";path="+n+";"+(o?"domain="+o:"")},remove:function(e,t,r){this.set(e,"",-1,t,r)}};var n;if(!window.localStorage){n=e()}else{n=t()}return{get:n.get,set:n.set,remove:n.remove,cookie:r}});