-
-
Save MikeeI/8da8191be204b53d6cb9 to your computer and use it in GitHub Desktop.
Get and Set localStorage from Content Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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