onBeforeRequest Invalid request ID

I’m using onBeforeRequest to intercept page requests with “blocking” active. Frequently when I call browser.webRequest.filterResponseData(details.requestId it failes with the error ‘Invalid request ID’.

Why? And how should I handle that error?

I was just ignoring it, but I noticed that sometimes when I’m browsing with the extension active, certain requests can block for several minutes before returning. If I unload the extension, the requests resolve immediately. I’ve put in a bunch of debugging statement but can’t figure out what’s causing the requests to block, but I’m wondering if these Invalid request ID errors are related.

Maybe you are using async handler and call it “too late” which causes a race condition?

Checkout the second example in the docs, does that looks like your code? If not, what’s different?

I get the data to write from a promise, it looks like this:

filter.onstart = event => {
    getData.then((data) => {
        filter.write(data)
        filter.close()
    })
}

You are saying this is not allowed? Is there any way to block the request while I wait for a background task to complete?

For context, the idea is to replace third party API calls with calls to a different third party API. For example, if a webapp is making calls to Infura (an ethereum related service), the goal is to catch that request, make it to the user’s own node instead, and then serve the response from the user’s own node.

This is only possible if there is a way to block the filter while an async task completes.

I use this API fully synchronously, so it works for me fine, also I use ondata handler and not onstart.

But there are some async examples in the docs so I would say it’s possible, look here:

thanks for the docs, managed to get things working.

I think the big thing I was missing is that ondata needs to be set. I was using onstart, but that meant that data wasn’t always being intercepted. The race condition was that sometimes onstart would call filter.close() in time, but other times it would not.

Now that I’m calling ondata to catch all the data, it’s possible to do onstop asynchronously, which is allowing me to call filter.write in a .then() block without issues.

1 Like