-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
329 lines (295 loc) · 9.7 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//Intial function
"Use strict";
(async function() {
try {
$(".dropdown").dropdown();
$("#uploadmodal").modal();
$("#feedbackmodal").modal();
$("#aboutmodal").modal();
$("#settingsmodal").modal();
$("#authorise").click(function() {
authorise();
});
$("#refresh").click(function() {
getWebCards();
});
$("#feedback").click(function() {
$("#feedbackmodal").modal("open");
});
$("#about").click(function() {
$("#aboutmodal").modal("open");
});
$("#settings").click(function() {
$("#settingsmodal").modal("open");
});
$("#upload").click(function() {
checkForms();
});
$("#submit-feedback").click(function() {
sendFeedback();
});
$("#edit-config").click(function() {
editConfig();
});
const app = {
name: "Listy",
id: "joe",
version: "1",
vendor: "joe.listy"
};
let appHandle = await window.safeApp.initialise(app);
auth = await window.safeApp.connect(appHandle);
Materialize.toast(" App Token: " + auth, 3000, "rounded");
getWebCards();
} catch (err) {
console.error(err);
} finally {
authorised = false;
}
})();
async function getWebCards() {
try {
let listyHash = await window.safeCrypto.sha3Hash(auth, "listy");
let listyHandle = await window.safeMutableData.newPublic(auth, listyHash, 54321);
let entriesHandle = await window.safeMutableData.getEntries(listyHandle);
loadingMessage.innerHTML = "";
webCards.innerHTML = "";
let time = new Date().getTime();
var safeurls = [];
function checkUrls(item) {
if (item == this) {
return false;
} else {
return true;
}
}
window.safeMutableDataEntries.forEach(entriesHandle, (key, value) => {
if (
parseInt(uintToString(key)) < time &&
parseInt(uintToString(key)).toString().length === 13 &&
uintToString(key).length === 13
) {
var webCardItems = JSON.parse(uintToString(value.buf));
var title = webCardItems.title
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
var description = webCardItems.description
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
var url = webCardItems.url;
var keys = Object.keys(webCardItems);
if (
keys.length === 3 &&
keys[0] === "title" &&
keys[1] === "description" &&
keys[2] === "url" &&
safeurls.every(checkUrls, [url]) === true
) {
safeurls.push(url);
if (
title.length !== 0 &&
title.length < 51 &&
typeof title === "string" &&
description.length !== 0 &&
description.length < 300 &&
typeof description === "string" &&
url.substring(0, 7) === "safe://" &&
url.length < 100 &&
typeof url === "string"
) {
window.safeApp.webFetch(auth, url, { range: { start: 0, end: 1 } }).then(
data => {
$("#webCards").append(
'<div class="card-panel dark-primary-colour item"><a class="tooltipped accent-text-colour" data-position="bottom" data-tooltip="' +
url +
'" href="' +
url +
'" >' +
title +
'</a><p align="left" class="accent-text-colour description">' +
description +
"</p></div>"
);
$(".tooltipped").tooltip();
},
err => {
console.log("No website found at " + url);
console.error(err);
}
);
}
}
}
});
window.safeMutableDataEntries.free(entriesHandle);
window.safeMutableData.free(listyHandle);
} catch (err) {
console.error(err);
}
}
async function authorise() {
try {
if (authorised !== true) {
window.safeApp.free(auth);
const app = {
name: "Listy",
id: "joe",
version: "1",
vendor: "listy.joe"
};
const permissions = {
_public: ["Read"]
};
const owncontainer = {
own_container: true
};
let appHandle = await window.safeApp.initialise(app);
let authURI = await window.safeApp.authorise(appHandle, permissions, owncontainer);
let authorisedAppHandle = await window.safeApp.connectAuthorised(appHandle, authURI);
auth = authorisedAppHandle;
authorised = true;
Materialize.toast("Authorised App Token: " + auth, 3000, "rounded");
setTimeout(function() {
getConfig();
}, 1600);
return auth;
}
} catch (err) {
console.error(err);
}
}
function checkForms() {
if (
title.value.length !== 0 &&
title.value.length < 51 &&
description.value.length !== 0 &&
description.value.length < 301 &&
service.value.length !== 0 &&
publicID.value.length !== 0
) {
uploadWebCard();
} else {
Materialize.toast("Make sure all fields are filled and don't exceed limits ", 3000, "rounded");
}
}
async function uploadWebCard() {
try {
if (authorised !== true) {
const auth = await authorise();
}
let listyHash = await window.safeCrypto.sha3Hash(auth, "listy");
let listyHandle = await window.safeMutableData.newPublic(auth, listyHash, 54321);
let mutationHandle = await window.safeMutableData.newMutation(auth);
let time = new Date().getTime();
let url = "safe://" + service.value + "." + publicID.value;
let webCard = {
title: title.value,
description: description.value,
url: url
};
window.safeMutableDataMutation.insert(mutationHandle, time.toString(), JSON.stringify(webCard));
window.safeMutableData.applyEntriesMutation(listyHandle, mutationHandle);
window.safeMutableDataMutation.free(mutationHandle);
window.safeMutableData.free(listyHandle);
} catch (err) {
console.error(err);
} finally {
Materialize.toast("Web Card has been uploaded", 3000, "rounded");
$("#uploadmodal").modal("close");
setTimeout(function() {
getWebCards();
}, 2000);
}
}
async function getConfig() {
try {
let ownContainerHandle = await window.safeApp.getOwnContainer(auth);
try {
let value = await window.safeMutableData.get(ownContainerHandle, "custom-colours");
let colours = JSON.parse(value.buf.toString());
document.documentElement.style.setProperty("--primaryColor", colours.primaryColor);
document.documentElement.style.setProperty("--accentColor", colours.accentColor);
document.documentElement.style.setProperty("--darkPrimaryColor", colours.darkPrimaryColor);
} catch (err) {
let colorsConfig = {
primaryColor: "#448aff",
accentColor: "#ffea00",
darkPrimaryColor: "#1565c0"
};
let mutationHandle = await window.safeMutableData.newMutation(auth);
window.safeMutableDataMutation.insert(mutationHandle, "custom-colours", JSON.stringify(colorsConfig));
window.safeMutableData.applyEntriesMutation(ownContainerHandle, mutationHandle);
window.safeMutableDataMutation.free(mutationHandle);
window.safeMutableData.free(ownContainerHandle);
}
} catch (err) {
console.error(err);
} finally {
getWebCards();
}
}
async function editConfig() {
try {
if (authorised !== true) {
const auth = await authorise();
}
let primary = document.getElementById("user-primary-colour").value;
let dark = document.getElementById("user-dark-primary-colour").value;
let accent = document.getElementById("user-accent-colour").value;
let colorsConfig = {
primaryColor: primary,
accentColor: accent,
darkPrimaryColor: dark
};
let ownContainerHandle = await window.safeApp.getOwnContainer(auth);
let mutationHandle = await window.safeMutableData.newMutation(auth);
let value = await window.safeMutableData.get(ownContainerHandle, "custom-colours");
window.safeMutableDataMutation.update(
mutationHandle,
"custom-colours",
JSON.stringify(colorsConfig),
value.version + 1
);
window.safeMutableData.applyEntriesMutation(ownContainerHandle, mutationHandle);
window.safeMutableDataMutation.free(mutationHandle);
window.safeMutableData.free(ownContainerHandle);
setTimeout(function() {
getConfig();
}, 1500);
} catch (err) {
console.error(err);
} finally {
$("#settingsmodal").modal("close");
}
}
async function sendFeedback() {
try {
if (authorised !== true) {
const auth = await authorise();
}
let time = new Date().getTime().toString();
let feedback =
"Listy Feedback: " + feedbackarea.value + "/ Score: " + listyscore.value.toString() + "/10";
let feedbackHash = await window.safeCrypto.sha3Hash(auth, "feedy");
let feedbackHandle = await window.safeMutableData.newPublic(auth, feedbackHash, 54321);
let mutationHandle = await window.safeMutableData.newMutation(auth);
window.safeMutableDataMutation.insert(mutationHandle, time, feedback);
window.safeMutableData.applyEntriesMutation(feedbackHandle, mutationHandle);
window.safeMutableDataMutation.free(mutationHandle);
window.safeMutableData.free(feedbackHandle);
} catch (err) {
console.error(err);
} finally {
$("#feedbackmodal").modal("close");
Materialize.toast("Thanks for your feedback!", 3000, "rounded");
}
}
function uintToString(uintArray) {
return new TextDecoder("utf-8").decode(uintArray);
}