0

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.

main.js

pageMod.PageMod({
    include: "https://na6.salesforce.com/500?fcf=00B8000000*",
    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); }
});

content-script.js

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... 

    </p>\
</div>");
japtain.cack
  • 131
  • 11

2 Answers2

2

You're only updating the variable, not anything that has been derived in the past from that variable, i.e. the HTML contents.

This has nothing to do with FF addon code, it's a general propery of how HTML DOM and javascript in general works.

the8472
  • 40,999
  • 5
  • 70
  • 122
  • So you're saying that the self.port.on is happening after the HTML has been appended; even though I have it before the jQuery function? That would explain everything... – japtain.cack Mar 04 '16 at 21:52
-1

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.

japtain.cack
  • 131
  • 11
  • Promises *do not* force synchronous behavior. They provide different semantics for asynchronous programming. The issue in your original code was that you were completely ignoring the asynchronous nature of inter-process messaging (messaging between background scripts and content scripts). Given this answer, I strongly recommend that you read the duplicate question, and [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Makyen Nov 27 '16 at 05:59