This is a chrome extension which clears the input fields of the webpage. it works perfectly fine when I click at the extension.
I tried to give a keyboard shortcut to it. it was working at first but next day when I opened chrome extension was not there(maybe because I kept my extension in my chrome's profile folder at /home/user/.config/google-chrome/Profile 3/Extensions/
). so chrome might have not recognised and removed it because it's not from store.
I added it again but this time keyboard shortcut was not working. I don't have any knowledge of developing extensions. I just looked up at the google.developer guides and created this.
// manifest.json
{
"name": "Reset Answers",
"description": "Resets checkboxes, radio button, Input Text!",
"version": "0.0.0.1",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": ["activeTab", "scripting"],
"action": {},
"commands": {
"_execute_action": {
"suggested_key": "Alt C",
"description": "clears input fields"
}
},
"icons": {
"16": "/images/icon16.png",
"48": "/images/icon48.png",
"128": "/images/icon128.png"
}
}
// background.js
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: clear,
});
});
function clear() {
var elements = document.getElementsByTagName("input");
for (let i = 0; i < elements.length; i ) {
if (elements[i].type == "text" || elements[i].type == "number") {
elements[i].value = "";
} else if (elements[i].type == "radio" || elements[i].type == "checkbox") {
elements[i].checked = false;
}
}
}
chrome.commands.onCommand.addListener((command) => {
});
I tried calling clear
function inside that event listener function is also not working.
chrome://extension/shortcuts
page shows the extension and the shortcut like this but its not working.
Alt C
is not being used for any other keyboard shortcut on my pc. What I am doing wrong here?
CodePudding user response:
Certain operating system and Chrome shortcuts (e.g. window management) always take priority over Extension command shortcuts and can not be overwritten.
The following sample outputs to console.log when Ctrl Shift 1 is entered.
manifest.json
{
"name": "hoge",
"version": "1.0",
"manifest_version": 3,
"commands": {
"command1": {
"suggested_key": "Ctrl Shift 1",
"description": "hoge"
}
},
"background": {
"service_worker": "background.js"
}
}
background.js
chrome.commands.onCommand.addListener((c) => {
switch (c) {
case "command1":
console.log("command1");
break;
}
});