我正在尝试开发Chrome扩展程序,您按下键盘快捷键,URL将在新窗口中加载,但我只能在相同的标签中打开URL。< / p>
的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 :(得分:0)
我使用此功能在新窗口中打开:
function openInNewWindow(url) {
var newWindow = window.open(url, '_blank');
newWindow.focus();
}