I want to be able to store data on background (on my extension) so I can access this data between multiple domains.
Where's what I'm doing:
content-script.js
function setItem(name, data) {
chrome.extension.sendMessage({ command: 'setItem', name: name, data: data });
}
function getItem(name) {
chrome.extension.sendMessage({ command: 'getItem', name: name }, function(response) {
return response;
});
}
background-script.js
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.command) {
case 'setItem':
localStorage.setObject(request.name, request.data);
return;
case 'getItem':
sendResponse(localStorage.getObject(request.name));
return;
}
});
But without sucess, since I cant return from inside the callback on getItem.
I do get the data inside the function(response) { }
callback, I just can't return it as the return of getItem.
How should I do this?