public
anonymous / background.html
Created

Message Passing attempt

  • Download Gist
background.html
HTML
1 2 3 4 5 6 7
<script>
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
</script>
content_script.js
JavaScript
1 2 3 4 5 6 7 8 9 10
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
else
sendResponse({}); // snub them.
});
manifest.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{
"name": "Test",
"version": "1.0",
"background": "background.html",
"permissions": [
"tabs", "http://*/*"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["content_script.js"]
}
]
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.