Chrome getSelection not working if called from context menu
I'm working on a Chrome extension, but I can't seem to get past the first few lines. The problem is that when I click the context menu, it cannot grab the selection from the document. It works great when I use it as a simple script included in the HTML page.
Here's the code:
var id = chrome.contextMenus.create({
"title" : "Search",
"contexts" : ["selection"],
"onclick" : openUrl
});
function openUrl() {
var sel = window.getSelection().toString().trim()
alert(sel)
}
This code returns an empty alert field.
I have a script that, on mouseup, grabs the word the user has selected and looks for that word in the dictionary. This script works great, I just need to execute it when the user clicks "Search" on the context menu. So what I'm looking for is: 1) User selects a word from the document 2) Right click on it and clicks on the context menu 3) a script containing all the command to be executed on that click.
I looked around before asking this question, but I couldn't find anything because I'm pretty new to this great site. Please feel free to redirect me to another similar question if I missed it. Thank!
source to share