Components.utils.cloneInto deprecation?

Might be related to Send message to extension from a web page

In the document from Web Extension API Custom Event section, there is a description about Firing from privileged code to non-privileged code, which describe a snippet that constructs an CustomEvent object and send it to injected page script from extension content script.

// doc is a reference to the content document
function dispatchCustomEvent(doc) {
  var eventDetail = Components.utils.cloneInto({foo: 'bar'}, doc.defaultView);
  var myEvent = doc.defaultView.CustomEvent("mytype", eventDetail);
  doc.dispatchEvent(myEvent);
}

But when I run the code above, the latest Firefox says the Components object will be deprecated, and there’s also no utils object anymore.

Is there any migration guide or API alternative for this purpose? Sending custom event from content script to injected page script (or in reverse direction).

Thanks in advance.

This is not at all deprecated, see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Sharing_objects_with_page_scripts - you just remove Components.utils.

The Firefox unspecific way would be https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Communicating_with_the_web_page

I’ve solved the problem, it’s simply because I forgot to check cloneInto function existence (same code is shared both logic in content script and page script). :joy: