Trigger shortcut in a Chrome extension

后端 未结 1 1764
轻奢々
轻奢々 2020-12-06 21:41

I am building a Chrome extension, and assigned command _execute_browser_action to Alt+J. I want to simulate this in background.js which li

相关标签:
1条回答
  • 2020-12-06 22:39

    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.

    0 讨论(0)
提交回复
热议问题