-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
81 lines (71 loc) · 1.8 KB
/
account.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
'use strict';
(function() {
let currBlock = getCheckBox();
window.addEventListener('load', init);
function init() {
if(currBlock) {
id('blocker_val').checked = true;
} else {
id('blocker_val').checked = false;
}
id('blocker_btn').addEventListener('click', updateBlock);
}
function handleError(err) {
id('error').classList.remove("hidden");
console.log(err);
}
async function updateBlock() {
try {
let bool = id('blocker_val').checked;
let login = sessionStorage.getItem('username');
let blockNum = 0;
if(bool) {
blockNum = 1;
}
let params = {
username: login,
block: blockNum
};
let res = await fetch('http://127.0.0.1:5000/changeblock', {
method: "POST",
headers: {
'Content-Type': 'application/json' // Set the content type to JSON
},
body: JSON.stringify(params)
});
await statusCheck(res);
currBlock = bool;
} catch (err) {
handleError(err);
}
}
async function getCheckBox() {
try {
let login = sessionStorage.getItem('username');
let params = {
username: login
};
let res = await fetch('http://127.0.0.1:5000/getblock', {
method: "GET",
body: JSON.stringify(params)
});
await statusCheck(res);
res = await res.json();
return res.block;
} catch(err) {
handleError(err);
}
}
async function loadAccount(res) {
id('blocker_val').checked = await getCheckBox();
}
async function statusCheck(res) {
if (!res.ok) {
throw new Error(await res.text());
}
return res;
}
function id(id) {
return document.getElementById(id);
}
})();