简体   繁体   English

使用nsINavHistoryObserver为Firefox扩展创建事件

[英]Using nsINavHistoryObserver to create events for a firefox extension

I am trying to make a firefox extension which looks at all the new pages that a user views and look at the URL. 我试图做一个firefox扩展,它可以查看用户查看的所有新页面并查看URL。 I was looking at the nsINavHistoryObserver interface and was interested in the onVisit function. 我正在查看nsINavHistoryObserver界面,并对onVisit函数感兴趣。 Is there a way to create an event listener that can listen to whenever the onVisit function is called? 有没有一种方法可以创建一个事件侦听器,该事件侦听器可以在调用onVisit函数时进行侦听?

Thanks 谢谢

you can find some information about Places (the bookmarks and history system) in Mozilla Developer Center: https://developer.mozilla.org/en/Places 您可以在Mozilla开发人员中心找到有关“地方”(书签和历史记录系统)的一些信息: https : //developer.mozilla.org/en/Places

you can find examples in our tests unit (notice some of them could not be optimized for performance) http://mxr.mozilla.org/mozilla-central/search?string=nsINavHistoryobserver&find=tests 您可以在我们的测试单元中找到示例(请注意,其中一些示例无法针对性能进行优化)http://mxr.mozilla.org/mozilla-central/search?string=nsINavHistoryobserver&find=tests

What you need is to get history service, and attach an history observer to it 您需要获取历史记录服务,并将历史记录观察者附加到该服务上

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
         getService(Ci.nsINavHistoryService);

var historyObserver = {
  onBeginUpdateBatch: function() {},
  onEndUpdateBatch: function() {},
  onVisit: function(aURI, aVisitID, aTime, aSessionID, aReferringID, aTransitionType) {},
  onTitleChanged: function(aURI, aPageTitle) {},
  onBeforeDeleteURI: function(aURI) {},
  onDeleteURI: function(aURI) {},
  onClearHistory: function() {},
  onPageChanged: function(aURI, aWhat, aValue) {},
  onDeleteVisits: function() {},
  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
};

hs.addObserver(historyObserver, false);

You should try to reduce most as possible the work you do in the addVisit notification, you should really handle it asynchronously to avoid slowing down the basic browser functionality. 您应该尽量减少在addVisit通知中所做的工作,您应该真正异步处理它,以免减慢基本浏览器功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM