How to manipulate window prototype in a <browser> element?

本秂侑毒 提交于 2019-12-10 12:08:38

问题


What I'm doing right now is:

When <browser src="..." /> loads, I attach data into its .contentWindow:

frame.addEventListener("load",function(){
    this.contentWindow.someMethod = function(){};
},true);

Now I want to know if there is a way to do this earlier, into the <browser>'s window prototype, or any Window prototype, as for example I can do in the "current" window:

// [W]indow is the constructor
Window.prototype.test = function(){ alert("hello"); };
// [w]indow is the instance
window.test();

回答1:


There are currently two ways to inject properties into a window before any JavaScript code runs. Usually, content-document-global-created notification is simpler. The other is implementing nsIDOMGlobalPropertyInitializer interface. Both allow you to get notified when a new window loads and before that window runs JavaScript code.

Here is the approximate code for doing it with the observer notification:

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

var myObserver =
{
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),

  observe: function(subject, topic, data)
  {
    if (topic == "content-document-global-created" &&
        subject instanceof Ci.nsIDOMWindow &&
        subject.location.hostname == "example.com")
    {
      XPCNativeWrapper.unwrap(subject).someMethod = function() {};
    }
  }
};

var observerService = Cc["@mozilla.org/observer-service;1"]
                        .getService(Ci.nsIObserverService);
observerService.addObserver(myObserver, "content-document-global-created", true);


来源:https://stackoverflow.com/questions/8292511/how-to-manipulate-window-prototype-in-a-browser-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!