gpt4 book ai didi

javascript - Firefox 中的窗口观察者

转载 作者:行者123 更新时间:2023-11-28 09:00:12 24 4
gpt4 key购买 nike

我正在构建一个扩展,我想捕获窗口事件。我有这个代码:......................................

  function MyWindowObserver() {
this.observe=function(aSubject, aTopic, aData) {
if(aSubject.innerWidth!=1536)
aSubject.close();
}
}

function myObserver(){}

myObserver.prototype = {
observe: function(subject, topic, data) {

},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "http-on-modify-request", false);

},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
}
}

function installButton()
{
var id = "button_cs";
var toolbarId = "nav-bar";

var toolbar = document.getElementById(toolbarId);

//add the button at the end of the navigation toolbar
toolbar.insertItem(id, toolbar.lastChild);
toolbar.setAttribute("currentset", toolbar.currentSet);
document.persist(toolbar.id, "currentset");

//if the navigation toolbar is hidden,
//show it, so the user can see your button
toolbar.collapsed = false;
}

function firstRun(extensions) {
var extension = extensions.get("bak@asda.com");

if (extension.firstRun) {
installButton();
}
}

var myExtension={

Observers:null,
prefs:null,
clock:null,
prevwidth:null,
prevon:null,
BrosNumber:null,

init:function(){

if (Application.extensions)
firstRun(Application.extensions);
else
Application.getExtensions(firstRun);

this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");

this.Observers = new myObserver();

this.Observers.register();
}
};

myExtension.init();

window.addEventListener('load', function(win) {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(new MyWindowObserver(), "toplevel-window-ready", false);
},false);

我做错了什么? aSubject 不是最后打开的窗口吗?

有什么想法吗?

最佳答案

它监视http-on-modify-requesttoplevel-window-ready

观察者不是基于每个窗口的。您只需在全局范围内注册它们即可。

var{interfaces: Ci, utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');

var observers = {
httpModReq: {
observe: function (aSubject, aTopic, aData) {
console.log('incoming observe httpModReq - aSubject: "' + aSubject + '" | aTopic: "' + aTopic + '" | aData: "' + aData + '"');
var httpChannel, requestURL;
httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
requestURL = httpChannel.URI.spec;

if (requestURL.indexOf('google.com') > -1) {
//they are loading google
//httpChannel.setRequestHeader('MyCustomRequestHeader', 'hiiii', false);
/*
try {
var Host = httpChannel.getRequestHeader('Host');
} catch (ex) {
//if doesnt have Host header it throws exception "NS_ERROR_NOT_AVAILABLE"
var Host = 'NULL';
}
console.log('REQUEST Header "Host" = ' + Host);
*/
}
},
register: function () {
Services.obs.addObserver(observer.httpModReq, 'http-on-modify-request', false);
},
unregister: function () {
Services.obs.removeObserver(observer.httpModReq, 'http-on-modify-request');
}
},
topWinRdy: {
observe: function (aSubject, aTopic, aData) {
console.log('incoming observe topWinRdy - aSubject: "' + aSubject + '" | aTopic: "' + aTopic + '" | aData: "' + aData + '"');
},
register: function () {
Services.obs.addObserver(observer.topWinRdy, 'toplevel-window-ready', false);
},
unregister: function () {
Services.obs.removeObserver(observer.topWinRdy, 'toplevel-window-ready');
}
}
};


//run this block to start observing
for (var o in observers) {
observers[o].register();
}

/*//run this block when you want to remove the observers
for (var o in observers) {
observers[o].unregister();
}
*/

您可以将其复制粘贴到暂存器中,并在环境设置为浏览器的情况下运行它。它会工作得很好。

关于javascript - Firefox 中的窗口观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com