Closed
Bug 1509839
Opened 6 years ago
Closed 6 years ago
chrome.runtime.sendMessage send undefined value
Categories
(WebExtensions :: General, defect)
Tracking
(Not tracked)
RESOLVED
INVALID
People
(Reporter: nn1436401, Unassigned)
Details
(Keywords: dev-doc-complete)
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0
Steps to reproduce:
In content script of extension write the following code:
chrome.runtime.sendMessage({a:1, b:undefined})
In background script listen to message and print
chrome.runtime.onMessage.addListener(console.log)
Actual results:
Output is:
{a:1, b:undefined}
Firefox unlike Chrome passes undefined value between content script and background script.
Expected results:
The object should be JSON-ified thus removing all undefined values.
Expecting output:
{a:1}
Updated•6 years ago
|
Component: JavaScript Engine → General
Product: Core → WebExtensions
Comment 1•6 years ago
|
||
This is the expected behavior.
Status: UNCONFIRMED → RESOLVED
Closed: 6 years ago
Resolution: --- → INVALID
Comment 2•6 years ago
|
||
Does <https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage#Parameters> need to be updated? It currently describes that the "message" parameter is an object that can be serialized to JSON, which can be read that the message is actually serialized to JSON. And under JSON serialization, |undefined| properties are omitted by default.
Comment 3•6 years ago
|
||
Yes,that is inaccurate. sendMessage uses structured clone serialization, not JSON serialization.
Keywords: dev-doc-needed
Any reason for this behavior ?
This breaks compatibility with other browsers supporting WebExtensions.
Comment 5•6 years ago
|
||
I can't imagine that it breaks compatibility with other browsers in any meaningful way. In any case, we've never aimed to be bug-for-bug compatible, especially not when it would mean a difficult-to-implement deviation from the structured clone specification for no obvious gain.
My code sends an object where several values are not initialized.
In Edge and Chrome I didn't see any problem since they threw away keys having undefined values.
But in FF it sends the object with undefined values and it broke my code in background script which didn't expect undefined in any value.
Comment 7•6 years ago
|
||
Updated page to say structured clone serialized, not JSON serialized. Better?
Keywords: dev-doc-needed → dev-doc-complete
You need to log in
before you can comment on or make changes to this bug.
Description
•