0

I need to inject an object named "smth" to window on pages with specific URLs with nsIDOMGlobalPropertyInitializer. Is there any way to implement this? It'll be ok if window.smth returns undefined on the other pages.

// currently
init: function(aWindow) {
    let self = this;
    let window = XPCNativeWrapper.unwrap(aWindow);

    if (window.location.href !== pageURL) {
        return;
    }

    return {
            // ...
    }
}

Now window.smth returns XPCOM wrapped nsISupports object on the other pages :(

Dmitrii Sorin
  • 3,855
  • 4
  • 31
  • 40

1 Answers1

1

I don't know how it may be possible if at all with that approach, but you can at least listen for the "content-document-global-created" notification: https://developer.mozilla.org/en-US/docs/Observer_Notifications#Documents and only inject the global

observe: function(subject, topic, data) {
    if (topic === 'content-document-global-created' && 
        subject instanceof Ci.nsIDOMWindow) {
        if (!subject.location.href.match(/http:\/\/example.com/)) {return;}
        XPCNativeWrapper.unwrap(subject).myGlobal = {};
    }
}
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
  • Yes, this could solve the problem, but i'm afraid there might be some troubles with memory consumption or some security problems. I mean that we can set which props can be readable and which can be writable using nsIDOMGlobalPropertyInitializer. And we can't do this using "content-document-global-created" XPCOM event (correct me if i'm not right). Anyway, thank you for the answer. – Dmitrii Sorin Oct 10 '12 at 08:17
  • Try the following in place of the corresponding line in my answer: `Object.defineProperty(XPCNativeWrapper.unwrap(subject), 'nonWritableGlobal', {writable:false, value:'myConstant'});` See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty for usage. Mozilla used to use `__defineGetter__` which you might also try if the above does not work, but the latter was deprecated and the former should work. – Brett Zamir Oct 10 '12 at 09:10