最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open new tab in Chrome extensions - Stack Overflow

programmeradmin2浏览0评论

I'm trying to write a simple google extension that will upon clicking "ctrl+alt+x" search for the selected text in google.

This is my mainfest:

{
  "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"
  ]
}

And this is my content.js:

$(document).bind('keydown', 'alt+ctrl+x', function() {

    var selectedText = window.getSelection().toString();

    if (selectedText)
    {
        var googleQuery = "=" + selectedText;
        alert(googleQuery);
        chrome.tabs.create({"url" : googleQuery});  
        alert(googleQuery);
    }
});

The code works until the line for opening a new tab (the first alert pops up but not the second). I just can't seem to get it working. What am I missing?

I'm trying to write a simple google extension that will upon clicking "ctrl+alt+x" search for the selected text in google.

This is my mainfest:

{
  "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"
  ]
}

And this is my content.js:

$(document).bind('keydown', 'alt+ctrl+x', function() {

    var selectedText = window.getSelection().toString();

    if (selectedText)
    {
        var googleQuery = "http://www.google./search?q=" + selectedText;
        alert(googleQuery);
        chrome.tabs.create({"url" : googleQuery});  
        alert(googleQuery);
    }
});

The code works until the line for opening a new tab (the first alert pops up but not the second). I just can't seem to get it working. What am I missing?

Share Improve this question asked Jan 10, 2012 at 22:05 synepissynepis 1,33216 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

According to the Google Chrome Tabs reference, chrome.tabs (and everything but chrome.extension) is not available to content scripts.

As an alternative, you could try window.open(), or use message passing to let your background page to open the tab.

发布评论

评论列表(0)

  1. 暂无评论