Chrome Extension Shortcuts

Omri Aharon

I'm writing a simple extension that has a .js file with only 2 functions and a listener (which only writes to Console - but I can't see it and not sure it catches anything).

I'd like to have shortcuts assigned, let's say Ctrl+Shift+1 and Ctrl+Shift+2 to both functions.

I'm not exactly sure how to do it, and if it's possible. The data I've been able to gather online didn't cover what I'm looking for.

I have no need for html popup. All I want is to press the shortcut and have the correspondent javascript function execute.

This is how my manifest.json file looks like:

{
  "manifest_version": 2,

  "name": "Simple Extension",
  "description": "This extension barely does anything.",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html" //not sure I even need this
  },

  "background": {
    "scripts": ["popup.js"]
  },

  "commands": {
      "funcA": {
        "suggested_key": {
          "default": "Ctrl+Shift+1"
        },
        "description": "Executes Func A"
      },
      "funcB": {
        "suggested_key": {
          "default": "Ctrl+Shift+2"
        },
        "description": "Executes Func B"
      }
   }
}

After installing the extension I saw on 'Keyboard Shortcuts' on the Extension tab that the shortcuts assignment worked well, but it's unclear to me what binds the shortcut to the javascript function itself.

Do I have to use a listener and then do the call manually ?

Edit:

This is the listener in my .js file:

chrome.commands.onCommand.addListener(function(command) {
        console.log('Command:', command);
});
Rob W

Register an event listener for the chrome.commands.onCommand event in the background or event page.

chrome.commands.onCommand.addListener(function(command) {
    // Now, do whatever you want when the shortcut is triggered.
    // e.g. if (command == 'funcA') ...
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

「コマンド」(ショートカット): `chrome:// extension / shortcuts`で並べ替えることはできますか?

分類Dev

chrome extension - restriction on Chrome version

分類Dev

chrome extension - restriction on Chrome version

分類Dev

Chrome Extension : Issue with SendMessage

分類Dev

Chrome Extension captureStream

分類Dev

Chrome extension options

分類Dev

Chrome Extension - Find out if extension tab is open

分類Dev

Chrome Extension detect button inside extension

分類Dev

chrome object properties undefined for Chrome Extension

分類Dev

Chrome Extension causes Chrome to become unusably slow

分類Dev

Using Chrome text-to-speech in a chrome extension

分類Dev

Chrome Extension example of oAuth 2.0

分類Dev

Chrome extension setTimeout not working properly

分類Dev

Chrome Extension cannot connect to websocket

分類Dev

Writing chrome extension in C#?

分類Dev

Chrome extension badge text - ✔ renders as ✔

分類Dev

Saving values to Textbox in Chrome Extension

分類Dev

post data from chrome extension

分類Dev

Chrome extension popup close event

分類Dev

Simple highlight text chrome extension

分類Dev

How to create hidden chrome Extension

分類Dev

Mocha Test, Yeoman, Chrome Extension

分類Dev

webstorm 10 chrome extension debugging

分類Dev

Google Chrome no longer allows me to create shortcuts to open tabs as windows

分類Dev

Modify HTTP responses from a Chrome extension

分類Dev

Is it possible to send an array with the Postman Chrome extension?

分類Dev

getSelection.getRangeAt(0) in Google Chrome Extension

分類Dev

Chrome Extension - Javascript Origins for Google API

分類Dev

Pass data from chrome extension to webpage

Related 関連記事

ホットタグ

アーカイブ