-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wedata.js
89 lines (82 loc) · 2.44 KB
/
wedata.js
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
// wedata utility for Greasemonkey
// usage
/*
var DATABASE_URL = 'http://wedata.net/databases/XXX/items.json';
var database = new Wedata.Database(DATABASE_URL);
database.get(function(items) {
items.forEach(function(item) {
// do something
});
});
// clear cache
GM_registerMenuCommand('XXX - clear cache', function() {
database.clearCache();
});
*/
var Wedata = {};
Wedata.Database = function (urls) {
this.items = [];
this.expires = 24 * 60 * 60 * 1000; // 1 day
this.urlSet = urls;
this.url = this.urlSet.shift();
};
Wedata.Database.prototype.get = function (callback) {
var self = this;
var cacheInfo = Wedata.Cache.get(self.url);
function getWeData(dataURL) {
GM_xmlhttpRequest({
method: "GET",
url: dataURL,
onload: function (res) {
if (res.status === 200) {
self.items = JSON.parse(res.responseText);
callback(self.items);
Wedata.Cache.set(dataURL, self.items, self.expires);
} else {
var rawData = Wedata.Cache.getRawData(dataURL);
if(typeof rawData !== "undefined") {
self.items = rawData;
callback(self.items);
}else{
self.url = self.urlSet.shift();
self.get(callback);
}
}
},
onerror: function (res) {
GM_log(res.status + ":" + res.message);
}
});
}
if (cacheInfo) {
self.items = cacheInfo;
callback(self.items);
} else {
getWeData(self.url);
}
};
Wedata.Database.prototype.clearCache = function () {
Wedata.Cache.set(this.url, null, 0);
};
Wedata.Cache = {};
Wedata.Cache.set = function (key, value, expire) {
var expire = new Date().getTime() + expire;
GM_setValue(key, JSON.stringify({ value: value, expire: expire }));
};
Wedata.Cache.get = function (key) {
if (!Wedata.Cache.isExpired(key)) {
return Wedata.Cache.getRawData(key);
}
};
Wedata.Cache.getRawData = function (key) {
var cached = JSON.parse(GM_getValue(key, null));
if (cached) {
return cached.value;
} else {
return null;
}
};
Wedata.Cache.isExpired = function (key) {
var cached = JSON.parse(GM_getValue(key, null));
return !(cached && cached.expire > new Date().getTime());
};