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