Keyboard Shortcut, Open New Window [Chrome Extension]
I'm trying to develop a Chrome extension where you press a keyboard shortcut and the URL will load in a new window, but I can only get the URL to open in the same tab.
开发者_Go百科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>
So far, all of the above works, the specific url will display, but in the same tab. Is it possible for it to display a a new (popup) window? I've tried numerous amounts of coding, but no such luck.
I tried this, but it did not work
(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) {
});
});
}
}
I use this function for opening in a new window:
function openInNewWindow(url) {
var newWindow = window.open(url, '_blank');
newWindow.focus();
}
精彩评论