-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
128 lines (123 loc) · 3.88 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lichess: List and decline challenges</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
color: #c9d1d9;
background-color: #0d1117;
}
a {
color: #58a6ff;
}
.spinner {
visibility: hidden;
display: inline-block;
position: relative;
width: 30px;
height: 30px;
}
.running .spinner {
visibility: visible;
}
.spinner:after {
content: ' ';
display: block;
border-radius: 50%;
width: 0;
height: 0;
margin: 8px;
box-sizing: border-box;
border: 16px solid black;
border-color: #c9d1d9 transparent #c9d1d9 transparent;
animation: spinner 1.2s infinite;
}
@keyframes spinner {
0% {
transform: rotate(0);
animation-timing-function: cubic-bezier(0.55, 0.055, 0.675, 0.19);
}
50% {
transform: rotate(900deg);
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
100% {
transform: rotate(1800deg);
}
}
td:nth-child(3) {
text-align: center;
}
td {
padding: 2px 10px;
}
tr:nth-child(odd) {
background: lightgray;
color: black;
}
</style>
</head>
<body>
<h2>Lichess: List and decline challenges</h2>
<label for="api-key">
API key (with <code>challenge:read</code> and <code>challenge:write</code> permission):
</label>
<input id="api-key" /><br />
<button id="run" type="button">Run</button>
<h3>Results <span class="spinner"></span></h3>
<table id="challenges" cellspacing="0">
<tr>
<th>Name</th>
<th>Variant</th>
<th>Time</th>
<th></th>
</tr>
</table>
<script>
async function run() {
const results = document.getElementById('results');
const table = document.getElementById('challenges');
const apiKey = document.getElementById('api-key').value.trim();
const headers = { Authorization: 'Bearer ' + apiKey };
document.body.classList.add('running');
const resp = await fetch('https://lichess.org/api/challenge', { headers });
const challenges = await resp.json();
for (const chall of challenges.in) {
const row = document.createElement('tr');
row.innerHTML = [
chall.challenger?.name ?? 'Anonymous',
chall.variant.name,
chall.timeControl?.show ?? chall.speed,
]
.map((s) => '<td>' + s + '</td>')
.join('');
const btn = document.createElement('button');
btn.innerText = 'Decline';
btn.addEventListener('click', () => {
document.body.classList.add('running');
fetch(`https://lichess.org/api/challenge/${chall.id}/decline`, {
method: 'POST',
headers,
});
document.body.classList.remove('running');
row.remove();
});
const btnTd = document.createElement('td');
btnTd.appendChild(btn);
row.appendChild(btnTd);
table.appendChild(row);
}
document.body.classList.remove('running');
}
document.getElementById('run').addEventListener('click', () => run());
const apiKeyEl = document.getElementById('api-key');
apiKeyEl.addEventListener('change', () => localStorage.setItem('apiKey', apiKeyEl.value));
const key = localStorage.getItem('apiKey');
if (key) apiKeyEl.value = key;
</script>
</body>
</html>