Can anyone tell me why I cannot get a string for the selected text on any given page using windows.getSelection().toString()?
The script is running in the background, even though I've named it as popup.js.
Everything I can find shows a variation of this, but in my code it always returns null. I am calling window.getSelection() on a shortcut combination being pressed. Text is definitely selected, but the result is always null.
manifest.json
{
"name": "CM_TextDiff",
"version": "1.0",
"manifest_version": 2,
"description": "Highlight changes in two lines of text. Useful to language teachers to highlight corrections.",
"icons": {
"128": "compare128.png"
},
"background": {
"scripts": [
"popup.js"
]
},
"browser_action": {
"default_title": "highlight your corrections",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"activeTab",
"http://*/*",
"https://*/*",
"clipboardRead",
"clipboardWrite",
"contextMenus"
],
popup.js
chrome.commands.onCommand.addListener(function(command) {
console.log('Command:', command);
getSelectedText();
});
function getSelectedText(){
alert ("The text content of the selection:\n" + window.getSelection().toString ());
}
I would expect this to display a popup with the text that is selected on the page. However, the result is always a nullstring.