I am trying to store a Range
object, created from the text that user has selected, using chrome.storage.sync
with the page URL as the key. Since, there can be multiple selections associated with a particular page, I am using an array to hold all the Range
objects.
But I am unable to do it successfully. Each time I try to push a new Range
object to the array, the previous objects become empty. Hence, I 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:
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.
EDIT:
With the help of @wOxxOm , I have been able to locate the problem to the fact that chrome.storage
api can only store JSONifiable objects but Range
object is not JSONifiable.
I thought about creating a new object and storing just the startContainer
, startOffset
, endContainer
and the endOffset
properties. Then, I would've been able to recreate the Range
object using the range.setStart(startNode, startOffset)
and range.setEnd(endNode, endOffset)
methods.
But then I found that even the Node
objects are not JSONifiable! :(
It would be great if you could tell me a fix around this problem.
Thanks!