问题
Is it possible to override the Ctrl+D? I want to, for example console.log
or something, and not add the links to the bookmarks.
回答1:
Shortcuts can be overridden using the chrome.commands API. An extension can suggest a default shortcut (eg. Ctrl+D) in the manifest file, but users are free to override this at chrome://extensions/
, as seen below:

Usage
This API is still under development and only available at the Beta and Dev channels, and the Canary builds More info. It will probably available to everyone starting at Chrome 24.
If you want to test the API in Chrome 23 or lower, add the "experimental" permission to the manifest file, and use chrome.experimental.commands
instead of chrome.commands
. Also visit chrome://flags/
and enable "Experimental Extension APIs", or start Chrome with the --enable-experimental-extension-apis
flag.
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"
}
}
}
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});
});
}
});
Documentation
- chrome.commands
- chrome.tabs (methods query and executeScript)
回答2:
It's not necessary to use chrome.commands
-- you can use a content script to trap the keydown
event, call preventDefault
and stopPropagation
on it, and handle it however you want. Example snippet that should work as part of a content script:
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && String.fromCharCode(event.keyCode) === 'D') {
console.log("you pressed ctrl-D");
event.preventDefault();
event.stopPropagation();
}
}, true);
The only things you can't override this way are window-handling commands, like ctrl-N
and ctrl-<tab>
.
回答3:
Alternative Solution: Type chrome:extensions into your browser's address bar. This will pull up the Chrome Extensions page.
Click on Keyboard Shortcuts in the top left menu ("Menu/Burger Button")
Assign Strg-D to a plugin that does not alter your Bookmarks.
This will solve the problem that an bookmark is created directly when you accidentally press Ctrl-D, instead the other Addon will pop up in that case.
来源:https://stackoverflow.com/questions/13429178/override-the-bookmark-shortcut-ctrld-function-in-chrome