Skip to content

Instantly share code, notes, and snippets.

@MikeeI
Forked from mohamedmansour/gist:803631
Created March 21, 2016 11:20
Show Gist options
  • Save MikeeI/8da8191be204b53d6cb9 to your computer and use it in GitHub Desktop.
Save MikeeI/8da8191be204b53d6cb9 to your computer and use it in GitHub Desktop.
Get and Set localStorage from Content Script
// Content Script to save data.
chrome.extension.sendRequest({storage: 'foo', value: 'bar'});
// Content Script to get data.
chrome.extension.sendRequest({storage: 'foo'}, function(response) {
console.log('foo => ' + response.storage);
});
// Background Page
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.storage) {
if (typeof request.value != 'undefined') {
localStorage[request.storage] = request.value;
}
sendResponse({storage: localStorage[request.storage]});
} else {
sendResponse({});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment