问题描述
|
我正在尝试开发一个Chrome扩展程序,在该扩展程序中,您可以按键盘快捷键,并且该URL将在新窗口中加载,但是我只能在同一标签中打开该URL。
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>
到目前为止,以上所有方法均有效,但将在同一选项卡中显示特定的URL。是否有可能显示一个新的(弹出)窗口?
我尝试了许多编码,但是没有运气。
我试过了,但是没用
(window == top) {
window.addEventListener(\'keyup\',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\',function(window) {
});
});
}
}
解决方法
我使用此功能在新窗口中打开:
function openInNewWindow(url) {
var newWindow = window.open(url,\'_blank\');
newWindow.focus();
}