-
-
Save akirattii/2f55bb320f414becbc42bbe56313a28b to your computer and use it in GitHub Desktop.
/***************************************************************** | |
* onMessage from the extension or tab (a content script) | |
*****************************************************************/ | |
chrome.runtime.onMessage.addListener( | |
function(request, sender, sendResponse) { | |
if (request.cmd == "any command") { | |
sendResponse({ result: "any response from background" }); | |
} else { | |
sendResponse({ result: "error", message: `Invalid 'cmd'` }); | |
} | |
// Note: Returning true is required here! | |
// ref: http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent | |
return true; | |
}); |
{ | |
"name": "example", | |
"version": "0.0.1", | |
"description": "Chrome Extension's message passing example", | |
"permissions": [ | |
"<all_urls>", | |
"webRequest", | |
"webRequestBlocking", | |
"background", | |
"activeTab" | |
], | |
"background": { | |
"scripts": ["background.js"] | |
// "persistent": false | |
}, | |
"content_scripts": [{ | |
//"matches": ["http://*/*"], | |
// "css": ["mystyles.css"], | |
"js": [ | |
"extlib/js/jquery.min.js", | |
"content.js" | |
] | |
}], | |
"browser_action": { | |
"default_title": "hoge", | |
"default_icon": "icon.png", | |
"default_popup": "popup.html" | |
}, | |
"manifest_version": 2, | |
"content_security_policy": "script-src 'self' https://ajax.googleapis.com https://maxcdn.bootstrapcdn.com/; object-src 'self'" | |
} |
// If you want to sendMessage from any popup or content script, | |
// use `chrome.runtime.sendMessage()`. | |
// Send message to background: | |
chrome.runtime.sendMessage(p, function(response) { | |
console.log(`message from background: ${JSON.stringify(response)}`); | |
}); | |
// If you want to sendMessage from tab of browser, | |
// use `chrome.tabs.sendMessage()`. | |
// Send message from active tab to background: | |
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { | |
chrome.tabs.sendMessage(tabs[0].id, p, function(response) { | |
console.log(`message from background: ${JSON.stringify(response)}`); | |
}); | |
}); |
@hlawuleka - The console log is the response from the background script. The message that is being set is contained in the "p" object. Then the callback includes the response from the listener in the background script.
It would be more clear if it said, for example:
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: 'Hello Background! How are you?'}, function(response) {
console.log('message from background:', response);
// Log: {message: 'Hello Tab!, I am doing swell!'}
});
});
Maybe you know answer, when I send message from popup.js and background is inactive, then onMessage listener in background.js not fired. But when I send message from content-script.js, onMessage fired even if background is inactive mode.
Hello ! The Code is awesome. But the thing which I am facing is, the extension doesn't send message when we install the extension and try to run on previous opened web pages. we have to reload the web pages then extension passes the message.
Thanks !!
Thank you so much. Sometimes that really hard to find something in documentation. Thank you!
uploaded the unpacked plugin as it is and nothing showed in my console
Version 90.0.4430.85 (Official Build) (64-bit)
Hi,
If I want to send message to a window that created by runtime.windows.create from background.js, is there any way to do that?
thank you. This really helped.
When you say
"// Send message from active tab to background: "
Then go ahead and say
console.log(
message from background: ${JSON.stringify(response)});
shouldn't your comment then say"Send message from background to active tab"
?