google-chrome-extensionbrowser-extensioncrossrider

Save a date value in local storage from a resource file and retrieve from background.js using crossrider


I need to save the date value of a particular action into the local storage from a resource file. The date value should be compared with the current date in the background in regular intervals. the code i am using in the resource file to store the date value is

var x = new Date();
var enableDate = x.getFullYear()+"-"+(x.getMonth()+1)+"-"+x.getDate();
appAPI.db.async.set("ext_enable_date",enableDate);

and the code i used to retrieve the saved date from 'background.js' is

var enDate = appAPI.db.get("ext_enable_date");

But i get null value in 'enDate'. How can i get it correct? Any help would be appreciated.


Solution

  • The Crossrider framework provides 2 types of local storage, as follows:

    The storages are completely distinct and one cannot access the other. Hence, the problem with code is that it saves the date using an asynchronous method (async.set) and then tries to retrieve it using a synchronous method (get).

    As your data is small, simply resolve the issue by using the synchronous methods as follows:

    var x = new Date();
    var enableDate = x.getFullYear()+"-"+(x.getMonth()+1)+"-"+x.getDate();
    appAPI.db.set("ext_enable_date",enableDate);
    var enDate = appAPI.db.get("ext_enable_date");
    

    [Disclosure: I am a Crossrider employee]