回應指令

指令是叫用擴充功能功能的按鍵組合。註冊指令 "commands" 鍵底下。例如:

{
  "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"
   }
  }
}

這個鍵組合會觸發 commands.onCommand敬上 在 Service Worker

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

如要查看指令的回應情形,請下載 Tab Flipper 範例並在解壓縮後載入