键盘快捷键,打开新窗口[Chrome扩展程序]
问题描述:
我正在尝试开发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) {
});
});
}
}
答
我使用此功能在新窗口中打开:
function openInNewWindow(url) {
var newWindow = window.open(url, '_blank');
newWindow.focus();
}
能否请您出示background.html – serg 2011-04-13 19:16:26
@serg增加 - 你可能记得从你以前的帮助! – itsdaniel0 2011-04-13 19:24:31
如果你想打开一个新窗口,那么为什么不在背景页面中使用'chrome.windows.create()'代码而不是'chrome.tabs.update()'? – serg 2011-04-13 19:28:17