Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:23:36+00:00 2026-06-01T12:23:36+00:00

I have this content script that downloads some binary data using XHR, which is

  • 0

I have this content script that downloads some binary data using XHR, which is sent later to the background script:

var self = this;
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
  if (this.status == 200) {
     self.data = {
        data: xhr.response,
        contentType: xhr.getResponseHeader('Content-Type')
     };
  }
};
xhr.send();

... later ...
sendResponse({data: self.data});

After receiving this data in background script, I’d like to form another XHR request that uploads this binary data to my server, so I do:

var formData = new FormData();
var bb = new WebKitBlobBuilder();
bb.append(data.data);
formData.append("data", bb.getBlob(data.contentType));
var req = new XMLHttpRequest();
req.open("POST", serverUrl);
req.send(formData);

The problem is that the file uploaded to the server contains just this string: “[object Object]”. I guess this happens because ArrayBuffer type is lost somehow while transferring it from content process to the background? How can I solve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:23:37+00:00Added an answer on June 1, 2026 at 12:23 pm

    Messages passed between a Content Script and a background page are JSON-serialized.

    If you want to transfer an ArrayBuffer object through a JSON-serialized channel, wrap the buffer in a view, before and after transferring.

    I show an isolated example, so that the solution is generally applicable, and not just in your case. The example shows how to pass around ArrayBuffers and typed arrays, but the method can also be applied to File and Blob objects, by using the FileReader API.

    // In your case: self.data = { data: new Uint8Array(xhr.response), ...
    // Generic example:
    var example = new ArrayBuffer(10);
    var data = {
        // Create a view
        data: Array.apply(null, new Uint8Array(example)),
        contentType: 'x-an-example'
    };
    
    // Transport over a JSON-serialized channel. In your case: sendResponse
    var transportData = JSON.stringify(data);
    //"{"data":[0,0,0,0,0,0,0,0,0,0],"contentType":"x-an-example"}"
    
    // At the receivers end. In your case: chrome.extension.onRequest
    var receivedData = JSON.parse(transportData);
    
    // data.data is an Object, NOT an ArrayBuffer or Uint8Array
    receivedData.data = new Uint8Array(receivedData.data).buffer;
    // Now, receivedData is the expected ArrayBuffer object
    

    This solution has been tested successfully in Chrome 18 and Firefox.

    • new Uint8Array(xhr.response) is used to create a view of the ArrayBuffer, so that the individual bytes can be read.
    • Array.apply(null, <Uint8Array>) is used to create a plain array, using the keys from the Uint8Array view. This step reduces the size of the serialized message. WARNING: This method only works for small amounts of data. When the size of the typed array exceeds 125836, a RangeError will be thrown. If you need to handle large pieces of data, use other methods to do the conversion between typed arrays and plain arrays.

    • At the receivers end, the original buffer can be obtained by creating a new Uint8Array, and reading the buffer attribute.

    Implementation in your Google Chrome extension:

    // Part of the Content script
        self.data = {
            data: Array.apply(null, new Uint8Array(xhr.response)),
            contentType: xhr.getResponseHeader('Content-Type')
        };
    ...
    sendResponse({data: self.data});
    
    // Part of the background page
    chrome.runtime.onMessage.addListener(function(data, sender, callback) {
        ...
        data.data = new Uint8Array(data.data).buffer;
    

    Documentation

    • MDN: Typed Arrays
    • MDN: ArrayBuffer
    • MDN: Uint8Array
    • MDN: <Function> .apply
    • Google Chrome Extension docs: Messaging > Simple one-time requests
      “This lets you send a one-time JSON-serializable message from a content script to extension, or vice versa, respectively”
    • SO bonus: Upload a File in a Google Chrome Extension – Using a Web worker to request, validate, process and submit binary data.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JS script that changes content of webpages. I use this script
This is the script I have written for gzipping content on my site, which
this is what I have so far: <script> $(document).ready(function(){ $(.entry-content img:first).addClass('postimage'); }); </script> As
I have this menu structure that is used to navigate through content slider panels.
Hey everyone, I have written a script that downloads a zip file from a
I have a script that allows users to 'save as' a pdf, this is
I've written a PHP script that handles file downloads, determining which file is being
I'd like if someone could give me some advice on creating this script, which
I have a PHP script which downloads files to the client, in a new
I have a batch script that prompts a user for some input then outputs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.