Sharing add-on objects (content scripts) with web content (pages) in Firefox

I spent several days trying to share one of my Firefox for Android extension objects with web pages, which I also open from my extension (declared as resources). The fact is that I read a lot about the latest changes about unsafewindow, so I tried a very small example with new features, but didn’t do the job. I copied the examples, and I also tried my own, but there is no way to copy existing objects with functionality. Look, I have a VERY large object for cloning in the content window, but I decided to test with a small one:

//From addon
var dog = {
    name: 'Spike',
    woof: function(){alert('woof woof!')}
};

And after that I tried to copy this object to the active window:

//From addon
var contentWindow = window.BrowserApp.selectedBrowser.contentWindow;
contentWindow.dog = Components.utils.cloneInto(
    dog, 
    contentWindow, 
    {cloneFunctions: true}
);

, :

alert(contentWindow.dog); //Shows: [object Object]
alert(contentWindow.dog.name); //Shows: Spike
alert(contentWindow.dog.woof); //Shows: undefined

, , , "cloneFunctions: true".

, ( ) :

function greetme(user) {
    return "cheers " + user;
}
var foo = Components.utils.createObjectIn(contentWindow,{defineAs: "foo"});
Components.utils.exportFunction(greetme, foo, {defineAs: "greetme"}); 
//foo is not an object in current window

... , , , .

(A LOT) !

https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/

+4
1

- , XRay. , (-) dog, XRay .

Firefox Android Nightly (, Firefox ).

( WebIDE):

var dog = {
  name: 'Spike',
  woof: function () {
    alert(contentWindow.document.title + "\n" + this.name + ': woof woof!');
  }
};

var contentWindow = BrowserApp.selectedBrowser.contentWindow;

// Need to unwrap this, so that we can actually set properties on the
// object itself and not just the wrapper. Aka. make "dog" visible to
// the actual script.
var unsafeWindow = Components.utils.waiveXrays(contentWindow);

// Define Window.dog (on the unsafe window, so that the website code
// can actually see it).
unsafeWindow.dog = Components.utils.cloneInto(dog, contentWindow, {
  cloneFunctions: true
});

:

dog.woof();

.

+2

All Articles