For more information on asynchronous code in JavaScript, see below:
. ?
contentHtml = '';. , console.log(contentHtml);, .
contentHtml?
, , contentHtml .
, contentHtml. , Firefox. .
JavaScript , , . , , , , , .
, , , :
var tabs = require("sdk/tabs");
var contentHtml = '';
function workerAttachedToTabWhenTabActivated() {
self.port.emit("html", document.body.innerHTML);
}
function receiveHtmlMessageFromWorkerViaPortOn(message) {
contentHtml = message;
console.log("contentHtml after receiving html message:" + contentHtml);
}
tabs.on('activate', function(tab) {
var worker = tab.attach({
contentScript: 'workerAttachedToTabWhenTabActivated();'
});
worker.port.on("html", receiveHtmlMessageFromWorkerViaPortOn(message))
});
console.log("contentHtml after setting up tab.attach:" + contentHtml);
, contentHtml message ,
console.log("contentHtml after setting up tab.attach:" + contentHtml);
contentHtml message. contentHtml message , , , , html message, .
, , html message, , .