5

I'm building a chrome extension and attempting to attach an event listener to this, but I'm not seeing anything in the console of the background page.

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
          console.log('REFERRER', request.ref);
        });

This code is in my main.js background page, all my other event listeners (chrome.tabs.onUpdated, chrome.extension.onMessage, etc) are all working fine though.

Robeezy
  • 2,424
  • 3
  • 22
  • 28
  • 3
    This question has been asked and answered before. See [Chrome extension: Communication between content script and background.html](http://stackoverflow.com/questions/11752341/chrome-extension-communication-between-content-script-and-background-html/11756188#11756188). – Rob W Dec 04 '12 at 21:54
  • Possible duplicate of [Chrome extension: Communication between content script and background.html](https://stackoverflow.com/questions/11752341/chrome-extension-communication-between-content-script-and-background-html) – Herohtar Feb 21 '18 at 23:20

1 Answers1

14

Yes, Request was deprecated in favor of 'Message'. So instead of onRequest you should use onMessage, and sendMessage as a replacement for sendRequest.

Stan
  • 8,683
  • 9
  • 58
  • 102
  • Thanks, I figured it might be since I couldn't find any actual documentation on it. It didn't end up working out for me though since I'm trying to access document.referrer in chrome.webRequest.onBeforeRequest and that happens before the content script can send the referrer to the background page. Working on a work-around though. – Robeezy Dec 04 '12 at 19:34
  • @rpod, You can post another question about webRequest. Before a web request there is no page where you can inject your content script, and I don't see any reason why you need to get this from a content script. You'll get http-headers just in your background page's `onBeforeSendHeaders` event handler. If you want to modify referrer, don't forget to add "webRequestBlocking" permission into your manifest. – Stan Dec 04 '12 at 20:31
  • what else was deprecated? – user65165 Nov 18 '16 at 23:48
  • @user65165 I'm afraid that many things were updated so you should consult with Google documentation. – Stan Nov 19 '16 at 14:43