sha*_*gon 10 bookmarks google-chrome keyboard-shortcuts google-chrome-extension
是否可以覆盖Ctrl+ D?我想,例如console.log或某些东西,而不是添加到书签的链接.
可以使用chrome.commandsAPI 覆盖快捷方式.扩展可以在清单文件中建议默认快捷方式(例如,Ctrl + D),但是用户可以自由地覆盖它chrome://extensions/,如下所示:

此API仍处于开发阶段,仅在Beta和Dev频道上可用,而Canary构建了 更多信息.它可能适用于从Chrome 24开始的每个人.
如果您想在Chrome 23或更低版本中测试API,请将"实验"权限添加到清单文件中,而chrome.experimental.commands不是使用chrome.commands.另请访问chrome://flags/并启用"Experimental Extension API",或使用该--enable-experimental-extension-apis标志启动Chrome .
manifest.json{
"name": "Remap shortcut",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
],
"commands": {
"test-shortcut": {
"suggested_key": {
"default": "Ctrl+D",
"mac": "Command+D",
"linux": "Ctrl+D"
},
"description": "Whatever you want"
}
}
}
Run Code Online (Sandbox Code Playgroud)
background.js// Chrome 24+. Use chrome.experimental.commands in Chrome 23-
chrome.commands.onCommand.addListener(function(command) {
if (command === 'test-shortcut') {
// Do whatever you want, for instance console.log in the tab:
chrome.tabs.query({active:true}, function(tabs) {
var tabId = tabs[0].id;
var code = 'console.log("Intercepted Ctrl+D!");';
chrome.tabs.executeScript(tabId, {code: code});
});
}
});
Run Code Online (Sandbox Code Playgroud)
没有必要使用chrome.commands——您可以使用内容脚本来捕获keydown事件、调用它preventDefault并stopPropagation根据需要处理它。应该作为内容脚本的一部分工作的示例片段:
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && String.fromCharCode(event.keyCode) === 'D') {
console.log("you pressed ctrl-D");
event.preventDefault();
event.stopPropagation();
}
}, true);
Run Code Online (Sandbox Code Playgroud)
您不能以这种方式覆盖的唯一内容是窗口处理命令,例如ctrl-N和ctrl-<tab>。
| 归档时间: |
|
| 查看次数: |
5692 次 |
| 最近记录: |