Shatu
Shatu

Reputation: 1839

Event Listeners to know when a page is Bookmarked/Downloaded

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.

Upvotes: 2

Views: 788

Answers (2)

atcw
atcw

Reputation: 31

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

Upvotes: 1

Wladimir Palant
Wladimir Palant

Reputation: 57681

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

Upvotes: 4

Related Questions