3

I am building a Chrome extension, and assigned command _execute_browser_action to Alt+J. I want to simulate this in background.js which listens for all commands using

chrome.commands.onCommand.addListener(function(command) { /* ... */ });

I want _execute_browser_action to be called through a different command shortcut, say Cmd+Shift+K

In my manifest.json I have declared the following:

"commands": {
    "_execute_browser_action": {
        "suggested_key": {
          "mac": "Alt+J",
          "linux": "Ctrl+Shift+J"
        }
    },
    "asdf" : {
        "suggested_key": {
          "default": "Ctrl+Shift+K",
          "mac": "Command+Shift+K"
        },
        "description": "asdf"
    }
  }

This is my background.js:

chrome.commands.onCommand.addListener(function(command) {
  console.log('onCommand event received for message: ', command);
  if (command === "asdf") {
    alert("asdf");
    var keyPress = jQuery.Event("keypress");
    keyPress.altKey = true;
    keyPress.ctrlKey = false;
    keyPress.which = 74;
    //how do I trigger this?
  }
});

I want to know how to trigger this so that my popup.html opens.

Xan
  • 74,770
  • 16
  • 179
  • 206
user3883991
  • 71
  • 2
  • 6

1 Answers1

4

You can't trigger keyboard shortcuts on your own.

Think of it this way as a timeline of events:

  1. A hardware event occurs, and is translated by OS into input event.
  2. If it's a global shortcut that Chrome registered, or if Chrome is focused, OS dispatches the event to Chrome.
  3. Chrome checks if it's a registered shortcut. If yes, the corresponding event is triggered.
  4. If it's not a shortcut, and a page is in focus, Chrome passes the event into the page as a DOM input event.
  5. If there is jQuery in the page, it converts that DOM event into its own internal event, matches the listeners and dispatches the internal event to them.

Your code only triggers 5 (internal jQuery events).
By directly manipulating DOM events, you can go up to 4.
Under no circumstance can an extension go back to 3 in this stack.

You're trying to open your popup in a roundabout way from code. Sadly, this is simply not possible at all for "ordinary" extensions - a conscious decision on part of Chrome dev team.

Think of another way to provide UI, say, notifications or injecting something in the current page with Content Scripts.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • I know it's an old Q&A but I was wandering how these extensions are doing it?: https://chrome.google.com/webstore/detail/duplicate-tab-shortcut/klehggjefofgiajjfpoebdidnpjmljhb and https://chrome.google.com/webstore/detail/new-tab-in-group-keyboard/hodhmojdnjljopdihkoniodkacnpimjf seems like it's possible in 2021 – Keval Langalia Feb 19 '21 at 01:41
  • @KevalLangalia They work by calling the APIs for those actions (chrome.tabs), not by sending keyboard events to the browser. You can ask Chrome to register your own keyboard shortcuts (you'll receive the event from the user) and then you can do whatever a regular extension can do. – Xan Feb 19 '21 at 07:46