我一直在尝试实现Action 命令,以便键盘快捷键将触发background.js中的功能。当前代码在按下键盘快捷键时不会发生任何事情。
理想情况下,键盘快捷键会触发background.js中的函数reddenPage。
我假设一些代码需要放在background.js中,我只是不确定代码应该在哪里或应该是什么。任何帮助深表感谢!
Manifest.json
{
"name": "Page Redder",
"action": {},
"manifest_version": 3,
"version": "0.1",
"description": "Turns the page red when you click the icon",
"permissions": [
"activeTab",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+F",
"mac": "MacCtrl+Shift+F"
}
}
}
}
background.js
function reddenPage() {
document.body.style.backgroundColor = 'red';
}
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: reddenPage
});
});