-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.js
134 lines (128 loc) · 3.08 KB
/
common.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
//tts
function tts(word){
var msg = new SpeechSynthesisUtterance();
msg.text = word;
window.speechSynthesis.speak(msg);
}
//window.onbeforeunload=()=>tts("");
// src: https://www.quirksmode.org/js/cookies.html
function setId(id){
document.cookie = "id="+id+"; expires=2147483647; path=/";
}
function getId() {
var nameEQ = "id=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function clearId() {
document.cookie = "id=; expires=-1; path=/";
}
function saveData(data,cb=()=>{}){
let id=getId();
if(id){
fetch("https://db.copticwordle.repl.co/saveData", {
method: "POST",
headers: {
'Accept':'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: id,
data: data,
}),
})
.then(async rsp=>{
let msg=await rsp.json();
if (rsp.status!=200) {
msg=msg.message//JSON.parse(msg).message;ge
alert('Error: '+msg);
} else {
cb();
}
})
.catch(err=>{
alert("Something went wrong...");
console.error(err);
})
}
}
async function loadData(){
let id=getId();
if(id){
return new Promise((rs,rj)=>{
fetch("https://db.copticwordle.repl.co/loadData", {
method: "POST",
headers: {
'Accept':'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: id,
}),
})
.then(async rsp=>{
let msg=await rsp.json();
if (rsp.status!=200) {
msg=msg.message//JSON.parse(msg).message;ge
alert('Error: '+msg);
rj();
} else {
let data=msg.data;
console.log(data);
rs(data);
}
})
.catch(err=>{
alert("Something went wrong...");
console.error(err);
rj();
})
});
}
}
function checkLogin(){
if(id){
document.getElementById("account").parentElement.outerHTML=`<button id="account">Log Out</button>`;
document.getElementById("account").onclick=()=>{
fetch("https://db.copticwordle.repl.co/logout", {
method: "POST",
headers: {
'Accept':'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: id,
}),
})
.then(async rsp=>{
let msg=await rsp.json();
if (rsp.status!=200) {
msg=msg.message//JSON.parse(msg).message;ge
alert('Error: '+msg);
} else {
clearId();
location.reload();
}
})
.catch(err=>{
alert("Something went wrong...");
console.error(err);
})
}
(async()=>{
//make vars global?
dataFULL=await loadData();
var data=dataFULL.substring(0,dataFULL.length-1);
var thm=dataFULL[dataFULL.length-1];
var ctr=thm;
changeTheme();
update();
//
})();
}
}