Im trying to send message to content.js
from background.js
when extension icon is clicked.
Background.js
:
chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.query({active : true, lastFocusedWindow : true}, function (tabs) {
var CurrTab = tabs[0];
chrome.tabs.sendMessage(CurrTab, 'run');
})
})
Content.js
:
chrome.runtime.onMessage.addListener(function(){
view();
})
I have this error in background.js
, i don't know why.
Error handling response: TypeError: Error in invocation of
tabs.sendMessage(integer tabId, any message, optional object options,
optional function responseCallback): No matching signature.
What im doing wrong?
In Background.js change the following :
chrome.tabs.sendMessage(CurrTab, 'run');
to
chrome.tabs.sendMessage(CurrTab.id, 'run');
As told by wOxxOm in the comments.
Secondly make sure that in manifest.json file you have specified the url of the website(where content script needs to be injected) in content_scripts/matches tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With