I'm trying to use custom DOM events to communicate between the main world and the isolated world for a page in a webextension in Firefox. When trying to access an event object property, I get an access denied error. Most of what follows is just context. I really want to know what a "restricted" object is rather than how to make it unrestricted.
listener
function onCustom( e ) {
console.log( e );
console.log( e.detail );
console.log( e.detail.plugh );
}
document.addEventListener( 'custom', onCustom );
sender
e = new CustomEvent( 'custom', { detail: {
plugh: 'xyzzy'
} } );
document.dispatchEvent( e );
If the listener and the sender are both in the same world, it works fine. (This case is just a sanity check, since the point is to communicate between worlds.)
If the listener is in the isolated world and the sender is in the main world, it works fine.
If the listener is in the main world and the sender is in the isolated world, it pukes as follows.
console output
custom { blah, blah, blah, lots of properties }
Object { plugh: "xyzzy" }
Uncaught Error: Permission denied to access property "plugh"
That's all the error information that there is: an untyped error and a message string.
I can expand the Object (second line) in the devtools console. It's there.
If I expand the "custom" object (first line) in the devtools console, it says (in part):
detail: Restricted { }
The detail property of a CustomEvent can be anything serializable. I can do my own serialization with JSON.stringify() in the sender and deserialize it with JSON.parse() in the listener. It's a workaround that does work. I still want to know what's going on with the object that I cannot access it after it was serialized and deserialized transparently for me.
EDIT (from the comments): The documented way to send objects from the isolated world into the main world is cloneInto().
sender (isolated to main only)
e = new CustomEvent( 'custom', cloneInto( { detail: {
plugh: 'xyzzy'
} }, wrappedJSObject ) );
document.dispatchEvent( e );
cloneIntofunction as explained in Firefox's documentation.