web-extension

Is there a callback for port.postMessage() in WebExtenstions


Is there a way to wait till the receiving party processes message sent using port.postMessage(message)? I'd expect something like callback passed as second argument similarly to chrome.scripting.executeScript(injection, callback)


Solution

  • To my knowledge not natively but a wrapper is easy to implement. You need a class that will generate a random UID (Unique ID) for each message and store a map { [uid]: cb }. Attach generated UID to the message. Response will also need to contain same UID.