Sending function through message fails, Is it another bug?

In my addon I am trying to send namespace using sendMessage method. I am passing the complete namespace which includes methods.

I have noticed, I can recieve any Object which is in the namespace, but there are no methods, which were defined during load of background.js.

The background script is loaded, the namespace was created, all functions were defined and are part of the namespace. Later when I click on addon’s button, I open new page, where I send a message to get namespace. In the response which is processed by callback function I found the namespace, but this is missing any functions which are part of the namespace.

I wonder if this is another bug in Debugger or this was a intent of the developers? These bugs are quite depressing.

I’m pretty sure that the object is passed using a structured clone. The actual object is likely to be in a different process, so the receiver will get a different object which is a copy. The structured clone algorithm does not copy functions. If a function were to be copied from the sender to the caller, it would likely not do what you expect because it would be in an entirely different context and a different process, so it couldn’t possibly run. That is why a separate response function is provided, which is not copied and will be executed in the context of the sender.

1 Like

Which means, that I need to define the page functions on the side of page script, not on the side of background script.

On the begin I planned to use same namespace, but in Firefox it’s impossible. So the background namespace will use same name as the page script namespace but I have to update data in page, when every change is done on background namespace.

Thanks.

Yes, send data, not code. You can have the same function names if you want, but it might be better to make the separation more explicit. I suppose with care, you could even use the same code, but you would have to explicitly include it in both contexts. Just remember the two contexts are in entirely different system processes and you just have a nice simple way to communicate between them, but obviously not to execute code from one in the other.