Chrome 扩展程序中的键盘快捷键

发布于 2024-12-18 12:14:01 字数 402 浏览 0 评论 0原文

我正在尝试为我的 chrome 扩展程序连接键盘快捷键。我为此使用了 jQuery 插件: http://oscargodson.com/labs/jkey/

这是我用来测试它的代码:

$(document).ready(function() {
    function say_hello() {
       alert("hello!");
    }
    $(document).jkey('/', say_hello);
});

我现在在我的后台页面中有这个,但它不起作用。这种类型的代码应该放在后台页面中,还是更适合内容脚本?或者我应该把它完全放在其他地方?

I'm trying to hook up a keyboard shortcut for my chrome extension. I'm using a jQuery plugin for this: http://oscargodson.com/labs/jkey/.

Here's the code I'm using to test it out:

$(document).ready(function() {
    function say_hello() {
       alert("hello!");
    }
    $(document).jkey('/', say_hello);
});

I have this in my background page right now, but it doesn't work. Is this type of code something that should go in the background page, or something that's more appropriate for a content script? Or should I place it somewhere else entirely?

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

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

发布评论

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

评论(2

赏烟花じ飞满天 2024-12-25 12:14:01

我知道这个问题很古老,但以防万一您还没有发现,这是您问题的最新解决方案:

Chrome 扩展现在有一个事件,只要在任何 chrome 窗口中按下特定的组合键(除了可能是一个单独的组合键),该事件就会触发隐身会话,我没有尝试过),使用 chrome.commands API

设置方法如下:

  1. 将“commands”键添加到您的manifest.json根目录中,并填充一个
    或更多命令(在本例中为“重新启动应用程序”)。每个命令应该
    指定 PC 和 Mac 所需的组合键。 (一些组合,
    像 ctrl-f5 一样,受到限制,仅供参考。我也不确定描述是否正确
    是可选的或必需的。)

    <前><代码>“命令”:{
    “重新启动应用程序”:{
    “建议的密钥”:{
    “默认”:“Ctrl+Shift+5”,
    "mac": "Command+Shift+5"
    },
    "description": "重新启动我的应用程序(调试)"
    }
    }

  2. 在background.js中,将处理程序绑定到“onCommand”事件,并且
    过滤“command”,它将匹配您中声明的键
    当你的组合被按下时就会显现:

    chrome.commands.onCommand.addListener(函数(命令)
    {
        if(命令==“重新启动应用程序”)
        {
            chrome.runtime.reload();
        };
    });
    

就是这样!

有人质疑热键的价值,但我是它们的坚定拥护者。每次使用将鼠标/键盘模式切换为仅键盘所节省的时间可能不到 1 秒,但经过几次繁重的调试会话后,时间就会开始增加。将上面示例中的快捷方式添加到我开发的应用程序中可能已经节省了我几个小时的时间:)

享受吧!

-马特

I know this question is ancient, but just in case you haven't discovered, here's the latest solution to your problem:

Chrome extensions now have an event that will trigger whenever specific key-combinations are pressed in any chrome window (except maybe a separate incognito session, which I haven't tried), with chrome.commands API

Here's how to set it up:

  1. Add a "commands" key to your manifest.json root, populated with one
    or more commands ("Restart App" in this case). Each command should
    specify the desired key combination for PC and Mac. (Some combos,
    like ctrl-f5, are restricted, fyi. Im also not sure if description
    is optional or required.)

    "commands": {
      "Restart App": {
        "suggested_key": {
          "default": "Ctrl+Shift+5",
          "mac": "Command+Shift+5"
        },
        "description": "Restart my app (Debugging)"
      }
    }
    
  2. In background.js, bind a handler to the "onCommand" event, and
    filter on "command", which will match the key declared in your
    manifest when your combo has been pressed:

    chrome.commands.onCommand.addListener(function (command)
    {
        if (command == "Restart App")
        {
            chrome.runtime.reload();
        };
    });
    

And that's it!

I've had people question the value of hotkeys, but I'm a strong advocate for them. The amount of time saved by switching a mouse/keyboard pattern to keyboard-only is probably <1sec per use, but over several heavy debug sessions it starts to add up. Adding the shortcut in the example above to apps I develop has probably saved hours of my time by now :)

Enjoy!

-Matt

说不完的你爱 2024-12-25 12:14:01

背景页面(通常)无法获得焦点或单击,因此它们永远不会接收事件。您必须将其作为内容脚本注入。

请注意,这在某些页面上不起作用,包括“新标签页”。不幸的是,没有办法解决这个问题。 :/

Background pages can't (ordinarily) be focused or clicked on, so they never receive events. You'd have to inject this as a content script.

Note that this won't work on some pages, including the New Tab page. Unfortunately, there's no way around this. :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文