Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arafathusayn/7139e9abb0230f67f42cc13cedef6c8b to your computer and use it in GitHub Desktop.
Save arafathusayn/7139e9abb0230f67f42cc13cedef6c8b 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