Chrome命令API [英] Chrome commands API

查看:122
本文介绍了Chrome命令API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从使用命令API的Chrome文档中加载了此示例扩展程序。
$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆