I want to use a shortcut Ctrl + B to open popup.html of my extension, so I don't need to move the mouse and click.
I tried the commands config from Google document but it seems doesn't work. Does anyone know how to fix this? Below is my configuration (Note that I'm using manifest_version: 3)
manifest.json:
{
"name": "",
"description": "",
"version": "0.0.1",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html"
},
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+B",
"mac": "Ctrl+B"
}
}
}
}
popup.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
Hello world
</body>
</html>
background.js
// Currently empty
Don't know if this works for you, but if position of popup.html is not an issue for you this shall work if added to your background.js script. (executed on local host in manifest version 3)
manifest.json
"commands": {
"my_shortcut": {
"suggested_key": {
"default": "Ctrl+B",
"mac": "Command+B"
},
"description": "Opens popup.html",
"global": true
},
}
background.js
chrome.commands.onCommand.addListener((command) => {
if (command === 'my_shortcut')
chrome.system.display.getInfo({ singleUnified: true }, (info) => {
const wDimension = info[0].workArea;
const { top, left, height, width } = wDimension;
console.log(top);
const w = 440;
const h = 220;
const l = width / 2 - w / 2 + left;
const t = height / 2 - h / 2 + top;
const newWindow = () => {
console.log('in new window function');
};
chrome.windows.create(
{
url: 'popup.html',
type: 'popup',
width: w,
height: h,
left: Math.round(l),
top: Math.round(t),
},
newWindow
);
});
}