How to use window.getSelection on an object parameter (here: browser.menus.onClicked.addListener((info, tab) `

I am building a FF Add-on that has to ensure the following:

  1. User highlights text, e.g. the word “example” on a page
  2. User opens right-click custom browser menu
  3. User selects icon of add-on
  4. In consequence, user is being taken to a website that contains the hightlighted text between www. and .com, e.g.: www.example.com

Currently, the injection of the content taken through
“window.getSelection().toString();” into the “url: part” does not work.

Maybe it is a scope problem? Thx in advance.

I am an absolute beginner. I moved “let selObj = window.getSelection().toString();” directly under “browser.tabs.create({… – with no success.

‘use strict’;

const openLabelledId = "open-labelled";  browser.menus.create({ id: openLabelledId, title: "Open", contexts: ["selection"] 

});

let selObj = window.getSelection().toString();  browser.menus.onClicked.addListener((info, tab) => { if (info.menuItemId === openLabelledId) { browser.tabs.create({   url: "(`http://$  {selObj).com+`)" }); 

} });

Expected result: selObj defines which website will be opened, e.g. when highlighting “cnn”, www.cnn.com is being opened

Actual result: “File on moz-extension://562a42ef-c853-4689-8488-fafbfa753353/(http://$ {selObj).com) can’t be read.”