我正在尝试编写一个简单的Google扩展程序,点击“ctrl + alt + x”搜索google中的所选文本。
这是我的主要节日:
{
"name": "A jQuery Chrome extension",
"version": "0.1",
"description": "Use jQuery to build Chrome extensions",
"content_scripts": [
{
"matches" : ["http://*/*"],
"js": ["jquery.js", "jquery.hotkeys.js", "content.js"]
}
],
"background_page": "background.html",
"permissions": [
"tabs"
]
}
这是我的内容.js:
$(document).bind('keydown', 'alt+ctrl+x', function() {
var selectedText = window.getSelection().toString();
if (selectedText)
{
var googleQuery = "http://www.google.com/search?q=" + selectedText;
alert(googleQuery);
chrome.tabs.create({"url" : googleQuery});
alert(googleQuery);
}
});
代码一直有效,直到打开新标签页的行(第一个警告弹出但不弹出第二个警告)。我似乎无法让它工作。我错过了什么?
答案 0 :(得分:6)
根据Google Chrome Content Scripts reference,内容脚本无法使用chrome.tabs
(以及chrome.extension
以外的所有内容)。
作为替代方案,您可以尝试window.open()
,或使用message passing让您的背景页面打开标签。