如何在<浏览器>操作窗口原型元件? [英] How to manipulate window prototype in a <browser> element?

查看:156
本文介绍了如何在<浏览器>操作窗口原型元件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $ << browser src =.../> load,我将数据附加到它的 .contentWindow 中:

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

现在我想知道是否有办法做到这一点,进入< browser> 的窗口原型,或者任何Window原型,例如我可以在当前窗口中进行操作:

  // [W] indow是构造函数
Window.prototype.test = function(){alert(hello); };
// [w] indow是实例
window.test();


解决方案

目前有两种方法将属性注入窗口任何JavaScript代码运行之前。通常, content-document-global-created 通知更简单。另一个是实现 nsIDOMGlobalPropertyInitializer 界面 。两者都允许您在新窗口加载时,在窗口运行JavaScript代码之前得到通知。



以下是使用观察者通知进行操作的近似代码:

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);


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();

解决方案

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);

这篇关于如何在&lt;浏览器&gt;操作窗口原型元件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆