Permissions
When the "activeTab" permission is declared in manifest.json, it does not mean that this permission is granted at any time and anywhere. The following conditions must be met:
- The user performs an action. (Extension icon in Google Chrome toolbar)
- The user operates the right-click menu. (contextmenu)
- The user executes a keyboard shortcut through the commands API.
- The user accepts a suggestion through the omnibox API. (Address bar)
For example, you create a side panel with a button, and you want to take a screenshot when the user clicks the button. This will not work because the side panel is not in the above conditions. If you move the button to the right-click menu (contextmenu), and the user takes a screenshot after clicking the right-click menu, it will work.
Sending messages
First, there are two methods for sending messages: chrome.runtime.sendMessage, chrome.tabs.sendMessage. You may not notice that there are two methods when you look at the documentation.
- chrome.tabs.sendMessage, sends a message to the
content script
of the tab. - chrome.runtime.sendMessage, sends a message to other components or background scripts,
content script
cannot receive messages sent by it. - There is only one method to receive messages: chrome.runtime.onMessage.