I am following an example from Mozilla Docs
tabs.on('activate', function(tab) {
tab.attach({
contentScript: 'self.postMessage(document.body.innerHTML);',
onMessage: function (message) {
console.log(message);
}
});
});
with a little modification like this.
var bodyHTML;
tabs.on('activate', function(tab) {
tab.attach({
contentScript: 'self.postMessage(document.body.innerHTML);',
onMessage: function (message) {
bodyHTML = message;
console.log("From attach : " + bodyHTML);
}
});
});
console.log("After tab : " + bodyHTML);
Now when I execute this addon, console.log("After tab : " + bodyHTML);
is getting executed first and then console.log("From attach : " + bodyHTML);
. What could be the issue and how can I order the sequence of execution?
The tabs.on()
call sets up an event. It doesn't directly cause anything else to happen. The function in the onMessage
property is triggered by the message which is posted by the content script, which itself is triggered by the activation of a tab (typically the visiting of a web page). The console.log()
is executed right after the tabs.on()
call just above it, most likely before any tabs are attached.