-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
160 lines (141 loc) · 4.44 KB
/
Copy pathscript.js
File metadata and controls
160 lines (141 loc) · 4.44 KB
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
const SUPABASE_URL = "https://jwpvozanqtemykhdqhvk.supabase.co";
const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imp3cHZvemFucXRlbXlraGRxaHZrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzkzNzY3MDgsImV4cCI6MjA1NDk1MjcwOH0.uoNqHwXBalSEoaJgtlmPE8gMr4VmTGmL-XDPFJq1Xr0";
const sb = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
var popupid = 0;
async function logOut() {
await sb.auth.signOut();
window.location.href = "/";
}
async function getUser(bypass=false) {
const { data, error } = await sb.auth.getUser();
if (error || !data.user) {
return { user:false, data:error };
}
if (!bypass) {
const { data: d, error: e } = await sb.from("users").select("username").eq("id", data.user.id).single();
if (!d || e || d.username[0] === ".") window.location.assign("/u/setup.html");
}
return { user: true, data: data.user };
}
function random(type, ...settings) {
switch (type) {
case 100:
let count = settings[0] || 10;
let result = "";
const chars = "qwertyuiopasdfghjklzxcvbnm0123456789_";
for (let i = 0; i < count; i++) result += chars.charAt(Math.floor(Math.random() * chars.length));
return result;
default:
return "Unknown type";
}
}
function popup(title, message, close = true, compact = false) {
var x = document.createElement("div");
x.style.opacity = 0;
x.style.transition = '0.1s';
x.id = "popup" + popupid++;
x.className = "popup";
x.innerHTML = `
<div onclick="e = window.event; e.stopPropagation()" class="${compact?"cm":""}">
<div class="inside">
<h1>${title}</h1>
<h2 onclick="document.getElementById('${x.id}').style.opacity = 0; window.setTimeout(() => document.body.removeChild(document.getElementById('${x.id}')), 201)">X</h2>
</div>
<p style="margin-bottom:0">
${message}
</p>
</div>
`;
if (close) {
x.onclick = () => {
x.style.opacity = 0;
window.setTimeout(() => document.body.removeChild(document.getElementById(x.id)), 201);
}
}
document.body.appendChild(x);
window.setTimeout(() => x.style.opacity = 1, 1);
}
async function getBalance(uid) {
const { data: balance, error: userExistsErrorn } = await sb
.from("udata")
.select("balance_nus, balance_noca, balance_sats")
.eq("user_id", uid)
.single();
if (!balance || userExistsErrorn) return false;
return [balance.balance_nus, balance.balance_noca, balance.balance_sats];
}
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
async function getVariable(vars) {
const { data, error } = await sb
.from("variable")
.select("value")
.eq("key", vars)
.single();
if (!data || error) return null;
else return data.value;
}
function formatNumber(num) {
var level = 0;
while (num >= 1000) {
num = +((num / 1000).toFixed(3))
level += 1;
}
num = +(num.toFixed(2));
var res = "";
switch (level) {
case 0:
res = "";
break;
case 1:
res = "k";
break;
case 2:
res = "M";
break;
case 3:
res = "G";
break;
case 4:
res = "T";
break;
case 5:
res = "P";
break;
case 6:
res = "E";
break;
case 7:
res = "Z";
break;
case 8:
res = "Y";
break;
case 9:
res = "R";
break;
case 10:
res = "Q";
break;
default:
res = "*10^" + level * 3;
}
return [num, res];
}
addEventListener("keydown", async (e) => {
if (e.key === "q" && (e.ctrlKey || e.metaKey)) {
const { user, data } = await getUser();
if (user) {
popup('Log Out', `Please confirm that you want to log out.</p><br><div class='flex cc'><br><button onclick='logOut()' class='fullwidth'>Log Out</button></div><p style='margin:0'>`)
}
}
});
console.log("script loaded");