命令是调用扩展程序功能的按键组合。注册命令
(位于清单中的 "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 示例并将其加载(解压缩)。