Chrome扩展程序错误与'onCommand' [英] Chrome extension error with 'onCommand'

查看:589
本文介绍了Chrome扩展程序错误与'onCommand'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



manifest.json在运行Chrome扩展程序时遇到错误Uncaught TypeError:无法读取属性'onCommand'undefined :

  {
name:Test,
description:Key command test 。,
version:1.0,
manifest_version:2,
content_scripts:[{
js:[background_test.js]] ,
matches:[http:// * / *,https:// * / *]
}],
commands:{
Ctrl + M:{
suggested_key:{
default:Ctrl + M,
mac:Command + M
},
description:Ctrl + M。

Ctrl-L:{
suggested_key:{
default:Ctrl + L,
mac:Command + L
},
description:Ctrl + L
}
}
}
pre>

background_test.js:

  chrome.commands.onCommand。 addListener(function(command){
if(command ===Ctrl + L){
console.log(Ctrl-L successful。);
}
else if(command ===Ctrl + M){
console.log(Ctrl + M successful。);
}
});

如果按下Ctrl-M并打印,则应打印Ctrl-M成功如果按下Ctrl-L,则Ctrl-L成功。



此问题似乎包含对此问题的答案,但我不明白答案,因为我没有足够的声誉,所以无法添加注释以要求进一步的解释:是您的 onCommand 在内容脚本中定义的监听器?它可能不会在那里工作;您需要将其包含在背景页面或操作弹出窗口中。我该如何在后台页面中定义 onCommand ?我无法在任何地方找到任何内容,无论是在API还是通过Google搜索。

我也尝试重新加载扩展并手动输入键盘快捷键,如,无济于事。



我在这里丢失了什么?

解决方案

chrome.commands content_scripts不可用(如 https://developer.chrome.com/extensions/content_scripts)。
$ b

为了让它工作,您可以将您的清单更改为:

 <$ c 
name:Test,
description:Key command test。,
version:1.0,
manifest_version :2,
权限:[
< all_urls>
],
background:
{
scripts:[background_test.js],
persistent:true
},
$ bcommands:{
Ctrl + M:{
suggested_key:{
default:Ctrl + M,
mac:Command + M
},
description:Ctrl + M。
},
Ctrl + L:{
suggested_key:{
default:Ctrl + L,
mac:Command + L
},
description:Ctrl + L
}
}
}
pre>

此外,Ctlr + L无法正常工作(至少在Mac上),因为Chrome已经使用它来专注于地址栏。



元素将在扩展的控制台中可见。要查看它,请打开chrome:// extensions /并单击Inspect视图:您的扩展的后台页面。


I'm getting the error "Uncaught TypeError: Cannot read property 'onCommand' of undefined" while running a Chrome Extension with the following content:

manifest.json:

{
  "name": "Test",
  "description": "Key command test.",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [ {
    "js": ["background_test.js"],
    "matches": [ "http://*/*", "https://*/*"]
  }],
  "commands": {
    "Ctrl+M": {
      "suggested_key": {
        "default": "Ctrl+M",
        "mac": "Command+M"
      },
      "description": "Ctrl+M."
    },
    "Ctrl-L": {
      "suggested_key": {
        "default": "Ctrl+L",
        "mac": "Command+L"
      },
      "description": "Ctrl+L"
    }
  }
}

background_test.js:

chrome.commands.onCommand.addListener(function(command) {
  if (command === "Ctrl+L") { 
    console.log("Ctrl-L successful.");
  }
  else if (command === "Ctrl+M") { 
    console.log("Ctrl+M successful.");
  }
}); 

All it's supposed to do is print "Ctrl-M successful" if Ctrl-M is pressed and print "Ctrl-L successful" if Ctrl-L is pressed.

This question appears to contain an answer to this problem, but I don't understand the answer and can't add a comment to ask for further explanation since I don't have enough reputation: "Is your onCommand listener defined in a content script? It probably won't work there; you need to include it in a background page or an action popup." How am I supposed to define onCommand in the background page?? I couldn't find anything on that anywhere, whether in the API or via Googling in general.

I also tried reloading the extension and manually inputting the keyboard shortcuts manually as suggested here, to no avail.

What am I missing here?

解决方案

The chrome.commands is not available by content_scripts (as defined in https://developer.chrome.com/extensions/content_scripts).

To get it working you can change your manifest to :

{
  "name": "Test",
  "description": "Key command test.",
  "version": "1.0",
  "manifest_version": 2,
  "permissions": [
    "<all_urls>"
  ],
  "background":
    {
    "scripts": ["background_test.js"],
    "persistent": true
    },

  "commands": {
    "Ctrl+M": {
      "suggested_key": {
        "default": "Ctrl+M",
        "mac": "Command+M"
      },
      "description": "Ctrl+M."
    },
    "Ctrl+L": {
      "suggested_key": {
        "default": "Ctrl+L",
        "mac": "Command+L"
      },
      "description": "Ctrl+L"
    }
  }
}

In addition Ctlr+L is not working (at least on Mac) as already used by chrome to get focus on adress bar.

The element will be visible in the console of the extension. To see it open chrome://extensions/ and click on the Inspect views: background page of your extension.

这篇关于Chrome扩展程序错误与'onCommand'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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