gpt4 book ai didi

firefox - 如何在 Firefox 扩展中观察指定的 Firefox 事件?

转载 作者:行者123 更新时间:2023-12-02 15:47:01 33 4
gpt4 key购买 nike

我想等待 Firefox-Browser-Events (sessionstore-windows-restored, user-interaction-inactive,..) 以清除历史记录。我的扩展目前能够清除快捷方式的历史记录,但我不知道如何监听此类事件。我已经浏览过observer notification ,但我尝试过的似乎都不起作用。

到目前为止我的扩展代码在这里:

   (function() {

(window.myObj =
{
myObserver : function()
{
this.register();
},

init : function()
{
try {

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

switch (topic) {
case 'sessionstore-windows-restored':
Firebug.Console.log('sessionstore-windows-restored observed');
myObj.clearHistory();
break;
case 'user-interaction-inactive':
Firebug.Console.log('user-interaction-inactive');
break;
case 'user-interaction-active':
Firebug.Console.log('user-interaction-active');
break;
}

Firebug.Console.log('HUI :' + subject + ','+topic+','+data);
},
register: function(){
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "readyToClearHistory", false);
},
unregister: function(){
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "readyToClearHistory");
}
}
}
catch (e) {
Firebug.Console.log('MIST :' + e);
}

observer = new myObserver();
},
clearHistory : function(){
var globalHistory = Components.classes["@mozilla.org/browser/global-history;2"].getService(Components.interfaces.nsIBrowserHistory);
var now = Date.now();

// clear one hour
globalHistory.removeVisitsByTimeframe(now - 3600, now);

try {
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
os.notifyObservers(null, "browser:purge-session-history", "");
}
catch (e) {

// Clear last URL of the Open Web Location dialog
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
try {
prefs.clearUserPref("general.open_location.last_url");
}
catch (e) {
}
}
}
}
}

一些示例代码会很好。我需要什么来解决这个问题?

最佳答案

我没有发现代码中特定于 nsIObserver 的部分有任何问题。 ,这意味着您的问题可能出在其他地方。这个完整的示例有效:

var ObserverTest = {

register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(ObserverTest, "user-interaction-active", false);
},

observe: function(subject, topic, data) {
switch (topic) {
case 'sessionstore-windows-restored':
// do stuff
break;
case 'user-interaction-inactive':
// do stuff
break;
case 'user-interaction-active':
// every 5 seconds and immediately when user becomes active
alert("active");
break;
}
},

unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(ObserverTest, "user-interaction-active");
}
}

window.addEventListener("load", ObserverTest.register, false);
window.addEventListener("unload", ObserverTest.unregister, false);
一般来说,我发现第一次尝试使用不熟悉的组件时从一些非常简单的东西开始很有帮助,就像我上面所做的那样,这样我就可以将其与特定于应用程序的代码隔离并验证我是否正在正确使用它。在我知道它正在工作之前,我不会将其拉入更大的应用程序。

关于firefox - 如何在 Firefox 扩展中观察指定的 Firefox 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5183230/

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