Chrome extension: identify web requests of background-page iframe? Chrome extension: identify web requests of background-page iframe? google-chrome google-chrome

Chrome extension: identify web requests of background-page iframe?


State of Affairs

Hi - struggling through a similar problem and here's what I've found:

The tabId and parentFrameId values look suspect.

Check out the documentation for the details object returned to WebRequest callback handlers:

  • tabId is set to -1 when there's no tab (like in a background page).
  • since extensions are in their own processes 0 indicates the parent is your top-level frame

Using chrome.tabs you can actually get a tabId for a background page but unfortunately, as you've found, WebRequest always gives -1 for a tabId

Generally, it seems as though WebRequest was going to be deprecated in favor of DeclarativeWebRequest but development of the latter is currently on pause.

Potential real solution:

I haven't tested this but it's possible that only requests made from your background page will be available to you and have a tabId of -1. You could potentially filter for these.

Hacky solution:

  1. Inject a frame onto an actual page within a tab with a known destination URL. The URL could even be a web-accessible URL within your extension to avoid collision.
  2. Extension-side keep track of the tabId and frameId that generated that request.
  3. Within the iframe load the actual page that you're interested in (either via a redirect or by having the content of the iframe be another iframe to the page).
  4. Use the stored tabId and frameId (available as the requests parentId) to decide which requests to filter.