const clonedObject = cloneInto(myObject, targetWindow);cloneInto() / notebook
cloneInto()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
targetWindow.foo = clonedObject;// content script
const addonScriptObject = { greeting: "hello from your extension" };
window.addonScriptObject = cloneInto(addonScriptObject, window);// page script
button.addEventListener("click", () => {
console.log(window.addonScriptObject.greeting); // "hello from your extension"
});// Content script
window.foo.addonScriptObject = cloneInto(addonScriptObject, window);// page script
function foo(greeting) {
console.log(`they said: ${greeting.message}`);
}// content script
const addonScriptObject = { message: "hello from your extension" };
window.foo(cloneInto(addonScriptObject, window)); // "they said: hello from your extension"// content script
const addonScriptObject = {
greetMe() {
alert("hello from your extension");
},
};
window.addonScriptObject = cloneInto(addonScriptObject, window, {
cloneFunctions: true,
});// page script
const test = document.getElementById("test");
test.addEventListener("click", () => {
window.addonScriptObject.greetMe();
});// content script
const addonScriptObject = {
body: window.document.body,
};
window.addonScriptObject = cloneInto(addonScriptObject, window, {
wrapReflectors: true,
});// page script
const test = document.getElementById("test");
test.addEventListener("click", () => {
console.log(window.addonScriptObject.body.innerHTML);
});