2011-04-13 103 views
1

我正在嘗試開發Chrome擴展程序,您在按下鍵盤快捷鍵時將會在新窗口中加載該URL,但我只能獲得要在中打開的URL相同的選項卡。鍵盤快捷鍵,打開新窗口[Chrome擴展程序]

的script.js

if (window == top) { 
    window.addEventListener('keyup', doKeyPress, false); //add the keyboard handler 
    } 
    var post = "urlhere.com"; 

    var trigger_key = 85; // u key 
    function doKeyPress(e){ 
     if (e.altKey && e.keyCode == trigger_key) { 
      chrome.extension.sendRequest({redirect: post}); 
     } 
    } 

background.html

<script> 
chrome.browserAction.onClicked.addListener(function() { 
    var w = 440; 
    var h = 220; 
    var left = (screen.width/2)-(w/2); 
    var top = (screen.height/2)-(h/2); 
    chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': w, 'height': h, 'left': left, 'top': top} , function(window) { 
    }); 
}); 

chrome.extension.onRequest.addListener(function(request, sender) { 
     chrome.tabs.update(sender.tab.id, {url: request.redirect}); 
    }); 

    </script> 

到目前爲止,上述所有作品,具體網址將被顯示,但在相同的標籤。它可能會顯示一個新的(彈出)窗口? 我已經嘗試了大量的編碼,但沒有這樣的運氣。

我想這一點,但沒有奏效

(window == top) { 
    window.addEventListener('keyup', doKeyPress, false); //add the keyboard handler 
    } 
    var post = "urlhere.com"; 
    var w = 440; 
    var h = 220; 
    var left = (screen.width/2)-(w/2); 
    var top = (screen.height/2)-(h/2); 
    var trigger_key = 85; // u key 
    function doKeyPress(e){ 
     if (e.altKey && e.keyCode == trigger_key) { 
chrome.browserAction.onClicked.addListener(function() { 
    chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': w, 'height': h, 'left': left, 'top': top} , function(window) { 
    }); 
});  
     } 
    } 
+0

能否請您出示background.html – serg 2011-04-13 19:16:26

+0

@serg增加 - 你可能記得從你以前的幫助! – itsdaniel0 2011-04-13 19:24:31

+0

如果你想打開一個新窗口,那麼爲什麼不在背景頁面中使用'chrome.windows.create()'代碼而不是'chrome.tabs.update()'? – serg 2011-04-13 19:28:17

回答

0

我使用此功能在新窗口中打開:

function openInNewWindow(url) { 
    var newWindow = window.open(url, '_blank'); 
    newWindow.focus(); 
}