2012-01-10 132 views
0

我正試圖編寫一個簡單的谷歌擴展程序,它將在點擊「ctrl + alt + x」搜索google中的選定文本。在Chrome擴展程序中打開新標籤

這是我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" 
    ] 
} 

這是我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); 
    } 
}); 

代碼工作,直到行打開一個新標籤(第一個警報彈出,但不第二)。我似乎無法得到它的工作。我錯過了什麼?

回答

5

根據Google Chrome Content Scripts referencechrome.tabs(以及除chrome.extension之外的所有內容)都不適用於內容腳本。

或者,您可以嘗試window.open(),或使用message passing讓您的背景頁面打開選項卡。

+2

Thx,我用消息傳遞。它像一個魅力。我現在可以通過按ctrl + alt + x來僅通過google選定的文本:) – synepis 2012-01-10 22:45:00

相關問題