在Chrome扩展程序中触发快捷键 [英] Trigger shortcut in a Chrome extension
问题描述
我正在构建Chrome扩展程序,并将命令 _execute_browser_action
分配给 Alt + J 。我想在background.js中模拟这个,它使用
chrome.commands.onCommand.addListener(function(command ){/ * ... * /});
我想要 _execute_browser_action
通过不同的命令快捷方式,比如 Cmd + Shift + K
$ $ $ $ $ code $$$ {$ b $ _execute_browser_action{
建议_键:{
mac:Alt + J,
linux:Ctrl + Shift + J
}
},
asdf :{
suggested_key:{
default:Ctrl + Shift + K,
mac:Command + Shift + K
},
description:asdf
}
}
是我的background.js:
chrome.commands.onCommand.addListener(函数(command){
console.log ('onCommand event for message:',command);
if(command ===asdf){
alert(asdf);
var keyPress = jQuery.Event( keypress);
keyP ress.altKey = true;
keyPress.ctrlKey = false;
keyPress.which = 74;
//我如何触发这个?
}
});
我想知道如何触发此操作,以便我的popup.html打开。
您无法自行触发键盘快捷键。
方式作为事件的时间线:
- 发生硬件事件,并由OS转换为输入事件。
- 如果这是Chrome注册的全球快捷方式,或者Chrome专注于,则操作系统会将该事件分派给Chrome。
- Chrome会检查它是否为已注册的快捷方式。如果是,则触发相应的事件。
- 如果它不是快捷方式,且页面处于焦点状态,则Chrome将事件作为DOM输入事件传递到页面中。
- 如果页面中有jQuery,它会将该DOM事件转换为其自己的内部事件,匹配侦听器并将内部事件分发给它们。
您的代码仅触发5个(内部jQuery事件)。
通过直接操纵DOM事件,你可以上升到4.
在任何情况下,扩展都不能返回到3这个堆栈。
您试图以代码的迂回方式打开弹出窗口。不幸的是,这是根本不可能为普通扩展 - 对Chrome开发团队的一部分进行有意识的决定。
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.
You can't trigger keyboard shortcuts on your own.
Think of it this way as a timeline of events:
- A hardware event occurs, and is translated by OS into input event.
- If it's a global shortcut that Chrome registered, or if Chrome is focused, OS dispatches the event to Chrome.
- Chrome checks if it's a registered shortcut. If yes, the corresponding event is triggered.
- If it's not a shortcut, and a page is in focus, Chrome passes the event into the page as a DOM input event.
- 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.
这篇关于在Chrome扩展程序中触发快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!