Skip to content

Bug-2017797 changes to structuredClone behavior#43539

Open
rebloor wants to merge 11 commits intomdn:mainfrom
rebloor:Bug-2017797-changes-to-structuredClone-behavior
Open

Bug-2017797 changes to structuredClone behavior#43539
rebloor wants to merge 11 commits intomdn:mainfrom
rebloor:Bug-2017797-changes-to-structuredClone-behavior

Conversation

@rebloor
Copy link
Contributor

@rebloor rebloor commented Mar 23, 2026

Description

Addresses the dev-docs-needed request Bug 2017797 Clipboard2File stopped working in Firefox Nightly.

@rebloor rebloor requested a review from Rob--W March 23, 2026 23:09
@rebloor rebloor self-assigned this Mar 23, 2026
@rebloor rebloor added the Content:WebExt WebExtensions docs label Mar 23, 2026
@github-actions github-actions bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Mar 23, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 23, 2026

Preview URLs (5 pages)

Flaws (6)

Note! 1 document with no flaws that don't need to be listed. 🎉

Found an unexpected or unresolvable flaw? Please report it here.

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts
Title: Content scripts
Flaw count: 2

  • macros:
    • Macro domxref produces link /en-US/docs/Web/API/globalThis which doesn't resolve
    • Macro domxref produces link /en-US/docs/Web/API/structuredClone which is a redirect

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts/cloneInto
Title: cloneInto()
Flaw count: 1

  • macros:
    • Macro domxref produces link /en-US/docs/Web/API/structuredClone which is a redirect

URL: /en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
Title: Share objects with page scripts
Flaw count: 2

  • macros:
    • Macro domxref produces link /en-US/docs/Web/API/structuredClone which is a redirect
    • Macro domxref produces link /en-US/docs/Web/API/structuredClone which is a redirect

URL: /en-US/docs/Mozilla/Firefox/Releases/149
Title: Firefox 149 release notes for developers (Stable)
Flaw count: 1

  • macros:
    • Macro domxref produces link /en-US/docs/Web/API/structuredClone which is a redirect

(comment last updated: 2026-03-24 23:07:32)

Copy link
Member

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firefox 149 release notes article should also mention the web facing change and the introduction of the separate window.structuredClone method in content scripts as called out in https://bugzilla.mozilla.org/show_bug.cgi?id=2017797#c39

In this PR I also added a TODO to expand on the documentation in the sharing with page article; if you anticipate that to need multiple rounds of reviews, please stick a placeholder there, so we can merge this PR ASAP and iterate on the article in a follow-up. I want the release notes updated because Firefox 149 is getting released today.

rebloor and others added 5 commits March 25, 2026 04:45
Co-authored-by: Rob Wu <rob@robwu.nl>
structuredClone section in sharing objects article
Grammar changes
@github-actions github-actions bot added the Content:Firefox Content in the Mozilla/Firefox subtree label Mar 24, 2026
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@rebloor rebloor marked this pull request as ready for review March 24, 2026 17:06
@rebloor rebloor requested review from a team as code owners March 24, 2026 17:06
@rebloor rebloor requested review from Rob--W, chrisdavidmills, dipikabh and willdurand and removed request for a team March 24, 2026 17:06
@willdurand willdurand removed their request for review March 24, 2026 19:05
Copy link
Member

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a solid basis for the content, thanks. I have offered various suggestions to improve the content. Please apply them, adjust the language if needed and request review again.

- [`exportFunction()`](#exportfunction): export a function to page scripts.
- [`cloneInto()`](#cloneinto): export an object to page scripts.
- constructors from the page context
- [`window.structuredClone()`](#structuredclone): an alternative to `cloneInto` in some cases.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my previous suggestion I formatted the link in a special way so that it is obvious that it is not a regular link to the API reference, because there is a clear difference between structuredClone and window.structuredClone.

Co-authored-by: Rob Wu <rob@robwu.nl>
@github-actions github-actions bot added size/m [PR only] 51-500 LoC changed and removed size/s [PR only] 6-50 LoC changed labels Mar 24, 2026
@rebloor rebloor requested a review from Rob--W March 24, 2026 22:15

> [!NOTE]
> In Firefox 148 and earlier, `window.structuredClone(value)` creates values in the scope of the caller instead of the window's scope. Use [`cloneInto()`](#cloneinto) if you want to support Firefox 148 and earlier.
>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole section should not be part of a quote (>). Please remove that.

(I am not adding a suggested edit because the three backticks in the end would be lost by github)

- Adds support for `tabId` as a top-level parameter in {{WebExtAPIRef("action.isEnabled")}} and {{WebExtAPIRef("browserAction.isEnabled")}}. This change provides for compatibility with the Chrome implementation of `action.isEnabled`. ([Firefox bug 2013477](https://bugzil.la/2013477))
- A user gesture is no longer required for {{WebExtAPIRef("action.openPopup")}} and {{WebExtAPIRef("browserAction.openPopup")}} to open a popup. This feature was available behind the `extensions.openPopupWithoutUserGesture.enabled` preference from Firefox 108. This change aligns Firefox's behavior with Chrome and Safari. ([Firefox bug 1799344](https://bugzil.la/1799344))
- If `windowId` is passed in {{WebExtAPIRef("action.openPopup")}} or {{WebExtAPIRef("browserAction.openPopup")}}, the window must be focused (active) for the popup to open. To open a popup in an unfocused window {{WebExtAPIRef("windows.update","windows.update(windowId, { focused: true })")}} must be called first. This change aligns Firefox behavior with Chrome. ([Firefox bug 2011516](https://bugzil.la/2011516))
- The implementation of {{domxref("structuredClone")}} has changed to instantiate objects in the `this` realm instead of the caller's realm. For backwards compatibility, the global scope of content scripts now have its own `structuredClone` method to continue allowing extensions to clone values in the content script's realm by default. This method shadows the `window.structuredClone` method, which remains available to allow extensions to clone values into the realm of the web page. For more information, see [`structuredClone` at Sharing objects with page scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have -> has (reviewing my own text and spotting typos - I am far from perfect!)

Suggested change
- The implementation of {{domxref("structuredClone")}} has changed to instantiate objects in the `this` realm instead of the caller's realm. For backwards compatibility, the global scope of content scripts now have its own `structuredClone` method to continue allowing extensions to clone values in the content script's realm by default. This method shadows the `window.structuredClone` method, which remains available to allow extensions to clone values into the realm of the web page. For more information, see [`structuredClone` at Sharing objects with page scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).
- The implementation of {{domxref("structuredClone")}} has changed to instantiate objects in the `this` realm instead of the caller's realm. For backwards compatibility, the global scope of content scripts now has its own `structuredClone` method to continue allowing extensions to clone values in the content script's realm by default. This method shadows the `window.structuredClone` method, which remains available to allow extensions to clone values into the realm of the web page. For more information, see [`structuredClone` at Sharing objects with page scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).

Copy link
Contributor Author

@rebloor rebloor Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The implementation of {{domxref("structuredClone")}} has changed to instantiate objects in the `this` realm instead of the caller's realm. For backwards compatibility, the global scope of content scripts now have its own `structuredClone` method to continue allowing extensions to clone values in the content script's realm by default. This method shadows the `window.structuredClone` method, which remains available to allow extensions to clone values into the realm of the web page. For more information, see [`structuredClone` at Sharing objects with page scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).
- The implementation of {{domxref("structuredClone")}} has changed to instantiate objects in the `this` realm instead of the caller's realm. For backwards compatibility, the global scope of content scripts now includes a `structuredClone` method, allowing extensions to continue cloning values in the content script's realm by default. This method shadows the `window.structuredClone` method, which remains available to allow extensions to clone values into the web page's realm. For more information, see [`structuredClone` in Sharing objects with page scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of us saw, forgot to save my own edit to fix that.

Co-authored-by: Rob Wu <rob@robwu.nl>
Comment on lines +173 to +174
> The cross-browser {{domxref("structuredClone")}} method can also be used to create structured clones.
>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally provided this suggestion so that there is a mention and link to structuredClone on this page. In the latest version there is already a mention somewhere else, so I'm now proposing to drop it to keep this NOTE simple.

Suggested change
> The cross-browser {{domxref("structuredClone")}} method can also be used to create structured clones.
>


- In Firefox, this behavior is called [Xray vision](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#xray_vision_in_firefox).
Content scripts may encounter JavaScript objects from its own global scope or Xray-wrapped versions from the web page.
A content script may encounter JavaScript objects from its global scope or Xray-wrapped versions from the web page. In regular web pages, {{domxref("globalThis")}} is identical to `window`, but in Firefox's content scripts, `globalThis` is a distinct object inheriting from `window`. This distinction often makes no practical difference for the availability of global APIs. The exception is when the global scope contains a definition of a standard API that shadows the definition in `window`, such as [`structuredClone` in content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

globalThis is rendered as a red non-existing link. Perhaps it should be jsxref instead of domxref? I'm not sure, please try it and otherwise find the right version of the syntax. It is supposed to point to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis

Suggested change
A content script may encounter JavaScript objects from its global scope or Xray-wrapped versions from the web page. In regular web pages, {{domxref("globalThis")}} is identical to `window`, but in Firefox's content scripts, `globalThis` is a distinct object inheriting from `window`. This distinction often makes no practical difference for the availability of global APIs. The exception is when the global scope contains a definition of a standard API that shadows the definition in `window`, such as [`structuredClone` in content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).
A content script may encounter JavaScript objects from its global scope or Xray-wrapped versions from the web page. In regular web pages, {{jsxref("globalThis")}} is identical to `window`, but in Firefox's content scripts, `globalThis` is a distinct object inheriting from `window`. This distinction often makes no practical difference for the availability of global APIs. The exception is when the global scope contains a definition of a standard API that shadows the definition in `window`, such as [`structuredClone` in content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#structuredclone).


The purpose of this feature is to make it harder for the less-privileged script to confuse the more-privileged script by redefining the native properties of objects.

So, for example, when a content script accesses the page's [window](/en-US/docs/Web/API/Window), it won't see any properties the page script added to the window, and if the page script has redefined any existing properties of the window, the content script will see the original version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross link the content script environment section because we now have additional information there about globalThis that is relevant. Not sure if this is the best way to document it, or if we should just add an extra sentence in the end.

Suggested change
So, for example, when a content script accesses the page's [window](/en-US/docs/Web/API/Window) from a [content script environment](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#content_script_environment), it won't see any properties the page script added to the window, and if the page script has redefined any existing properties of the window, the content script will see the original version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:Firefox Content in the Mozilla/Firefox subtree Content:WebAPI Web API docs Content:WebExt WebExtensions docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants