指令是叫用擴充功能功能的按鍵組合。在資訊清單中的 "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"
}
}
}
此鍵組合會觸發 Service Worker 中的 commands.onCommand
事件。
chrome.commands.onCommand.addListener((command) => {
if (command !== "open-tab") return;
chrome.tabs.create({ url: "https://developer.chrome.com" });
});
如要查看回應指令的實際情形,請下載 Tab Flipper 範例,並載入解壓縮後。