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

做~自己de王妃 提交于 2019-12-05 07:50:23
MrChrisRodriguez

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!