-
Notifications
You must be signed in to change notification settings - Fork 0
/
saver.js
155 lines (131 loc) · 4.3 KB
/
saver.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
export function get_timestamp() { return new Date().toLocaleString(); }
export class GuacaConfig {
constructor() {
this.history = [];
this.url = (try_storage())? ((validate_storage())? false : true) : true;
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js");
}
}
// config's config interface
islsa() { return try_storage(); }
isurl() { return this.url; }
toggle_ls(sel_ls) {
if (sel_ls) {
if (this.url) {
if (try_storage()) {
this.url = false;
localStorage.setItem("guaca_cfgs", location.hash.substring(1));
location.hash = "";
return true;
} else { return false; }
} else { return true; }
} else {
if (this.url) { return true; }
else {
let cfgs = localStorage.getItem("guaca_cfgs");
this.clear_config();
this.url = true;
location.hash = cfgs;
return true;
}
}
}
copy_url() {
let cfg = location.href + ((this.url)? "" : localStorage.getItem("guaca_cfgs"));
navigator.clipboard.writeText(cfg);
window.alert("Url de configuration copiée !");
}
clear_url() { location.hash = ""; }
merge_url() {
let url_cfgs = location.hash.substring(1);
localStorage.setItem("guaca_cfgs", localStorage.getItem("guaca_cfgs") + "=" + url_cfgs);
let new_names = Array(url_cfgs.split("=").length).fill("");
new_names = JSON.stringify(JSON.parse(localStorage.getItem("guaca_names")).concat(new_names));
localStorage.setItem("guaca_names", new_names);
this.clear_url();
}
set_url() {
localStorage.setItem("guaca_cfgs", location.hash.substring(1));
localStorage.setItem("guaca_names", '[""]');
this.clear_url();
}
// config interface
clear_config() {
if (this.url) { location.hash = ""; } else {
localStorage.removeItem("guaca_cfgs");
localStorage.removeItem("guaca_names");
localStorage.removeItem("guaca_history");
}
}
has_config() {
if (this.url) {
let cfgs = location.hash.substring(1).split("=");
return {"has": cfgs[0] != "", "url": true, "cfgs": cfgs};
} else {
let cfgs = localStorage.getItem("guaca_cfgs").split("=");
let names = localStorage.getItem("guaca_names");
if (names == null) {
this.clear_config();
this.url = true;
location.hash = cfgs;
return {"has": cfgs[0] != "", "url": true, "cfgs": cfgs};
} else {
this.history = JSON.parse(localStorage.getItem("guaca_history"));
return {
"has": cfgs[0] != "", "url": false,
"cfgs": cfgs, "names": JSON.parse(names)
};
}
}
}
update_config(cfgs) {
if (this.url) { location.hash = cfgs.join("="); } else {
localStorage.setItem("guaca_cfgs", cfgs.join("="));
}
}
update_names(names) {
if (!this.url) { localStorage.setItem("guaca_names", JSON.stringify(names)); }
}
check_db_cfg() {
if (try_storage()) {
let url_cfgs = location.hash.substring(1).split("=")[0] != "";
let ls_cfgs = localStorage.getItem("guaca_cfgs");
ls_cfgs = (ls_cfgs == null)? false : ls_cfgs.split("=")[0] != "";
return url_cfgs && ls_cfgs;
} else { return false; }
}
// history interface
add_history(new_res) { this.history.push(new_res); this.update_history(); }
clear_history() { this.history = []; this.update_history(); }
copy_history(history) { this.history = history; this.update_history(); }
get_history() {
if (!this.url) {
this.history = JSON.parse(localStorage.getItem("guaca_history")); }
return this.history;
}
has_history() {
return (try_storage() && localStorage.hasOwnProperty("guaca_history"))? true : false;
}
update_history() {
if (!this.url) {
localStorage.setItem("guaca_history", JSON.stringify(this.history)); }
}
remove_history(id) { this.history.splice(id, 1); this.update_history(); }
}
function try_storage() {
let tryit = "try_lstest";
try {
localStorage.setItem(tryit, tryit);
localStorage.getItem(tryit);
localStorage.removeItem(tryit);
return true;
} catch(e) { return false; }
}
function validate_storage() {
return (
localStorage.hasOwnProperty("guaca_cfgs")
&& localStorage.hasOwnProperty("guaca_names")
&& localStorage.hasOwnProperty("guaca_history")
);
}