Question

I'm working on a Chrome extension and I've the following problem. When a certain event happens, my background page creates a new tab (page) with chrome.tabs.create API and send an Object.

The sent Object (called items) is a list of objects, having a particular class (prototype) called Item.

Here some code:

// in background.html
chrome.tabs.create({index: 0, url: "results.html"}, function(tab) {
     chrome.tabs.sendRequest(tab.id, {'items': itemsList}, function(response) {
         console.log(response.farewall);
     });
});     

On the other hand, in the newly created page, I receive the sent objects

// newpage.html
chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        console.dir(request.items);
        sendResponse({});
    }                   
);

The problem is that, when I receive the objects list, in the newpage.html, the Object type is lost. Indeed using console.dir() in the new background page, the objects type in the itemsList is correctly reported, but not in the received items list object in newpage.html.

I could manually serialize the data in he background.html manually through a string, and manually de-serialize in newpage.html but I would like to know if there is a better way to afford this and prevent that objects type (namely Item) in the list is lost.

Was it helpful?

Solution

When passing an object through a request, Chrome requires it to be "JSON-serializable", which hints that it is encoded to JSON string before the transfer, transferred as a string, and then decoded back. And JSON doesn't support function serialization.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top