-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation of component.utils exportFunction and cloneInto #30877
Merged
+374
−3
Merged
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6b440cc
Add documentation of component.utils exportFunction and cloneInto
rebloor 48c8a78
Resolved broken links, various edits
rebloor 50aec8a
various edits
rebloor aa20e9f
Various edits
rebloor 1fafc90
Merge branch 'main' into components.utils
rebloor 11031e7
Merge branch 'main' into components.utils
rebloor c666642
Merge branch 'main' into components.utils
rebloor 17c18b8
Remove incorrect "based on" note
rebloor 2c29e95
move exportFunction and cloneInto under Content_scripts
rebloor 7b321fa
Cross-reference and slug corrections
rebloor 99b8384
Merge branch 'main' into components.utils
rebloor 44465a5
Apply suggestions from review
rebloor 32258a6
Further updates from feedback
rebloor e4b00ee
casing fix
rebloor eab357c
Merge branch 'main' into components.utils
rebloor 3c7fbc5
Apply suggestions from review
rebloor c69abf5
Remove spurious ad//
rebloor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
files/en-us/mozilla/add-ons/webextensions/api/components/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: components | ||
slug: Mozilla/Add-ons/WebExtensions/API/components | ||
page-type: webextension-api | ||
browser-compat: webextensions.api.components | ||
--- | ||
|
||
{{AddonSidebar}} | ||
|
||
Utilities that enable an extension to [share objects with page scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts). | ||
|
||
## Properties | ||
|
||
- {{WebExtAPIRef("components.utils")}} | ||
- : Provides functions to export functions and create structured clones of objects defined in a privileged scope in a less-privileged scope. | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
> **Note:** This API is based on Chromium's [`chrome.devtools`](https://developer.chrome.com/docs/extensions/mv2/devtools/) API. | ||
|
||
<!-- | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are | ||
// met: | ||
// | ||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following disclaimer | ||
// in the documentation and/or other materials provided with the | ||
// distribution. | ||
// * Neither the name of Google Inc. nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
--> |
160 changes: 160 additions & 0 deletions
160
files/en-us/mozilla/add-ons/webextensions/api/components/utils/cloneinto/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
--- | ||
title: components.utils.cloneInto() | ||
slug: Mozilla/Add-ons/WebExtensions/API/components/utils/cloneInto | ||
page-type: webextension-api-function | ||
browser-compat: webextensions.api.components.utils.cloneInto | ||
--- | ||
|
||
{{AddonSidebar()}} | ||
|
||
This function provides a safe way to take an object defined in a privileged scope and create a [structured clone](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) of it in a less-privileged scope. It returns a reference to the clone: | ||
|
||
```js | ||
var clonedObject = cloneInto(myObject, targetWindow); | ||
``` | ||
|
||
You can then assign the clone to an object in the target scope as an expando property, and scripts running in that scope can access it: | ||
|
||
```js | ||
targetWindow.foo = clonedObject; | ||
``` | ||
|
||
This enables privileged code, such as an extension, to share an object with less-privileged code, such as a web page script. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
let clonedObject = Components.utils.cloneInto( | ||
obj, // object | ||
targetScope, // object | ||
options // object | ||
); | ||
``` | ||
|
||
### Parameters | ||
|
||
- `obj` | ||
- : `object`. The object to clone. | ||
- `targetScope` | ||
- : `object`. The object to attach the object to. | ||
- `options` {{optional_inline}} | ||
- : `object`. Options for the function. | ||
- `cloneFunctions` {{optional_inline}} | ||
- : `Boolean`. Whether the object's functions should be cloned. Default to `false`. Cloned functions have the same semantics as functions exported using [`Components.utils.exportFunction`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/components/utils/exportFunction). See [Cloning objects that have functions](#Cloning_objects_that_have_functions). {{optional_inline}} | ||
- `wrapReflectors` {{optional_inline}} | ||
- : `boolean`. Whether objects reflected from C++, such as DOM objects, should be cloned. Defaults to `false`. See [Cloning objects that contain DOM elements](#Cloning_objects_that_contain_DOM_elements). | ||
|
||
### Return Value | ||
|
||
A reference to the cloned object. | ||
|
||
## Examples | ||
|
||
This add-on script creates an object, clones it into the content window and makes it a property of the content window global: | ||
|
||
```js | ||
// add-on script | ||
var addonScriptObject = { greeting: "hello from add-on" }; | ||
contentWindow.addonScriptObject = cloneInto(addonScriptObject, contentWindow); | ||
``` | ||
|
||
Scripts running in the page can access the object: | ||
|
||
```js | ||
// page script | ||
button.addEventListener( | ||
"click", | ||
function () { | ||
console.log(window.addonScriptObject.greeting); // "hello from add-on" | ||
}, | ||
false, | ||
); | ||
``` | ||
|
||
Of course, you don't have to assign the clone to the window itself; you can assign it to some other object in the target scope: | ||
|
||
```js | ||
contentWindow.foo.addonScriptObject = cloneInto( | ||
addonScriptObject, | ||
contentWindow, | ||
); | ||
``` | ||
|
||
You can also pass it into a function defined in the page script. Suppose the page script defines a function like this: | ||
|
||
```js | ||
// page script | ||
function foo(greeting) { | ||
console.log("they said: " + greeting.message); | ||
} | ||
``` | ||
|
||
The add-on script can define an object, clone it, and pass it into this function: | ||
|
||
```js | ||
// add-on script | ||
var addonScriptObject = { message: "hello from add-on" }; | ||
contentWindow.foo(cloneInto(addonScriptObject, contentWindow)); // "they said: hello from add-on" | ||
``` | ||
|
||
### Cloning objects that have functions | ||
|
||
If the object to clone contains functions, you must pass the `{cloneFunctions:true}` flag, or you get an error. If you do pass this flag, then functions in the object are cloned using the same mechanism used in [`Components.utils.exportFunction`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/components/utils/exportFunction): | ||
|
||
```js | ||
// add-on script | ||
var addonScriptObject = { | ||
greetme: function () { | ||
alert("hello from add-on"); | ||
}, | ||
}; | ||
contentWindow.addonScriptObject = cloneInto(addonScriptObject, contentWindow, { | ||
cloneFunctions: true, | ||
}); | ||
``` | ||
|
||
```js | ||
// page script | ||
var test = document.getElementById("test"); | ||
test.addEventListener( | ||
"click", | ||
function () { | ||
window.addonScriptObject.greetme(); | ||
}, | ||
false, | ||
); | ||
``` | ||
|
||
### Cloning objects that contain DOM elements | ||
|
||
By default, if the object you clone contains objects reflected from C++, such as DOM elements, the cloning operation fails with an error. If you pass the `{wrapReflectors:true}` flag, then the object you clone contains these objects: | ||
|
||
```js | ||
// add-on script | ||
var addonScriptObject = { | ||
body: contentWindow.document.body, | ||
}; | ||
contentWindow.addonScriptObject = cloneInto(addonScriptObject, contentWindow, { | ||
wrapReflectors: true, | ||
}); | ||
``` | ||
|
||
```js | ||
// page script | ||
var test = document.getElementById("test"); | ||
test.addEventListener( | ||
"click", | ||
function () { | ||
console.log(window.addonScriptObject.body.innerHTML); | ||
}, | ||
false, | ||
); | ||
``` | ||
|
||
Access to these objects in the target scope is subject to the normal [script security checks](https://firefox-source-docs.mozilla.org/dom/scriptSecurity/index.html). | ||
|
||
{{WebExtExamples}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a mistake? This API is Firefox-only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rob--W Indeed - I'll remove it.