I’m trying to take the selected text on a page, save it to a variable, put it into a link and open that link in a new tab. I’ve got it working apart from taking the selected text.
manifest.json:
{
"manifest_version": 2,
"name": "Send2search test",
"version": "1.0",
"permissions": ["menus"],
"background": {
"scripts": ["bg.js"]
},
}
bg.js:
browser.menus.create(
{
id: "gglSearch",
title: "Search in new tab.",
contexts: ["selection"],
},
);
browser.menus.onClicked.addListener(async function (info, tab) {
var selectedTxt = window.getSelection().toString();
if (info.menuItemId == "gglSearch") {
var searchUrl = `https://www.google.com/search?q=${selectedTxt}`;
let openNewTab = browser.tabs.create(
{
active: false,
url: searchUrl,
index: tab.index+1
}
);
};
});
Every time it just opens blank google search page, so nothing gets saved in the selectedTxt variable. What am I doing wrong?
I’ve also tried content.getSelection().toString()
and info.selectionText()
, those cause no tab to be opened at all.
PS: I know what I’m making is a feature already, I’m just trying to work out the basics on this simple example.