Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stnc/54815b1fe9e43c5dfd7ed49d8a465d0f to your computer and use it in GitHub Desktop.
Save stnc/54815b1fe9e43c5dfd7ed49d8a465d0f to your computer and use it in GitHub Desktop.
How to send messages from service worker to the chrome extension without having the webapp open

In the background script we have:

chrome.webRequest.onBeforeRequest.addListener(function (details) {
  if (details.method === 'HEAD') {
    if (details.url.endsWith('?start')) {
      ringSound.play();
    } else if (details.url.endsWith('?stop')) {
      ringSound.pause();
      ringSound.currentTime = 0;
    }
  }
}, { urls: [ 'https://*.gruveo.com/api/ring?*' ] } );

In the webapp we do:

fetch('https://www.gruveo.com/api/ring?' + (start ? 'start' : 'stop'), { method: 'HEAD' }).catch(function () {});

You can get our extension from https://chrome.google.com/webstore/detail/gruveo-instant-connect/jlongfhigcebblbebgmpobcgjhlhegcc and analyze it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment