Skip to content

Commit

Permalink
fix #339 by normalizing localStorage and browser.storage.local usage
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed Dec 24, 2016
1 parent 280a64d commit fe77c4b
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,42 @@ exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = 'undefined' != typeof chrome
&& 'undefined' != typeof chrome.storage
? chrome.storage.local
: localstorage();
exports.storage = (function() {
var browser = window.browser || window.chrome;

if (typeof browser !== 'undefined' &&
typeof browser.storage !== 'undefined' &&
typeof browser.storage.local !== 'undefined') {
if (browser.storage.local.get.length === 2) {
// Chrome/Opera/Edge use the callback pattern
return browser.storage.local;
} else if (browser.storage.local.get.length === 1) {
// Firefox uses the Promise pattern
return {
get: function(key, cb) {
browser.storage.local.get(key).then(cb);
},
set: browser.storage.local.set,
remove: browser.storage.local.remove
};
}
}

if (window.localStorage) {
return {
get: function(key, cb) {
var value = window.localStorage.getItem(key);
cb(value);
},
set: function(key, value) {
window.localStorage.setItem(key, value);
},
remove: function(key) {
window.localStorage.removeItem(key);
}
};
}
})();

/**
* Colors.
Expand Down Expand Up @@ -131,13 +163,11 @@ function log() {
*/

function save(namespaces) {
try {
if (null == namespaces) {
exports.storage.removeItem('debug');
} else {
exports.storage.debug = namespaces;
}
} catch(e) {}
if (null == namespaces) {
exports.storage.remove('debug');
} else {
exports.storage.set('debug', namespaces);
}
}

/**
Expand All @@ -148,38 +178,22 @@ function save(namespaces) {
*/

function load() {
try {
return exports.storage.debug;
} catch(e) {}

// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
if (typeof process !== 'undefined' && 'env' in process) {
return process.env.DEBUG;
if (exports.storage) {
exports.storage.get('debug', exports.enable);
} else {
if (typeof process !== 'undefined' &&
typeof process.env !== 'undefined' &&
typeof process.env.DEBUG !== 'undefined') {
exports.enable(process.env.DEBUG);
}
}
}

/**
* Enable namespaces listed in `localStorage.debug` initially.
* Enable namespaces listed in the storage `debug`` initially.
*/

exports.enable(load());

/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*
* @return {LocalStorage}
* @api private
*/

function localstorage() {
try {
return window.localStorage;
} catch (e) {}
}
exports.load();

/** Attach to Window*/
if (window) {
Expand Down

0 comments on commit fe77c4b

Please sign in to comment.