找到你要的答案

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

Q:保存日期值在本地存储从资源文件和background.js使用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.

我需要将特定操作的日期值从资源文件保存到本地存储中。日期值应与后台的当前日期进行定期比较。我在资源文件中使用的代码来存储日期值是

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

和我的代码用于检索保存日期从“JS”背景。

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

但我得到了空值的endate”。我怎样才能纠正它?任何帮助将不胜感激。

answer1: 回答1:

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

  • appAPI.db - a synchronous database for saving small data (integers, booleans, and short strings of approximately 10 characters)
  • appAPI.db.async - an asynchronous database for saving larger data

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]

的crossrider框架提供了2种类型的本地存储,如下:

  • appAPI.db - a synchronous database for saving small data (integers, booleans, and short strings of approximately 10 characters)
  • appAPI.db.async - an asynchronous database for saving larger data

该库是完全不同的,一个不能访问其他。因此,代码的问题在于它保存日期使用异步方法(异步。集)然后试图检索它使用同步方法(得到)。

由于数据很小,只需使用同步方法解决问题如下:

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");

【披露:我是一个crossrider员工]

google-chrome-extension  browser-extension  crossrider