Skip to content

Instantly share code, notes, and snippets.

@zdila
Created February 20, 2017 09:54
Show Gist options
  • Save zdila/ba6c0c1e0be5caafbc1d5bb4832f7d75 to your computer and use it in GitHub Desktop.
Save zdila/ba6c0c1e0be5caafbc1d5bb4832f7d75 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