-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpearlstorage.js
82 lines (72 loc) · 2.04 KB
/
pearlstorage.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
var url;
debug = false
function setUrl(purl){
url = purl;
}
function saveValue(key,value){
try {
localStorage[key] = value;
} catch(e) {
if(debug) console.log("Error inside getItem() for key:" + key);
if(debug) console.log(e);
}
}
function loadValue(key){
try {
value = localStorage[key];
}catch(e) {
if(debug) console.log("Error inside getItem() for key:" + key);
if(debug) console.log(e);
}
return value;
}
function getKey(pearlType){
return (pearlType == "localpearls" ? url :
(pearlType == "domainpearls" ?
url.split(/\/+/g)[0] + "//" + url.split(/\/+/g)[1] :
"http?://*/*"))
}
function getPearls(pearlType){
var key = getKey(pearlType)
if(debug) console.log("Getting Pearl! " + pearlType + " key: " + key)
value = loadValue(key)
if(debug) console.log("Pearl caught! " + value)
return value ? value : "";
}
function getAllPearls(){
if(debug) console.log("Getting all pearls")
localPearls = getPearls("localpearls")
domainPearls = getPearls("domainpearls")
globalPearls = getPearls("globalpearls")
if(debug) console.log("All pearls caught")
localAndDomain = localPearls + ( localPearls.length > 0 ? "," : "") + domainPearls;
return localAndDomain + (localAndDomain.length > 0 ? "," : "") + globalPearls + "";
}
function storePearls(pearlType,value){
if(debug) console.log("Storing pearl: " + pearlType);
var key = getKey(pearlType)
saveValue(key,value)
}
function saveToggle(value){
saveValue('toggled',value)
}
function loadToggle(){
value = loadValue('toggled');
return value == "false" ? false : true;
}
function saveExact(value){
saveValue('exact',value)
}
function loadExact(){
value = loadValue('exact');
return value == "false" ? false : true;
}
// A function that gathers all saved values and returns them as json text
function getAllValuesJSON(){
var values = {};
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
values[key] = localStorage.getItem(key);
}
return JSON.stringify(values);
}