Synchronous communication between Script Content and Script Main Addon - FireFox addon sdk

Is there a way to make synchronous communication between the contents of the script and the main script addon?

If I create such a method, the method returns immediately. So, is there a 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 useful if it were possible because the asynchronous code was not clean and organized, and writing a nightmare especially if I need to access this method more than once.

+4
source share
1 answer

No, the Add-on SDK was designed to allow asynchronous communication. The initial idea was that, ultimately, SDK-based extensions would be executed in a separate process, while content scripts would need to be run in the process of the web page. I'm not sure that this plan is still being pursued, but this is probably the main reason for the asynchronous API - otherwise switching to a multiprocess would be impossible without breaking all the extensions.

Obviously, you can use the low-level APIs and especially the Chrome permissions to access web pages directly and synchronously. But then you leave the well-documented and maintained landscape, maybe not a good idea if code readability is the only reason you look at it.

+2
source

Source: https://habr.com/ru/post/1434822/


All Articles