Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Chrome Extension Command Toggling

I'm writing a simple Chrome extension that intercepts media key events and interacts with a website.

I have it set up so that the extension can be toggled off and on by clicking it. While on, it monitors a tab and dispatches events to it when the media keys are pressed. When it's off, it still intercepts the media key presses, but doesn't do anything with them.

My problem is that I want to stop intercepting the media key presses when the extension is "off". I would like Chrome to utilize the media keys the way it normally would if my extension wasn't installed. I assume there is a way to stop intercepting the key presses, or to forward them to Chrome, but I can't figure out how.

Currently I'm using the manifest to intercept the key presses with this code:

{
    .... a bunch of stuff ...

    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "commands": {
        "playPause": {
            "suggested_key": {
                "default": "MediaPlayPause"
            },
            "global": true,
            "description": "Toggle play/pause"
        },
        "playNext": {
            "suggested_key": {
                "default": "MediaNextTrack"
            },
            "global": true,
            "description": "Play next track"
        },
        "playPrev": {
            "suggested_key": {
                "default": "MediaPrevTrack"
            },
            "global": true,
            "description": "Play previous track"
        }
    }
}

Then I'm catching the key presses with this:

function commandFired(command) {
    chrome.storage.local.get('activeTabId', result => {
        if (result.activeTabId == -1) {
            //send to chrome normally here
            return;
        }

        switch (command) {
            case "playNext":
                //Handling next
                break;
            case "playPrev":
                //Handling prev
                break;
            case "playPause":
                //Handling play
                break;
        }
    });
}

chrome.commands.onCommand.addListener(commandFired);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Chrome doesn't have an API to disable hotkeys programmatically so you'll have to unregister the entire service worker and offer a button in the popup to re-register it.

manifest.json:

  "action": {"default_popup": "popup.html"},

popup.html:

<label><input type=checkbox id=power>Enabled</label>
<script src=popup.js></script>

popup.js:

Object.assign(document.getElementById('power'), {
  checked: !!navigator.serviceWorker.controller,
  async onchange() {
    if (this.checked) {
      const bg = chrome.runtime.getManifest().background;
      await navigator.serviceWorker.register(bg.service_worker, {type: bg.type});
    } else {
      (await navigator.serviceWorker.getRegistration())?.unregister();
    }
  },
});

In case you want to keep listening to other chrome events, register a different bg2.js file after unregistering the main one. The main bg.js can import bg2.js to deduplicate the code, more info.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!