Chrome命令API [英] Chrome commands API
问题描述
$ b
manifest.json
< pre $
{
name:示例扩展命令扩展名,
description:按Ctrl + Shift + F(Command + Shift + F on a Mac)打开浏览器动作弹出窗口,按Ctrl + Shift + Y发送一个事件(在Mac上Command + Shift + Y)。,
version:1.0,
manifest_version :2,
background:{
scripts:[background.js],
persistent:false
},
browser_action :{
default_popup:browser_action.html
},
commands:{
toggle-feature:{
suggested_key: {default:Ctrl + Shift + Y},
description:发送'toggle-feature'事件给扩展名
,
_execute_browser_action:{
suggested_key:{
default:Ctrl + Shift + F,
mac:MacCtrl + Shift + F
}
}
}
}
background.js
chrome.commands.onCommand.addListener(function(command){
console.log('onCommand event for message:',command);
});
非常简单,但侦听器回调没有被触发 - 控制台中没有输出,也没有任何错误。如果我使用其他API(例如选项卡),我的监听器会按照他们的要求进行触发,这只是命令API不适用于我。
评论者 rsanchez 提供了正确的答案:
您是否使用解包的扩展名?您需要删除并重新添加推荐的shorcut键的扩展名。
I've loaded this sample extension from Chrome docs which uses the commands API.
manifest.json
{
"name": "Sample Extension Commands extension",
"description": "Press Ctrl+Shift+F (Command+Shift+F on a Mac) to open the browser action popup, press Ctrl+Shift+Y to send an event (Command+Shift+Y on a Mac).",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_popup": "browser_action.html"
},
"commands": {
"toggle-feature": {
"suggested_key": { "default": "Ctrl+Shift+Y" },
"description": "Send a 'toggle-feature' event to the extension"
},
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+F",
"mac": "MacCtrl+Shift+F"
}
}
}
}
background.js
chrome.commands.onCommand.addListener(function(command) {
console.log('onCommand event received for message: ', command);
});
Very simple, yet the listener callback is not getting triggered - I get no output in the console, nor any errors. If I use other API, for example tabs, my listeners are getting triggered as they should, it's just the commands API that doesn't work for me.
Commenter rsanchez provides the correct answer:
Are you working with an unpacked extension? You need to remove and re-add the extension for suggested shorcut keys to be considered.
这篇关于Chrome命令API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!