-1

I have the following manifest file:

{
    "manifest_version": 2,
    "permissions": [
        "http://*/*",
        "https://*/*"
    ],
    "background": {
        "scripts": [
            "./src/background.js"
        ]
    }
}

In my background script, I have the following code:

console.log('I am here');
chrome.extension.onRequest.addListener((request, sender, response) => {
    console.log(request, sender, response);
});

When I load a page, I would expect the console.log inside of the listener to fire, but nothing happens. Though I do get the output I am here.

What I am trying to do is capture a response from a URL which contains json. I then want to get specific data from the json and send it to my server.

Maybe using chrome.extension.onRequest isn't the best way to do this. What should I be doing?

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • 2
    That's for messages sent via `chrome.runtime.sendMessage`. I don't understand what "URL which contains json" is, and how/where the URL request initiated. – wOxxOm Feb 28 '17 at 22:20
  • Your code appears to have nothing to do with what your question is titled, nor is there anything which explains why you would expect your listener to fire. Please [edit] the question to be more clear as to what you are actually trying to do. – Makyen Feb 28 '17 at 23:12
  • I suggest you read the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (perhaps along with the pages linked from the overview). The [architecture section](https://developer.chrome.com/extensions/overview#arch) has overall architecture information which should help your understanding of how things are generally organized/done. You will probably also want to read [Content Scripts](https://developer.chrome.com/extensions/content_scripts), and [Message Passing](https://developer.chrome.com/extensions/messaging). – Makyen Feb 28 '17 at 23:12

1 Answers1

0

onRequest is deprecated, instead try using chrome.runtime.onMessage.

Use onMessage like so: chrome.runtime.onMessage.addListener(function callback)

Fired when a message is sent from either an extension process (by runtime.sendMessage) or a content script (by tabs.sendMessage).

callback is as follows:

function(any message, MessageSender sender, function sendResponse) {...};

More info can be found on the developer.chrome.com website:

https://developer.chrome.com/extensions/runtime#method-sendMessage

Justin Taddei
  • 2,142
  • 1
  • 16
  • 30
  • Using that instead doesn't work. I still get the same affect – Get Off My Lawn Feb 28 '17 at 22:03
  • You need to call [`sendMessage`](https://developer.chrome.com/extensions/runtime#method-sendMessage) also. – Justin Taddei Feb 28 '17 at 22:04
  • And `onMessage` is deprecated too. You need [`runtime.onMessage`](https://developer.chrome.com/extensions/runtime#event-onMessage). I will update the answer to reflect this. – Justin Taddei Feb 28 '17 at 22:07
  • Okay, so using this update how do I capture network data from pages? – Get Off My Lawn Feb 28 '17 at 22:11
  • 2
    There is no API to capture incoming network traffic in the background page. You can capture headers. Or you can spoof XMLHttpRequest inside a [page-level script](https://stackoverflow.com/a/9517879) inserted from a content script. I think I've seen a couple of answers here. – wOxxOm Feb 28 '17 at 22:22