Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created June 30, 2011 00:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamedmansour/1055333 to your computer and use it in GitHub Desktop.
Save mohamedmansour/1055333 to your computer and use it in GitHub Desktop.
Local Storage Message Passing Chrome Extension
<!doctype html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendRequest(tab.id, {method: "getLocalStorage"}, function(response) {
var myObjectRetrieved = JSON.parse(response.data);
console.log(myObjectRetrieved);
});
});
// Automatically inject all pages with the content script when the extension
// first installed by the user. Instead of refreshing the page all the time.
// From: http://stackoverflow.com/questions/2399389/chrome-extension-first-run/2401788#2401788
function onInstall() {
// Install the content script to all opened windows.
// From: https://github.com/mohamedmansour/reload-all-tabs-extension/blob/master/js/reload_controller.js#L96
chrome.windows.getAll({ populate: true }, function(windows) {
for (var w = 0; w < windows.length; w++) {
var tabs = windows[w].tabs;
for (var t = 0; t < tabs.length; t++) {
var tab = tabs[t];
if (tab.url.indexOf('http') == 0) { // Only inject in web pages.
chrome.tabs.executeScript(tab.id, { file: 'page.js' });
}
}
}
});
}
function getVersion() {
var details = chrome.app.getDetails();
return details.version;
}
var currVersion = getVersion();
var prevVersion = localStorage['version']
if (currVersion != prevVersion) {
// Check if we just installed this extension.
if (typeof prevVersion == 'undefined') {
onInstall();
}
localStorage['version'] = currVersion;
}
</script>
{
"name": "Local Storage Passing",
"version": "0.1",
"description": "Local Storage Passing",
"background_page": "background.html",
"permissions": [ "tabs", "http://*/*", "https://*/*" ],
"browser_action": {
"default_title": "Local Storage Passing",
"default_icon": "icon16.png"
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": ["page.js"],
"run_at": "document_end"
}]
}
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == 'getLocalStorage') {
var objectString = JSON.stringify(localStorage);
sendResponse({data: objectString});
} else {
sendResponse({}); // snub them.
}
});
@guimaraesrodrigues
Copy link

Still a valid implementation to access domains local storage?

@mohamedmansour
Copy link
Author

Still a valid implementation to access domains local storage?

I believe localStorage is shared, test it out I believe.

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