I'm working on a Chrome extension but I don't seem to be able to go past the first few lines. The problem is that when I click on the context menu it fails to grab the selection from the document. It works fine obviously when I use it as a simple script included in an 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 box.
I have a script that on mouseup grabs the word that the user has selected and searches for this word on a dictionary. This script works fine I just need to execute it when the user clicks "Search" in the context menu. So what I'm looking for is: 1) User selects a word from the document 2) Right clicks on it and clicks on the context menu 3) The script containing all instruction to be executed on that click.
I looked around before asking this question but I couldn't find anything probably because I'm pretty new to this awesome site. Please feel free to redirect me to other similiar question if I missed any. Thanks!