0

I need to get the HTTP response headers send by the server for the main frame in my Tampermonkey userscript. If I am correct there is no way to get the information directly from the userscript.

So, my idea was to write a Firefox webextension to catch the headers, using browser.webRequest.onHeadersReceived.addListener.

function headers_received(obj){
  console.log("headers_received["+obj.type+"] : " + obj.url);
  console.log(obj.responseHeaders);
}

browser.webRequest.onHeadersReceived.addListener(
  headers_received,
  { urls: ['<all_urls>'] },
  ['blocking', 'responseHeaders']
)

I could inject the data I need in the HTML document from the addon and get it back in my userscript, but I want to avoid modifying the DOM.

The touchy question is : Is there any way to store the data from the Firefox webextension and retrieve it inside the Tampermonkey userscript ?

I tried using this : https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage

function headers_received(obj){
  console.log("headers_received["+obj.type+"] : " + obj.url);
  console.log(obj.responseHeaders);
  let headers = {headers:obj.responseHeaders};
  browser.storage.local.set(headers);
}

But I can't retrieve the data from the Tampermonkey userscript.

  • You can add a cookie ([example](https://stackoverflow.com/a/45105934)), so your userscript will read it via `document.cookie`. – wOxxOm Apr 03 '23 at 18:51

0 Answers0