Reputation: 6234
I am trying to write a chrome extension. I am facing some problems. I am unable to make the commands in chrome extension work. Below is given the code that I have written.
Here is my manifest.json
file.
{
"manifest_version": 2,
"name": "Test",
"description": "This is just a test",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"commands": {
"toggle-feature-foo": {
"suggested_key": {
"default": "Ctrl+Shift+1",
"mac": "Command+Shift+1"
},
"description": "Show Alert"
}
}
}
Here is the content.js
file.
alert("This is just a test 3");
chrome.commands.onCommand.addListener(function(command) {
alert('Command:', command);
});
The problem: I can see the first alert. But when I press Ctrl+Shift+1 I am unable to see the second alert. What am I doing wrong?
Upvotes: 0
Views: 177
Reputation: 7156
Content Script
cannot use chrome.* APIs
, with the exception of:
extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
https://developer.chrome.com/extensions/extension#method-getURLi18n
https://developer.chrome.com/extensions/i18nruntime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
https://developer.chrome.com/extensions/runtimestorage
https://developer.chrome.com/extensions/storageSolution:
Add your code in background page
or use message passing
https://developer.chrome.com/extensions/messaging
Upvotes: 2