Sellcodes
  • Browse
    • Login
    • Sign Up
  • Users are requesting the codes...
  • See all requests

Can't store/retrieve an object using chrome.storage.sync in a chrome extension

I'm trying to store a Range object, created from the text that the user selects, using chrome.storage.sync with the page URL as the key. Since, there can be multi-selections associated with a page, I'm using an array to hold all the Range objects.

But I can't do it successfully. Every time I try to push a new Range object to the array, the previous objects suddenly become empty. Hence, I can only get an array in which all objects are empty except for the last Range object that I pushed.

This is my code snippet:

var selectedRange = window.getSelection().getRangeAt(0);
var key = request.url;
chrome.storage.sync.get([key], function(result){

/*check if an object is already present in storage with this key*/
if(Object.keys(result).length === 0 && result.constructor === Object){
console.log("No entry for " + key + " present in storage");

var entry = {};
/*create a new array with the first Range object*/
entry[key] = [selectedRange];
chrome.storage.sync.set(entry, function(){
console.log(entry);
});
}

else{
console.log("Entry found!");

var value = result[key];
/*push the Range object in the existing array*/
value.push(selectedRange);

chrome.storage.sync.remove(key, function(response){
var entry = {};
entry[key] = value;
chrome.storage.sync.set(entry, function(){
console.log(entry);
});
});
}
});

Here's the output from my console when I add 3 objects one after the other:

output from the console when I add Range objects

What's even more weird is that if I simply try to store a string instead of the Range object, say, by using this definition for selectedRange,

var selectedRange = "SomeString";

the storing works exactly as expected. The documentation for storage API says that it can store user data as objects.

I cant figure out the reason behind this strange behaviour. It would be really helpful if someone could help me figure that out or how I could get around this issue.

Thanks!

link-img

1

# Users who requested
the code (so far)

link-img

$5

Suggested price from buyers
at which they’d buy

link-img

$5

Potential revenues from requesters (actual
revenues can be significantly higher)

Time of last request:on Jun 25, 2018

Tags:javascript

Sellers

If you own such codes, please set up an offer and then invite users to it by clicking on «Invite to offer».

Invite to offer

Buyers

If you’re also be willing to buy those codes then click on «I’d also buy it» (no commitment to buy, but please only click when you’re serious about it).

I'd also buy it!

Tell people about Sellcodes and get 20-40% on every resulting sale!

Spread the word
  • PARTICIPATE
  • Buy
  • Offer
  • Share
  • LEARN
  • About
  • Blog
  • FAQ
  • CONNECT
  • Twitter
  • Facebook
  • Contact
  • LEGAL
  • Terms
  • Privacy
  • © 2025 Sellcodes
Log in to your Sellcodes account
Keep me logged in Forgot your password?
Looking to create an account?
Sign up for Sellcodes
By creating an account you agree to our Terms Of Use and Privacy Policy
Did you mean to Login?