PlasmoHQ / plasmo

🧩 The Browser Extension Framework
https://www.plasmo.com
MIT License
10.14k stars 350 forks source link

Messaging: Fix async listeners #826

Closed mathieudutour closed 9 months ago

mathieudutour commented 9 months ago

Details

This PR fixes an issue where async listeners would not work:

CS:

listen(async (req, res) => {
  if (req.name === "getTabURL") {
    await timeout(1000)
    res.send(location.href)
    return
  } else {
    res.send("ERROR")
  }
})

BGSW:

await sendToContentScript({ name: "getTabURL" }) // will return undefined

See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage#sending_an_asynchronous_response_using_sendresponse

Code of Conduct