-
Notifications
You must be signed in to change notification settings - Fork 0
/
aubreyDB.js
60 lines (51 loc) · 1.55 KB
/
aubreyDB.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
//localStorage persistance layer
var aubreyDB = {
DBname:"aubrey-plugins",
_init:function(){
localStorage.setItem(this.DBname,"[]");
},
_getItems:function(){
if(!localStorage.getItem(this.DBname)){
return null;
}
return JSON.parse(localStorage.getItem(this.DBname));
},
_setItems:function(items){
if(typeof items != "object") {
throw "attempted attempt prevented";
}
localStorage.setItem(this.DBname,JSON.stringify(items));
},
_evalPlugin:function(plugin){
eval(plugin.body);
aubreyAddPlugin(p);
console.log("Loaded script " + plugin.body);
},
//------------------------public functions------------------------//
boot:function(){
if(!this._getItems()){
this._init();
console.log("Init complete");
}
var items = this._getItems();
console.log("Loading saved plugins");
for (var i = 0; i < items.length; i++){
this._evalPlugin(items[i]);
}
console.log("Boot complete!")
},
loadPlugin:function(plugin){
var items = this._getItems();
//Checks for duplicate plugins
for (var i = 0; i < items.length;i++) {
if(plugin.name == items[i].name){
console.log("Found duplicate plugin: " + plugin.name);
return;
};
}
this._evalPlugin(plugin);
console.log("Storing plugin");
items.push(plugin);
this._setItems(items);
}
};