Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created January 31, 2011 04:11
Show Gist options
  • Save mohamedmansour/803631 to your computer and use it in GitHub Desktop.
Save mohamedmansour/803631 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({});
}
});
@kav2k
Copy link

kav2k commented Jul 19, 2015

If anyone comes across this, chrome.extension.sendRequest is (long since) deprecated. You need to use chrome.runtime.sendMessage and chrome.runtime.onMessage. The signature is the same.

@white-collar
Copy link

Can I get access to any localStorage ? I need to extract data from site with my domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment