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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…