Search code examples
javascriptfirefox-addonxulfirefox-addon-sdk

document.activeElement returning a XULElement


I'm trying to get the focused element when the user press CTRL + SPACE on my Firefox add-on.

To do it, I thought to use the document.activeElement to get the focused element, but it is not working. It's always returning a XULElement.

I have just two files in this add-on:

Lib/main.js

var self = require ("sdk/self");

var workers = require("sdk/content/worker");
let worker =  workers.Worker({
                window: require("sdk/window/utils").getMostRecentBrowserWindow(),
                contentScriptFile: self.data.url("script.js")
              });


var { Hotkey } = require("sdk/hotkeys");
var showHotKey = Hotkey({
  combo: "control-space",
  onPress: function() {
        worker.port.emit ("getFocused", "");
  }
});

and the file Data/script.js

self.port.on ('getFocused', function (msg){
    var campo = document.activeElement;
    alert (campo);
});

(How you can see here: Add-on SDK Builder Test Project )

So, can someone help me with it?

It's possible to get the input text or textarea from the XULElement and change its text?

Thank you very much!

------------------------ EDIT ----------------------

I don't know if it helps, but, when the url text area (where we write the adress of the sites) has the focus, it returns a

[object HTMLInputElement]


Solution

  • getMostRecentBrowserWindow returns a ChromeWindow object. So it's normal that the activeElement` is a XUL element.

    What you should do is

    window: require("sdk/window/utils").getMostRecentBrowserWindow().gBrowser.contentWindow