Search code examples
firefox-addonfirefox-addon-sdk

Can I somehow listen on the URL user is typing with Firefox addon SDK?


Is there a way with Firefox Add-on SDK to get the URL while user is typing it in the location field?


Solution

  • Yes and no. Yes it is possible, but not within the SDK standard API.

    1. You need to enumerate top-level browser windows (aka. type navigator:browser, chrome://browser/content/browser.xul). window/utils::windows() may help here, but it has the drawback that it will not notify you about new windows. However, you can use require("chrome") and nsIWindowWatcher.registerNotification for that.
    2. Once you got to the browser windows, it is a matter of adding the usual key event listeners using the usual addEventListener DOM API. The DOM element in browser.xul you're looking for is #urlbar.
    3. Since SDK add-ons are restartless, and you're leaving the managed land, using the low-level APIs, you need to clean up after yourself on unload, e.g. remove any event listeners or observers again and restore anything you touched.