Skip to content

Commit 5bb6653

Browse files
committed
fix: fix app crash when accessing localStorage is blocked. (#41,#8)
1 parent 3b336fa commit 5bb6653

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"bannerjs": "2.0.1",
5454
"colors-cli": "^1.0.29",
5555
"rollup": "^3.17.3",
56-
"tsbb": "^3.7.9",
56+
"tsbb": "^4.0.5",
5757
"zlib": "1.0.5"
5858
},
5959
"dependencies": {}

src/main.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var storage = window.localStorage;
2-
31
function isJSON(obj) {
42
obj = JSON.stringify(obj);
53
if (!/^\{[\s\S]*\}$/.test(obj)) {
@@ -20,21 +18,33 @@ function isFunction(value) { return ({}).toString.call(value) === "[object Funct
2018
function isArray(value) { return Object.prototype.toString.call(value) === "[object Array]"; }
2119
// https://github.com/jaywcjlove/store.js/pull/8
2220
// Error: QuotaExceededError
23-
function dealIncognito(storage) {
21+
function dealIncognito(storage = window.localStorage) {
2422
var _KEY = '_Is_Incognit', _VALUE = 'yes';
25-
try { storage.setItem(_KEY, _VALUE) }
26-
catch (e) {
27-
if (e.name === 'QuotaExceededError') {
28-
var _nothing = function () { };
29-
storage.__proto__ = { setItem: _nothing, getItem: _nothing, removeItem: _nothing, clear: _nothing };
23+
try {
24+
storage.setItem(_KEY, _VALUE);
25+
storage.removeItem(_KEY);
26+
} catch (e) {
27+
Storage.prototype._data = {};
28+
Storage.prototype.setItem = function (id, val) {
29+
return this._data[id] = String(val);
30+
};
31+
Storage.prototype.getItem = function (id) {
32+
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
33+
};
34+
Storage.prototype.removeItem = function (id) {
35+
return delete this._data[id];
36+
};
37+
Storage.prototype.clear = function () {
38+
return this._data = {};
3039
}
40+
storage = Storage;
3141
}
3242
finally { if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); }
3343
return storage;
3444
}
3545

3646
// deal QuotaExceededError if user use incognito mode in browser
37-
storage = dealIncognito(storage);
47+
const storage = dealIncognito();
3848

3949
function Store() {
4050
if (!(this instanceof Store)) {

0 commit comments

Comments
 (0)