Rispondere ai comandi

I comandi sono combinazioni di chiavi che richiamano una funzionalità di estensione. Registra i comandi nel manifest sotto la chiave "commands". Ad esempio:

{
  "name": "Open developer.chrome.com",
  "version": "0.1",
  "manifest_version": 3,
  "description": "Opens developer.chrome.com when you use Cmd/Ctrl + Shift + Z",
  "background": {
    "service_worker": "background.js"
  },
  "commands": {
   "open-tab": {
     "suggested_key": {
       "default": "Ctrl+Shift+Z",
       "mac": "Command+Shift+Z"
     },
     "description": "Open developer.chrome.com"
   }
  }
}

Questa combinazione di chiavi attiva l'evento commands.onCommand nel service worker.

chrome.commands.onCommand.addListener((command) => {
  if (command !== "open-tab") return;
  chrome.tabs.create({ url: "https://developer.chrome.com" });
});

Per vedere come funziona la risposta ai comandi, scarica l'esempio di Tab Flipper e caricalo decompresso.