1

Here's the code I currently have:

var clicked = function(){
    var selection
    chrome.tts.speak(selection, {'lang': 'en-US', 'rate': 0.8, 'enqueue': true});
}
chrome.contextMenus.create({
  "title" : "Say '%s'!",
  "type" : "normal",
  "contexts" : ["selection"],
  "onclick" : clicked()
});

I need to get the selection from the current tab. I don't know how to get it. Can anyone help?

V360
  • 25
  • 1
  • 1
  • 7

1 Answers1

1

Two issues:

  1. Don't call clicked(), but pass a function reference, by omitting the parentheses.
  2. var selection is available from the function's parameters. In the documentation, this object is described as OnClickData. The desired info is stored in the selectionText key.

Code after applying these fixes:

// Background page
var clicked = function(info, tab) {
    var selection = info.selectionText;
    chrome.tts.speak(selection, {'lang': 'en-US', 'rate': 0.8, 'enqueue': true});
};
chrome.contextMenus.create({
  "title" : "Say '%s'!",
  "type" : "normal",
  "contexts" : ["selection"],
  "onclick" : clicked
});
Rob W
  • 341,306
  • 83
  • 791
  • 678