我正在尝试编写一个简单的谷歌扩展,单击“ctrl+alt+x”即可在谷歌中搜索所选文本。
这是我的主要节日:
{
"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"
]
}
这是我的 content.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);
}
});
代码一直有效,直到出现打开新选项卡的行为止(弹出第一个警报,但不弹出第二个警报)。我似乎无法让它发挥作用。我错过了什么?
最佳答案
根据Google Chrome Content Scripts reference 、chrome.tabs
(以及除 chrome.extension
之外的所有内容)对于内容脚本不可用。
作为替代方案,您可以尝试 window.open()
,或使用message passing让您的后台页面打开选项卡。
关于javascript - 在 Chrome 扩展程序中打开新标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8811203/