Firefox CloneInto does not copy function from add-in script to script page
I created a Greasemonkey script that contains an object that I would like to make available from the page's internal scripts.
There are several safe ways to do this , described on this page .
One is to use the Components.utils.cloneInto function .
Here's an example script.
// ==UserScript==
// @name Test CloneInto
// @namespace Test CloneInto
// @description Test CloneInto
// @include http://stackoverflow.com*
// @version 1
// @grant GM_xmlhttpRequest
// ==/UserScript==
var myObj = {}
myObj.test = function() {
alert("works");
}
myObj.num = 152;
unsafeWindow.myObj = cloneInto(myObj, unsafeWindow, {cloneFunctions: true});
var scriptDOM = document.createElement("script");
scriptDOM.type = "text/javascript";
scriptDOM.innerHTML = "alert(window.myObj.num);\
alert(window.myObj.test);"
document.getElementsByTagName("head")[0].appendChild(scriptDOM);
"152" displays correctly, but "undefined" appears instead of my function.
However, I used {cloneFunctions: true}
as suggested in the documentation.
I am using Firefox 34, has any idea how to fix this?
Edit: It works fine with Google Chrome and Tampermonkey.
Edit 2: Opened issue 2070 in the Greasemonkey repository .
source to share
There are several safe ways to do this, described on this page.
This is a description of modifying scripting content for the Add-on SDK.
The change is not related to Greasemonkey or user scripts, Addon-sdk content scripts are something other than Greasemonkey user scripts.
For Greasemonkey, I think this still works:
unsafeWindow.myObj = myObj
source to share