Jedimaster0 Jedimaster0 - 1 year ago 64
Javascript Question

Variable not updating in firefox content script when using self.port.on

I'm trying to update the version var and that's not working the way my mind thinks it should. For instance, when I do:

self.port.on("get-version", ver => { version = ver; alert(version); });

I get an alert with the version number, but the HTML is still 0.0.0, which I used to define the version variable at the beginning of the content script.


include: "*",
contentStyleFile: ["./css/salesforceoverlay.css"],
contentScriptFile: ["./external/jquery/jquery-1.11.3.min.js", "./js/salesforceoverlay.js"],
onAttach: function(worker) { worker.port.emit('get-version', self.version); }


var version = "0.0.0";
self.port.on("get-version", ver => { version = ver; alert(version); });

$("body").append("<div id='helpSection' class='overlay'>\
<p><b>Salesforce Overlay: " + version + "</b>\

//more paragraphs/text and such...


Answer Source

Javascript promises were the answer. I have since re-written this to the current web-extensions format so the code is gone, but the issue was with synchronous/asynchronous code. Javascript promises fixed my issue by forcing synchronous behaviour.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download