Override the bookmark shortcut (Ctrl+D) function in Chrome

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:

Keyboard Shortcuts - Extensions and Apps

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)

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>.


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 Ctrl-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.