键盘快捷键,打开新窗口 [Chrome 扩展]

发布于 2024-11-01 10:55:06 字数 1784 浏览 1 评论 0原文

我正在尝试开发一个 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>

到目前为止,上述所有内容都有效,具体的网址将显示,但在同一个选项卡中。它是否可以显示一个新的(弹出)窗口? 我已经尝试了大量的编码,但没有这样的运气。

我尝试过这个,但没有成功

(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'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.

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) {
    });
});     
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-11-08 10:55:06

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

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

I use this function for opening in a new window:

function openInNewWindow(url) {
   var newWindow = window.open(url, '_blank');
   newWindow.focus();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文