4

Is there any way to make a synchronous communication between the content script and the main addon script?

If i make a method like this for then the method returns immediately. So is there any way to wait for the main script to respond and then process the result?

main.js

 worker.port.on("GetValue"),function(key)
 {
     worker.port.emit('GetValue',ss.storage[key]);
 }

content script

//get value from local storage

function GetValueFromLocalStorage(key) 
{
    self.port.emit("GetValue", key);

    self.port.on("GetValue", function (value) 
        { 
                return value;

        }); 
}

It would be beneficial if this was possible because async code is not clean and organized and a nightmare to write especially if i have to access this method more than once.

Pankaj Kumar
  • 1,748
  • 6
  • 28
  • 41

1 Answers1

2

No, the Add-on SDK has been designed to allow asynchronous communication only. The original idea was that eventually SDK-based extensions would run in a separate process whereas content scripts would have to run in the process of the web page. Not sure whether this plan is still being pursued but it is probably the main reason for the asynchronous API - otherwise changing to multi-process would be impossible without breaking all extensions.

Obviously, you could use low-level APIs and especially chrome authority to access web pages directly and synchronously. But then you are leaving the well-documented and supported terrain, maybe not the best idea if code readability is the only reason you are looking into this.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126