Acessing Network Request and Response

386 views
Skip to first unread message

Oluwatobi Giwa

unread,
Aug 10, 2023, 9:42:07 AM8/10/23
to Chromium Extensions

Am trying to get all the sub-resources or urls or fetch/xhr (i hope am communicating well) from a webpage.

I've tried using the chrome.webRequest API but it did'nt get me all the sub-resources/fetch/xhr called on the page unlike when i tried using chrome.debugger which gets me exactly what I want, allow me to showcase the code.


Here is the same question on,  Stack Overflow

SOLUTION 1

chrome.webRequest.onBeforeRequest.addListener(details) => { console.log("all resource URL:", details.url, details.initiator"\n"); }, { urls: ["<all_urls>"] } )


SOLUTION 2

chrome.action.onClicked.addListener((tab) => { console.log("action button has been clicked\n") chrome.debugger.attach({ tabId: tab.id }, '1.0'() => {  chrome.debugger.sendCommand({ tabId: tab.id }, 'Network.enable', {}, () => {  chrome.debugger.onEvent.addListener((source, method, params) => { if (source.tabId === tab.id && method === 'Network.responseReceived') {  if (params.response.mimeType === 'application/json' && params.response.url.includes("url")) { console.log(params.response"\n"console.log(params.response.url)


And yes, unlike the webRequest version I have narrowed my search in the chrome.debugger to json responses, which is actually what I want but the debugging warning would not be nice for the users. So here are my troubles;

  1. Is there a way, still using the working solution of chrome.debugger to get away from the warnings, cos that essentially the problem with that approach. I don't suppose the users would like that, right?

2a) I prefer the chrome.webRequest approach, very clean, but how do i make it give me all fetch/xhr like i see on my network tab?

2b) I did notice it fetched me a url. Which is actually a js script, which is the initiator for the url i want, how to i get down into the script to get that particular url. I saw it in the "Request Iniator chain" of the js script.

wOxxOm

unread,
Aug 10, 2023, 12:07:35 PM8/10/23
to Chromium Extensions, Oluwatobi Giwa
The only solution is to inject the code in MAIN world of the web page and override things like `Response.prototype.text` or `XMLHttpRequest.prototype.send`, there are many examples around, here's one in https://stackoverflow.com/a/48134114, you can also inspect/debug other similar extensions in devtools.

1) The debugger warning can be removed only by adding a command-line switch for Chrome binary which is inconvenient in general and highly unsafe in this particular case as the debugger permission gives a lot of power to extensions.

2) No, webRequest can't do it, https://crbug.com/487422
Reply all
Reply to author
Forward
0 new messages