通过快捷键激活扩展 [英] Activate extension via short cut key
问题描述
是否可以通过快捷键打开/启动Google Chrome扩展程序?例如,我想指定一个简短的缩写,例如 CTRL + E
来打开我的扩展并启动它。
以前是否有人这样做过? (5/24/2013):您可以使用新的 chrome。命令
API,它为键盘命令提供内置框架支持。
原始答案如下:
第1步:使用内容脚本将按键监听器绑定到每个页面。
//在内容脚本中,侦听Crtl + Shift + E(大写或小写)
document.documentElement.addEventListener(keypress,function(event){
if((e.keyCode == 69 || e.keyCode == 101)& amp; ;& e.ctrlKey&< e.shiftKey){
// d (下面的步骤2)
}
},true);
步骤2:对于内容脚本之外的操作,使用消息传递来通知后台页面快捷键已被按下,它应该执行一些action。
第3步:后台页面会执行一些操作。目前不可能(并且很可能永远不可能)到以编程方式打开浏览器操作弹出窗口,但您可以发送交互式桌面通知,打开一个新的标签,或做很多其他的事情。
请在下面的评论中查看他对安全的重要提示。]Is it possible to open/initiate a google chrome extension via a short cut key. For example I would like to assign a short cut of, lets say, CTRL + E
to open my extension and initiate it.
Has anyone done this before?
UPDATE (5/24/2013): You can use the new chrome.commands
API, which provides built-in framework support for keyboard commands.
Original answer follows:
Step 1: Use a content script to bind a keypress listener to every page.
// in the content script, listen for Crtl+Shift+E (upper or lowercase)
document.documentElement.addEventListener("keypress", function(event) {
if((e.keyCode == 69 || e.keyCode == 101) && e.ctrlKey && e.shiftKey) {
// do something (step 2, below)
}
}, true);
Step 2: For action outside of the content script, use message passing to notify the background page that the shortcut key has been pressed and it should perform some action.
Step 3: The background page does some action. It's currently not possible (and will likely never be possible) to open a Browser Action popup programatically, but you could send an interactive desktop notification, open a new tab, or do a lot of other things.
[EDIT: Code edited to include Rob W.'s suggestions; see his important notes on security in the comment below.]
这篇关于通过快捷键激活扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!