I'm stuck trying to implement the Action command so that a keyboard shortcut will trigger the function inside background.js. The current code results in nothing happening when the keyboard shortcut is pressed.
Ideally the keyboard shortcut would trigger the function reddenPage inside background.js.
I assume some code needs to be placed in background.js, I'm just not sure where or what the code should be. Any help is much appreciated!
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
});
});