In sendMessage, what is tabs? Looks like it is undefined? Or some global? Is this perhaps copied from some example where tabs was being passed in as a parameter, but you don’t have that parameter?
I think I have taken it from this example: Beastify
So I see, I have skipped the main part of the code of the example:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {beastURL: chosenBeastURL});
});
`
Thank, you a lot Lithopsian, I was just going to give up learning webextensions. But now I got working code. It just needed the query method. I did not noticed, that the tabs is a argument of the callback, and I thought that it is the variable in Firefox.
So my working version is here:
chrome.commands.onCommand.addListener(
function(command) {
console.log("onCommand event received for message: ", command);
/*
Get all tabs using tabs.query()
& use callback to send message using
browser.tabs.sendMessage()
The query() calls callback to pass
the result to the function,
which is how asynchronous programming works.
*/
chrome.tabs.query(
{
active: true,
currentWindow: true
},
function(tabs) {
chrome.tabs.sendMessage(
tabs[0].id, // tab id integer
command // message
// , {} // options optional object
// , function(response) {...} // optional function
)
} // end of query() callback
); // end of query() call
} // end of onCommand() callback
);
The message
Recieved message:easy.js:9:3
easy
Is in the Console of Debuger in the window which contains the injected code. This is also interesting because I was expecting the output in incorrect console (in the Addon Debugger’s console).
When I debug it I god exception, but I see nothing under the message exception. I see only Exception:Object.
What does this mean? I see there was no message send because I am not able to receive it in event_handler.js background script (nor in content script where I have chrome.runtime.onMessage.addListener ). This is yet more weird because this part of code was already working well and I don’t remember that I would change something in this part sending messages. I try to find out why incorrect page is opened when I click on one of the popup items. The url in the commented part is correct. During this debuging I stuck on this problem with the exception.
There were some weird messages in console. Sounds like Toolbox is not able to view some property (access problem). This happens when I add breakpoint. When I remove breakpoints so the problem does not occur and the app works. Looks like bug in the Toolbox.