xdstore
Version:
Cross-domain storage
1 lines • 7.18 kB
JavaScript
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).xdstore=t()}}(function(){return function t(e,n,r){function i(a,o){if(!n[a]){if(!e[a]){var u="function"==typeof require&&require;if(!o&&u)return u(a,!0);if(s)return s(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var c=n[a]={exports:{}};e[a][0].call(c.exports,function(t){var n=e[a][1][t];return i(n||t)},c,c.exports,t,e,n,r)}return n[a].exports}for(var s="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(t,e,n){!function(t,n){var r={version:"2.5.9",areas:{},apis:{},inherit:function(t,e){for(var n in t)e.hasOwnProperty(n)||(e[n]=t[n]);return e},stringify:function(t){return void 0===t||"function"==typeof t?t+"":JSON.stringify(t)},parse:function(t){try{return JSON.parse(t)}catch(e){return t}},fn:function(t,e){r.storeAPI[t]=e;for(var n in r.apis)r.apis[n][t]=e},get:function(t,e){return t.getItem(e)},set:function(t,e,n){t.setItem(e,n)},remove:function(t,e){t.removeItem(e)},key:function(t,e){return t.key(e)},length:function(t){return t.length},clear:function(t){t.clear()},Store:function(t,e,n){var i=r.inherit(r.storeAPI,function(t,e,n){return 0===arguments.length?i.getAll():"function"==typeof e?i.transact(t,e,n):void 0!==e?i.set(t,e,n):"string"==typeof t||"number"==typeof t?i.get(t):t?i.setAll(t,e):i.clear()});i._id=t;try{e.setItem("_safariPrivate_","sucks"),i._area=e,e.removeItem("_safariPrivate_")}catch(t){}return i._area||(i._area=r.inherit(r.storageAPI,{items:{},name:"fake"})),i._ns=n||"",r.areas[t]||(r.areas[t]=i._area),r.apis[i._ns+i._id]||(r.apis[i._ns+i._id]=i),i},storeAPI:{area:function(t,e){var n=this[t];return n&&n.area||(n=r.Store(t,e,this._ns),this[t]||(this[t]=n)),n},namespace:function(t,e){if(!t)return this._ns?this._ns.substring(0,this._ns.length-1):"";var n=t,i=this[n];return i&&i.namespace||(i=r.Store(this._id,this._area,this._ns+n+"."),this[n]||(this[n]=i),e||i.area("session",r.areas.session)),i},isFake:function(){return"fake"===this._area.name},toString:function(){return"store"+(this._ns?"."+this.namespace():"")+"["+this._id+"]"},has:function(t){return this._area.has?this._area.has(this._in(t)):!!(this._in(t)in this._area)},size:function(){return this.keys().length},each:function(t,e){for(var n=0,i=r.length(this._area);n<i;n++){var s=this._out(r.key(this._area,n));if(void 0!==s&&!1===t.call(this,s,e||this.get(s)))break;i>r.length(this._area)&&(i--,n--)}return e||this},keys:function(){return this.each(function(t,e){e.push(t)},[])},get:function(t,e){var n=r.get(this._area,this._in(t));return null!==n?r.parse(n):e||n},getAll:function(){return this.each(function(t,e){e[t]=this.get(t)},{})},transact:function(t,e,n){var r=this.get(t,n),i=e(r);return this.set(t,void 0===i?r:i),this},set:function(t,e,n){var i=this.get(t);return null!=i&&!1===n?e:r.set(this._area,this._in(t),r.stringify(e),n)||i},setAll:function(t,e){var n,r;for(var i in t)r=t[i],this.set(i,r,e)!==r&&(n=!0);return n},remove:function(t){var e=this.get(t);return r.remove(this._area,this._in(t)),e},clear:function(){return this._ns?this.each(function(t){r.remove(this._area,this._in(t))},1):r.clear(this._area),this},clearAll:function(){var t=this._area;for(var e in r.areas)r.areas.hasOwnProperty(e)&&(this._area=r.areas[e],this.clear());return this._area=t,this},_in:function(t){return"string"!=typeof t&&(t=r.stringify(t)),this._ns?this._ns+t:t},_out:function(t){return this._ns?t&&0===t.indexOf(this._ns)?t.substring(this._ns.length):void 0:t}},storageAPI:{length:0,has:function(t){return this.items.hasOwnProperty(t)},key:function(t){var e=0;for(var n in this.items)if(this.has(n)&&t===e++)return n},setItem:function(t,e){this.has(t)||this.length++,this.items[t]=e},removeItem:function(t){this.has(t)&&(delete this.items[t],this.length--)},getItem:function(t){return this.has(t)?this.items[t]:null},clear:function(){for(var t in this.items)this.removeItem(t)},toString:function(){return this.length+" items in "+this.name+"Storage"}}},i=r.Store("local",function(){try{return localStorage}catch(t){}}());i.local=i,i._=r,i.area("session",function(){try{return sessionStorage}catch(t){}}()),"function"==typeof n&&void 0!==n.amd?n("store2",[],function(){return i}):void 0!==e&&e.exports?e.exports=i:(t.store&&(r.conflict=t.store),t.store=i)}(this,this.define)},{}],2:[function(t,e,n){e.exports=function(t,e,n){function r(t,e,n){var r=t[e];t[e]=function(){var t=r.apply(this,arguments);return n.apply(this,arguments),t}}n=n||{};var i=e._;if(e.isFake()||n.forceCookie){var s,a=new Date,o="xdstorelocal",u={},f=t.cookie.split(";");a.setTime(a.getTime()+2592e6),a=a.toGMTString();for(var c=0,h=f.length;c<h;c++){for(var l=f[c];" "===l.charAt(0);)l=l.substring(1,l.length);0===l.indexOf(o)&&(u=JSON.parse(l.substring(o.length+1)))}s=function(t,e,n){var s=0;for(var a in e)e.hasOwnProperty(a)&&s++;var o=i.inherit(i.storageAPI,{items:e,length:s,name:t});return n&&(r(o,"setItem",n),r(o,"removeItem",n)),o}("cookie",u,function(){t.cookie=o+"="+JSON.stringify(this.items)+"; expires="+a+"; path=/"+(n.secure?";secure":"")}),e._area=i.areas.local=s}return e}},{}],3:[function(t,e,n){var r=t("store2"),i=function(){navigator.userAgent.indexOf("Phantom"),navigator.userAgent.indexOf("Chrome");try{return r.set("_sane",1),1===r.get("_sane")&&(r.remove("_sane"),!0)}catch(t){return!1}}(),s=t("./cookiestore.js");r=s(window.document,r,{forceCookie:!i,secure:"https:"===location.protocol}),e.exports=r},{"./cookiestore.js":2,store2:1}],4:[function(t,e,n){var r=t("./store");e.exports=function(t){function e(){return s.instance+"-"+(1+s.messageId++)}function n(t,n,r){s.target.postMessage(JSON.stringify({namespace:s.namespace,id:r||e(),name:t,data:n}),s.targetUrl)}function i(t){if(s.allowOrigin instanceof RegExp){if(!t.origin.match(s.allowOrigin))return}else if("*"!==t.origin&&"null"!==t.origin&&t.origin!==s.allowOrigin)return;try{var e=JSON.parse(t.data)}catch(t){return}if(e.namespace===s.namespace){if("_init_"===e.name)return"PING"===e.data&&n("_init_","PONG"),s.onReady();a[e.name]&&a[e.name](e.data,function(t,r){n("_callback_"+e.name,{error:t,data:r},e.id)}),o[e.id]&&(o[e.id](e.data.error,e.data.data),delete o[e.id])}}if(!t.target)return{set:function(){},get:function(){},remove:function(){}};var s={target:t.target,targetUrl:t.targetUrl,onReady:t.onReady||function(){},namespace:t.namespace||"xdstore",allowOrigin:t.allowOrigin||"*",instance:Math.random().toString(36).substr(2,9),messageId:0},a={_set_:function(t,e){r.set(t.key,t.value),e(null)},_get_:function(t,e){e(null,r.get(t.key))},_remove_:function(t,e){e(null,r.remove(t.key))}},o={};return window.addEventListener?window.addEventListener("message",i,!1):window.attachEvent("onmessage",i),setTimeout(function(){n("_init_","PING")},0),{set:function(t,r,i){var s=e();i&&(o[s]=i),n("_set_",{key:t,value:r},s)},get:function(t,r){var i=e();r&&(o[i]=r),n("_get_",{key:t},i)},remove:function(t,r){var i=e();r&&(o[i]=r),n("_remove_",{key:t},i)}}}},{"./store":3}]},{},[4])(4)});