aklinker1 / webext-core

Collection of essential libraries and tools for building web extensions
https://webext-core.aklinker1.io
MIT License
116 stars 13 forks source link

`@webext-core/messaging` errors with `No response` when return value contains function #55

Open lionelhorn opened 8 months ago

lionelhorn commented 8 months ago

First, many thanks to the contributors ! Nice project and utils 👍

I'm using a content script in main world context to dynamically extract "react props" from the whole dom tree for use in another content script not running in main world context.

In console logs the following abreviation stands for MW: Main World CS: Content Script

Messenger caller is in a CS without MW context

Failing: with error thrown but error message doesn't explain why No response

Error message just says

chunk-ALTBYIMW.js:80 Uncaught (in promise) Error: No response
    at Object.<anonymous> (chunk-ALTBYIMW.js:80:92)
    at Generator.next (<anonymous>)
    at fulfilled (chunk-ALTBYIMW.js:37:19)

The issue is that the return value object contains a function.

Example simplified code :

mainWorldMessenger.onMessage("test", (data) => {
  const props = [
    {
      "className": "ml-md",
      onClick: () => {
      }
    }
  ]

  const ret = {reactProps: props};
  log("return value will be", ret)

  return ret
});
image

Working: messenger func returns with value

mainWorldMessenger.onMessage("test", (data) => {
  const props = [
    {
      "className": "ml-md",
    }
  ]

  const ret = {reactProps: props};
  log("return value will be", ret)

  return ret
});
image

Solution

Return value can be sanitized. An error message from @webext-core/messaging explaining why it doesn't return would be nice

aklinker1 commented 8 months ago

Interesting, I would have expected it to not error out when passing a function, but for the function to silently be removed from the response.

I'll see if I can figure out why a response isn't returned in this case and improve the error message.