【问题标题】:Need to get selected text as a string from the current tab需要从当前选项卡中获取选定文本作为字符串
【发布时间】:2013-04-10 17:08:09
【问题描述】:

这是我目前拥有的代码:

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()
});

我需要从当前选项卡中获取选择。我不知道如何得到它。 有人可以帮忙吗?

【问题讨论】:

标签: javascript html google-chrome-extension


【解决方案1】:

两个问题:

  1. 不要调用clicked(),而是通过省略括号传递函数引用。
  2. var selection 可从function's parameters 获得。在文档中,这个对象被描述为OnClickData。所需信息存储在 selectionText 键中。

应用这些修复后的代码:

// 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
});

【讨论】:

  • :) 这真的很有帮助!谢谢!
猜你喜欢
  • 2010-12-24
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2013-03-08
  • 2011-05-14
  • 2014-12-02
相关资源
最近更新 更多