-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.js
More file actions
113 lines (100 loc) · 3.15 KB
/
storage.js
File metadata and controls
113 lines (100 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
StorageApi = {}
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
// try
// {
// var wordObj = {};
// wordObj[key] = JSON.stringify(value);
// chrome.storage.sync.set(wordObj);
// }
// catch(err){}
}
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
function synchronizeChrome(){
var wordCount = localStorage.length;
var listWords = {};
for(i = 0; i < wordCount; i++) {
var key = localStorage.key(i);
var wordObjStringification = localStorage.getItem(key);
listWords[key] = wordObjStringification;
}
//console.dir(listWords);
chrome.storage.sync.set(listWords, function(){
alert('synchonize sucessfull');
});
}
StorageApi.getAllWordFromLocalStorage = function(){
var wordCount = localStorage.length;
var datas = [];
var events = [];
for(i = 0; i < wordCount; i++) {
var key = localStorage.key(i);
var wordObj = localStorage.getObject(key);
wordObj.date = moment(wordObj.date).format();
wordObj.hideDate = moment(wordObj.date).format('YYYY-MM-DD');
if(wordObj.viewCount == undefined || wordObj.viewCount == null) {
wordObj.viewCount = 0;
}
if(wordObj.savedCount == undefined || wordObj.savedCount == null) {
wordObj.savedCount = 0;
}
datas.push(wordObj);
}
var now = moment();
return _.sortBy(datas, function(item){
return now - item.date;
});
};
StorageApi.getAllWordForDisplayingOnCalendar = function(datas){
if(datas == undefined || datas == null) {
datas = this.getAllWordFromLocalStorage();
}
return _.map(datas, function(item){
return {
title: item.text,
start: item.date
};
});
};
StorageApi.updateViewCountToLocalStorage = function(word){
var wordObj = localStorage.getObject(word);
wordObj.viewCount = (wordObj.viewCount == undefined ? 0 : wordObj.viewCount) + 1;
localStorage.setObject(word, wordObj);
return wordObj;
};
StorageApi.setWordGoogleImages = function (word, googleImages) {
var wordObj = localStorage.getObject(word);
wordObj.googleImages = googleImages;
localStorage.setObject(word, wordObj);
return wordObj;
};
StorageApi.setWordDictionaryData = function (word, wordDictionaryData) {
var wordObj = localStorage.getObject(word);
wordObj.wordDictionaryData = wordDictionaryData;
localStorage.setObject(word, wordObj);
return wordObj;
};
StorageApi.setWordUkDictionaryData = function (word, wordDictionaryData) {
var wordObj = localStorage.getObject(word);
wordObj.wordUkDictionaryData = wordDictionaryData;
localStorage.setObject(word, wordObj);
return wordObj;
};
StorageApi.setWordUsDictionaryData = function (word, wordDictionaryData) {
var wordObj = localStorage.getObject(word);
wordObj.wordUsDictionaryData = wordDictionaryData;
localStorage.setObject(word, wordObj);
return wordObj;
};
StorageApi.getWord = function (word) {
return localStorage.getObject(word);
};
StorageApi.setTranslateFromEnglishToVn = function(word, translatedWord){
var wordObj = localStorage.getObject(word);
wordObj.translateFromEnglishToVn = translatedWord;
localStorage.setObject(word, wordObj);
return wordObj;
}