Skip to content

Commit

Permalink
Fake chrome.storage.sync for Firefox support #166
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonBarnabe committed Jan 30, 2016
1 parent d957cd1 commit 2c1987f
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
if (!options || !options.noSync) {
clearTimeout(syncTimeout);
syncTimeout = setTimeout(function() {
chrome.storage.sync.set({"settings": values});
getSync().set({"settings": values});
}, 0);
}
};
Expand All @@ -248,7 +248,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
me.set(key, defaults[key], {noBroadcast: true});
});

chrome.storage.sync.get("settings", function(result) {
getSync().get("settings", function(result) {
var synced = result.settings;
for (var key in defaults) {
if (synced && (key in synced)) {
Expand All @@ -273,7 +273,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
}
} else {
// user manually deleted our settings, we'll recreate them
chrome.storage.sync.set({"settings": values});
getSync().set({"settings": values});
}
}
});
Expand Down Expand Up @@ -393,3 +393,24 @@ function defineReadonlyProperty(obj, key, value) {
Object.freeze(copy);
Object.defineProperty(obj, key, {value: copy, configurable: true})
}

// Polyfill, can be removed when Firefox gets this - https://bugzilla.mozilla.org/show_bug.cgi?id=1220494
function getSync() {
if ("sync" in chrome.storage) {
return chrome.storage.sync;
}
crappyStorage = {};
return {
get: function(key, callback) {
callback(crappyStorage[key] || {});
},
set: function(source, callback) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
crappyStorage[property] = source[property];
}
}
callback();
}
}
}

2 comments on commit 2c1987f

@tophf
Copy link
Contributor

@tophf tophf commented on 2c1987f Jan 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait. So in Firefox the values won't be saved anywhere after the browser is closed?

@JasonBarnabe
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is just something to make it not error until Firefox adds support for it. https://bugzilla.mozilla.org/show_bug.cgi?id=1220494

Please sign in to comment.