0

In a Firefox browser extension I am writing, I wish to make a request from a content script in the context of a page (same cookies and authorizations, no CORS issues…). This section of the MDN mentions that I can use content.fetch and content.XMLHttpRequest to do that, though I can find no documentation on those or on content itself. When getting an ArrayBuffer from the response of content.fetch, I obtain a value which looks and acts like an ArrayBuffer but is not instanceof ArrayBuffer.

My assumption is that I am somehow dealing with an ArrayBuffer from the page context in the context of my content script, and that this value would have a different ArrayBuffer constructor. Is this something I can fix by messing with wrappedJSObject or something else? Or am I dealing with a browser bug?

More concretely, the following code behaves unexpectedly. The issue occurs even when this is the only code in the content script.

(async function () {
  const response = await content.fetch("./");
  console.log("response type", response instanceof Response);
  const blob = await response.blob();
  console.log("blob type", blob instanceof Blob);
  const arrayBuffer = await blob.arrayBuffer();
  console.log("array buffer", arrayBuffer);
  console.log("array buffer type", arrayBuffer instanceof ArrayBuffer);
})();

I expect this code to print this. (Last word is true.)

response type true
blob type true
array buffer ArrayBuffer { byteLength: … }
array buffer type true

In practice I get this. (Last word is false.)

response type true
blob type true
array buffer ArrayBuffer { byteLength: … }
array buffer type false
Victor Schubert
  • 323
  • 3
  • 8
  • Instead of `instanceof`, use [`arrayBuffer.constructor.name`](https://stackoverflow.com/a/55271454/10210841) – Rojo Jan 26 '21 at 19:10
  • Thanks @Rojo, this is how I make my extension work despite this issue but the `instanceof` check happens in a dependency and I’d much rather have a fix in my code than maintain a patch for said dependency. Also I’d very much like to understand the issue. – Victor Schubert Jan 26 '21 at 19:16
  • Well I meant to say that see what happens when you try `arrayBuffer.constructor.name`. Just try it, and tell me the output – Rojo Jan 26 '21 at 19:17
  • Oh sorry I misunderstood. `arrayBuffer.constructor.name` gives the `ArrayBuffer` string. – Victor Schubert Jan 26 '21 at 22:28
  • Huh. Your best bet is to ask in the [JS chat room](https://chat.stackoverflow.com/rooms/17/javascript). Obviously by `arrayBuffer.constructor.name` your variable is an ArrayBuffer. If your code properly compiles with what you have, then I don't see why you should worry. – Rojo Jan 27 '21 at 13:53

0 Answers0