# window.getSelection() and browser.commands.onCommand.addListener

**URL:** <https://discourse.mozilla.org/t/window-getselection-and-browser-commands-oncommand-addlistener/20457>\
**Category:** Development\
**Created:** [October 15, 2017, 4:31am UTC](https://discourse.mozilla.org/t/window-getselection-and-browser-commands-oncommand-addlistener/20457 "2017-10-15T04:31:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpa470](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@wpa470](https://discourse.mozilla.org/u/wpa470)\
**Post date:** [October 15, 2017, 4:31am UTC](https://discourse.mozilla.org/t/window-getselection-and-browser-commands-oncommand-addlistener/20457/1 "2017-10-15T04:31:47Z")

</div>

Hi,

When I try to use a hotkey detection for get a selected text, the function window.getSelection() send to me that:

Selection { anchorNode: null, anchorOffset: 0, focusNode: null, focusOffset: 0, isCollapsed: true, rangeCount: 0, caretBidiLevel: null }

Maybe it is a problem of focus ? Some add-on do the job but when I read the code, it seems it is old methods or they don’t use window.getSelection(), so is is possible to use window.getSelection() after a hotkey ? If yes, how ? if not, what is the best method with webextension.

I use the standard code of Firefox:

```
{
  "name": "Sample Commands Extension",
  "description": "Press Ctrl+Shift+U to send an event (Command+Shift+U on a Mac).",
  "manifest_version": 2,
  "version": "1.0",

  "commands": {
    "toto": {
      "suggested_key": {"default": "Ctrl+Shift+0" } ,
      "description": "Send a 'toggle-feature' event to the extension"
    }
  },
  
   "background": {
    "scripts": ["ts.js"]
  }
}

```

And

```
 browser.commands.onCommand.addListener(function(command) {
  if (command == "toto") {
    console.log("toggling the feature!");
  var texte="";
  text=window.getSelection();
  console.log(text);
   // var translate = require('yandex-translate')(key);
 
translate.translate('You can burn my house, steal my car, drink my liquor from an old fruitjar.', { to: 'ru' }, function(err, res) {
  console.log(res.text);
  
});

```

The hotkey works, but I always receive a null or nothing (I tried a lot of solution all the last day !)

I use Firefox 56 on Debian Stretch.

Thanks if you can help me 🙂

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [October 15, 2017, 9:45am UTC](https://discourse.mozilla.org/t/window-getselection-and-browser-commands-oncommand-addlistener/20457/2 "2017-10-15T09:45:29Z")

</div>

You are getting the selection from the extensions (invisible) background page, not the current website.

```auto
browser.commands.onCommand.addListener(async command => {
  const { id: tabId, } = (await browser.tabs.query({ active: true, currentWindow: true, })[0];
  const text = (await browser.tabs.executeScript(tabId, { code: 'getSelection()+""', }))[0];
  console.log('Page selection:', text);
  // ...
});

```

---

<div class="post-metadata">

**Author:** ![scarpafranco92](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@scarpafranco92](https://discourse.mozilla.org/u/scarpafranco92)\
**Post date:** [July 27, 2022, 10:44am UTC](https://discourse.mozilla.org/t/window-getselection-and-browser-commands-oncommand-addlistener/20457/4 "2022-07-27T10:44:13Z")

</div>

There’s a missing comma: from `...currentWindow: true, })[0];` to `...currentWindow: true, }))[0];`.
