用于了解页面何时被添加书签/下载的事件侦听器

发布于 2024-12-08 21:15:44 字数 116 浏览 2 评论 0原文

有什么方法可以跟踪 Firefox 中页面被添加书签或下载的时间吗?我 意思是是否有任何事件在书签或 下载页面?我正在使用 Add-on SDK 来开发 Add-on。

如果没有,请建议我一些解决方法。

Is there any way to track when a page is bookmarked or downloaded in Firefox? I
mean is there any event that is triggered on bookmarking or
downloading a page? I am using Add-on SDK for developing Add-on.

If not, then kindly suggest me some workarounds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2024-12-15 21:15:44

浏览器窗口具有被触发的 元素当用户添加书签或下载页面时。前者的ID为Browser:AddBookmarkAs,后者的ID为Browser:SavePage。附加 SDK 本身不允许您访问它们,因此您需要使用 chrome直接访问 XPCOM。像这样的事情:

// Add listener to all existing browser windows
var {Cc, Ci} = require("chrome");
var mediator = Cc["@mozilla.org/appshell/window-mediator;1"]
                 .getService(Ci.nsIWindowMediator);
var enumerator = mediator.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements())
  listenToWindow(enumerator.getNext().QueryInterface(Ci.nsIDOMWindow));

// Get notified when new browser windows open
var observers = require("observer-service");
observers.add("chrome-document-global-created", function(window)
{
  if (window instanceof Ci.nsIDOMWindow && window.location.href == "chrome://browser/content/browser.xul")
    listenToWindow(window);
});

function listenToWindow(window)
{
  window.document
        .getElementById("Browser:AddBookmarkAs")
        .addEventListener("command", onBookmark, false);
  window.document
        .getElementById("Browser:SavePage")
        .addEventListener("command", onSavePage, false);
}

此代码未经测试,因此可能存在小问题,但总体概念应该是正确的。

编辑:实际上,如果您使用内部window-utils包,似乎同样更简单。但不确定该包提供的 API 是否稳定。

var windows = require("window-utils");
for (window in windows.browserWindowIterator)
  listenToWindow(window);

var observers = require("observer-service");
observers.add("chrome-document-global-created", function(window)
{
  if (window instanceof Ci.nsIDOMWindow && windows.isBrowser(window))
    listenToWindow(window);
});

The browser window has <command> elements that get triggered when the user bookmarks or downloads a page. The former has ID Browser:AddBookmarkAs, the latter Browser:SavePage. The Add-on SDK itself doesn't give you access to them, so you need to use the chrome package to access XPCOM directly. Something like this:

// Add listener to all existing browser windows
var {Cc, Ci} = require("chrome");
var mediator = Cc["@mozilla.org/appshell/window-mediator;1"]
                 .getService(Ci.nsIWindowMediator);
var enumerator = mediator.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements())
  listenToWindow(enumerator.getNext().QueryInterface(Ci.nsIDOMWindow));

// Get notified when new browser windows open
var observers = require("observer-service");
observers.add("chrome-document-global-created", function(window)
{
  if (window instanceof Ci.nsIDOMWindow && window.location.href == "chrome://browser/content/browser.xul")
    listenToWindow(window);
});

function listenToWindow(window)
{
  window.document
        .getElementById("Browser:AddBookmarkAs")
        .addEventListener("command", onBookmark, false);
  window.document
        .getElementById("Browser:SavePage")
        .addEventListener("command", onSavePage, false);
}

This code isn't tested so there might be minor issues but the overall concept should be correct.

Edit: Actually, the same seems to be simpler if you use the internal window-utils package. Not sure whether the API provided by this package is stable however.

var windows = require("window-utils");
for (window in windows.browserWindowIterator)
  listenToWindow(window);

var observers = require("observer-service");
observers.add("chrome-document-global-created", function(window)
{
  if (window instanceof Ci.nsIDOMWindow && windows.isBrowser(window))
    listenToWindow(window);
});
千秋岁 2024-12-15 21:15:44

全部在addon sdk文档中。尽管我必须承认我第一次并没有看到它。

https://addons .mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/tutorials/event-targets.html

以下示例来自文档。

请注意,我必须将 Cr 添加到 require 中才能使其正常工作,

并在generateQI() 调用中用 Ci 替换 Components.interfaces。

var {Cc, Ci, Cu, Cr} = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);    
var bookmarkService = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
                          .getService(Ci.nsINavBookmarksService);

var bookmarkObserver = {
  onItemAdded: function(aItemId, aFolder, aIndex) {
    console.log("added ", bookmarkService.getBookmarkURI(aItemId).spec);
  },
  onItemVisited: function(aItemId, aVisitID, time) {
    console.log("visited ", bookmarkService.getBookmarkURI(aItemId).spec);
  },
  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver])
};

exports.main = function() {
  bookmarkService.addObserver(bookmarkObserver, false);   
};

exports.onUnload = function() {
  bookmarkService.removeObserver(bookmarkObserver);
}

It is all in the addon sdk documentation. Although I must admit I did not see it the first time around.

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/tutorials/event-targets.html

The following example is from the documentation.

Note that I had to add Cr to the require to make it work

as well as substitute Components.interfaces by Ci in the generateQI() call.

var {Cc, Ci, Cu, Cr} = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);    
var bookmarkService = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
                          .getService(Ci.nsINavBookmarksService);

var bookmarkObserver = {
  onItemAdded: function(aItemId, aFolder, aIndex) {
    console.log("added ", bookmarkService.getBookmarkURI(aItemId).spec);
  },
  onItemVisited: function(aItemId, aVisitID, time) {
    console.log("visited ", bookmarkService.getBookmarkURI(aItemId).spec);
  },
  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver])
};

exports.main = function() {
  bookmarkService.addObserver(bookmarkObserver, false);   
};

exports.onUnload = function() {
  bookmarkService.removeObserver(bookmarkObserver);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文