I am trying to make a simple extension, where the user can do a dictionary search by hitting a shortcut. It will open a new tab with the dictionary search of the word selected by the user. However, when I try to use "window.getSelection()", it returns an empty string. I am using the Chrome browser, latest version.
This is the background2.js file.
chrome.commands.onCommand.addListener(function(command) {
console.log('onCommand event received for message: ', command);
chrome.tabs.create({
url: "https://jisho.org/search/" + encodeURIComponent(window.getSelection().toString()),
active: true
});
});
This is the manifest.json file.
{
"manifest_version": 2,
"name": "Jisho Dictionary Search",
"permissions": [ "contextMenus" , "tabs" ],
"version": "1.2",
"background": {
"scripts": [ "background.js", "background2.js" ]
},
"description": "Adds a shortcut to do a Jisho search automatically",
"browser_action": {
"default_popup" : "browser_action.html"
},
"commands": {
"toggle_feature": {
"suggested_key": { "default": "Ctrl+X" },
"description" : "Opens new tab with word search on Jisho"
},
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+X"
}
}
}
}
Can someone help me understand why it is returning an empty string and how could I make it return the word the user selected?