UltraInstinct
UltraInstinct

Reputation: 44444

Listen to KeyStrokes from all the pages in Google Chrome

What exactly I am trying to do is add on more shortcuts to Google Chrome browser. As of now I am using window.addEventListener('keyup', keyCheck, false);. I post a message then to the background page to carry out the relevant task.

I wonder if there is a way to achieve this when the current tab doesn't show any proper page (like newtab page, or extensions page, downloads page etc..)?

Upvotes: 4

Views: 1857

Answers (1)

Mohamed Mansour
Mohamed Mansour

Reputation: 40159

You currently cannot inject any scripts in chrome://* pages or about:* pages that includes newtab, extensions, version, etc.

An example on how you can do keyboard shortcuts would be something like this:

[source]

if (window == top) {
  window.addEventListener("keyup", keyListener, false);
}

// Keyboard keyup listener callback.
function keyListener(e) {
  // Must press ctrl key to validate.
  if (e.ctrlKey && e.keyCode && !e.metaKey) {
    chrome.extension.sendRequest({
      code: e.keyCode,
      alt: e.altKey,
      shift: e.shiftKey
    });
  }
}

You can override those pages, but that would be an ugly fix.

Upvotes: 4

Related Questions