1

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!

waivy
  • 127
  • 1
  • 2
  • 7

1 Answers1

2
function openUrl(info, tab) {
     alert(info.selectionText);
}
VitVad
  • 336
  • 2
  • 8
  • It worked! Thank you so much! Could you please explain what are those info and tab parameters? Even a link to somewhere else will do. Thanks again! – waivy Jun 27 '12 at 12:23
  • [contextMenus](http://code.google.com/chrome/extensions/contextMenus.html#types) usually i even don't watch at specification - just make callback function in such way: function(e,r,t,y){console.log(e,r,t,y);} if there is some aruments in callback function I will see them, and chose what I need – VitVad Jun 27 '12 at 12:31