Created
June 30, 2011 00:14
-
-
Save mohamedmansour/1055333 to your computer and use it in GitHub Desktop.
Local Storage Message Passing Chrome Extension
This file contains 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
<!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> |
This file contains 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
{ | |
"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" | |
}] | |
} |
This file contains 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
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { | |
if (request.method == 'getLocalStorage') { | |
var objectString = JSON.stringify(localStorage); | |
sendResponse({data: objectString}); | |
} else { | |
sendResponse({}); // snub them. | |
} | |
}); |
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
Still a valid implementation to access domains local storage?