Skip to content

Commit

Permalink
Merge c483d7e into bd8b818
Browse files Browse the repository at this point in the history
  • Loading branch information
cbbfcd committed Dec 18, 2018
2 parents bd8b818 + c483d7e commit a0f7b21
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var storage = window.localStorage;
var storage = window.localStorage, _data = {};

function isJSON(obj) {
return typeof (obj) === "object" && Object.prototype.toString.call(obj).toLowerCase() === "[object object]" && !obj.length;
Expand All @@ -13,23 +13,66 @@ function deserialize(value) {
}
function isFunction(value) { return ({}).toString.call(value) === "[object Function]"; }
function isArray(value) { return Object.prototype.toString.call(value) === "[object Array]"; }
// https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError
function dealIncognito(storage) {
var _KEY = '_Is_Incognit', _VALUE = 'yes';
try { storage.setItem(_KEY, _VALUE) }
catch (e) {
if (e.name === 'QuotaExceededError') {
var _nothing = function () { };
storage.__proto__ = { setItem: _nothing, getItem: _nothing, removeItem: _nothing, clear: _nothing };

// http://crocodillon.com/blog/always-catch-localstorage-security-and-quota-exceeded-errors
function isQuotaExceeded(e) {
let quotaExceeded = false;
if (e) {
if (e.code) {
switch (e.code) {
case 22:
quotaExceeded = true;
break;
case 1014:
// Firefox
if (e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
quotaExceeded = true;
}
break;
}
} else if (e.number === -2147024882) {
// Internet Explorer 8
quotaExceeded = true;
}
}
finally { if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); }
return storage;
return quotaExceeded;
}

// deal QuotaExceededError if user use incognito mode in browser
storage = dealIncognito(storage);
// 隐身模式下的 hack
(function(){
function isSupported(){
const _KEY = '_Is_Incognit', _VALUE = 'yes';
try {
storage.setItem(_KEY, _VALUE)
return true;
} catch (error) {
return false;
} finally {
if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY);
}
}

if (!isSupported()) {
try {
storage.__proto__ = {
setItem: function (id, val) {
return _data[id] = String(val);
},
getItem: function (id) {
return _data.hasOwnProperty(id) ? _data[id] : undefined;
},
removeItem: function (id) {
return delete _data[id];
},
clear: function () {
return _data = {};
}
}
} catch (e) {
console.error('localStorage pollyfill error: ', e);
}
}
})()

function Store() {
if (!(this instanceof Store)) {
Expand All @@ -40,7 +83,14 @@ function Store() {
Store.prototype = {
set: function (key, val) {
if (key && !isJSON(key)) {
storage.setItem(key, stringify(val));
try {
storage.setItem(key, stringify(val));
} catch (error) {
if (isQuotaExceeded(error)) {
// Storage full, maybe notify user or do some clean-up
console.warn('Storage full!');
}
}
} else if (key && isJSON(key) && !val) {
for (var a in key) this.set(a, key[a]);
}
Expand Down

0 comments on commit a0f7b21

Please sign in to comment.