javascript - 在 Chrome 扩展程序中打开新标签页

标签 javascript jquery json google-chrome-extension

我正在尝试编写一个简单的谷歌扩展,单击“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 referencechrome.tabs(以及除 chrome.extension 之外的所有内容)对于内容脚本不可用。

作为替代方案,您可以尝试 window.open() ,或使用message passing让您的后台页面打开选项卡。

关于javascript - 在 Chrome 扩展程序中打开新标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8811203/

相关文章:

javascript - Fullcalendar v2 将拖动事件存储到本地存储中

javascript - Kendo Grid 无法识别 Grid 事件生成器函数 "FilterMenuInit"

javascript - 当点击事件发生时查找哪个div被点击

php - 从 MySQL 获取数据并使用 JSON 在 ListView 中显示

Android - 使用 Retrofit 解析没有数组标题的 JSON

javascript - VueJS路由器中的 `path`和 `fullPath`有什么区别?

php - getElementById 有时为 null。已经尝试过 .onload 和 .ready

ruby - 将数字的 JSON 序列化强制到特定精度

jquery - 为 Ajax 序列化 session 内容

javascript - NicEdit textarea如何添加滚动条?