mozilla / webextension-polyfill

A lightweight polyfill library for Promise-based WebExtension APIs in Chrome
Mozilla Public License 2.0
2.61k stars 210 forks source link

Uint8Array gets converted into object when using with sendMessage/onMessage #643

Closed sharma66mahesh closed 5 days ago

sharma66mahesh commented 2 weeks ago

I send an object with the following code from the UI side as follows:

const result = await Browser.runtime.sendMessage({ action: 'GET_UINT8ARRAY' });
console.log(result)

Then I process this request on my background script and try to send back an uint8Array with the following code:

Browser.runtime.onMessage((payload) => {
  if(payload.action == 'GET_UINT8ARRAY') {
    return new Promise(resolve => resolve({ uint8Arr: new Uint8Array([1,2,3]) }  ))
  } 
})

Expected result:

{
  uint8Arr: [1, 2, 3]
}

Actual result received:

{ 
  uint8Arr: {
    0: 1,
    1: 2,
    2: 3
  }
}
Rob--W commented 2 weeks ago

This is not an issue with the polyfill but with Chrome. Chrome has a serialization mechanism that does a bit more than JSON serialization, but is less capable than structured cloning.

Support for that is tracked at https://issues.chromium.org/issues/40321352