【发布时间】:2021-07-04 16:39:10
【问题描述】:
我一直在尝试实现Action command,以便键盘快捷键将触发 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
});
});
【问题讨论】:
标签: javascript google-chrome google-chrome-extension chrome-extension-manifest-v3