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

bookmarks, google-chrome, google-chrome-extension, keyboard-shortcuts

Solution

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`)

Problem

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.

Original source