added statistics, autofocus field on reload
This commit is contained in:
parent
3d5fa825c4
commit
e18d26f6f9
|
@ -20,3 +20,13 @@ body {
|
|||
margin-top: 1%;
|
||||
margin-bottom: 1%;
|
||||
}
|
||||
|
||||
.topSolvers {
|
||||
margin-left: auto;
|
||||
margin-right: 5%;
|
||||
max-width: max-content;
|
||||
float:right;
|
||||
display: inline;
|
||||
margin-top:1%;
|
||||
padding-top: 0%;
|
||||
}
|
|
@ -26,7 +26,6 @@ const get_cookie = (name) => {
|
|||
const cookie = cookies[i].trim();
|
||||
if (cookie.startsWith(name + '=')) {
|
||||
const value = cookie.substring(name.length + 1);
|
||||
console.log(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,9 +38,27 @@ const blobToBase64 = (blob) => {
|
|||
});
|
||||
}
|
||||
|
||||
const show_stats = async () => {
|
||||
const response = await fetch("/api/user/stats", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
const stats = (await response.json()).stats;
|
||||
const statsText = document.getElementById("topSolversText");
|
||||
statsText.innerHTML += '<br/>'
|
||||
stats.top_five.forEach(stat => {
|
||||
statsText.innerHTML += `<b>${stat.username}: ${stat.count}</b><br/>`;
|
||||
});
|
||||
statsText.innerHTML += `You have solved ${stats.my_count} captcha(s)`
|
||||
}
|
||||
|
||||
window.onload = async () => {
|
||||
console.log("koka: " + get_cookie("JWT"));
|
||||
console.log("all: " + document.cookie);
|
||||
const inputField = document.getElementById("captcha");
|
||||
inputField.focus();
|
||||
// intentionally do not wait for it
|
||||
show_stats();
|
||||
if (!document.cookie.includes('JWT')) {
|
||||
document.location.href = "/login";
|
||||
}
|
||||
|
@ -51,9 +68,8 @@ window.onload = async () => {
|
|||
|
||||
const url = URL.createObjectURL(captcha);
|
||||
document.getElementById("captcha_image").src = url;
|
||||
console.log(captcha.type)
|
||||
const form = document.getElementById("captchaForm");
|
||||
const inputField = document.getElementById("captcha");
|
||||
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
// if (!await check_solution(inputField.value)) {
|
||||
|
|
|
@ -32,7 +32,17 @@ class UserController {
|
|||
console.log(e)
|
||||
return res.status(500).send({"message": "Unknown server error"});
|
||||
}
|
||||
}
|
||||
|
||||
async stats (req, res) {
|
||||
try {
|
||||
const user_id = jwt.decode(req.token).id;
|
||||
const stats = await UserService.get_stats(user_id);
|
||||
return res.status(200).send({"message": "Success", "stats": stats});
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
return res.status(500).send({"message": "Unknown server error"});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,5 +7,5 @@ const UserRouter = new Router();
|
|||
|
||||
UserRouter.post('/user/register', auth.verify_admin_token, UserController.register);
|
||||
UserRouter.post('/user/login', UserController.login);
|
||||
|
||||
UserRouter.get('/user/stats', auth.verify_user_jwt, UserController.stats);
|
||||
export default UserRouter;
|
|
@ -8,6 +8,13 @@ class UserService {
|
|||
async get_by_username(username) {
|
||||
return (await db.query("SELECT * FROM users WHERE username = $1", [username])).rows[0];
|
||||
}
|
||||
|
||||
async get_stats(user_id) {
|
||||
return {
|
||||
my_count: (await db.query("SELECT COUNT(*) FROM captchas WHERE submitter = $1", [user_id])).rows[0].count,
|
||||
top_five: (await db.query("select username, (select count(*) from captchas where submitter = users.id) from users JOIN captchas on submitter = users.id GROUP BY users.id ORDER BY count DESC LIMIT 5")).rows
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserService();
|
|
@ -6,8 +6,12 @@ html
|
|||
meta(name="description" content="")
|
||||
link(href="css/index.css" rel="stylesheet")
|
||||
body
|
||||
div(id="tsparticles")
|
||||
div(id="main")
|
||||
div(class="topSolvers")
|
||||
label(id="topSolversText") Top-5 solvers:
|
||||
|
||||
main(class="box")
|
||||
|
||||
h2 Captcha Aggregator
|
||||
|
||||
form(id="captchaForm")
|
||||
|
|
|
@ -6,10 +6,9 @@ html
|
|||
meta(name="description" content="")
|
||||
link(href="css/index.css" rel="stylesheet")
|
||||
body
|
||||
div(id="tsparticles")
|
||||
div(id="main")
|
||||
main(class="box")
|
||||
h2 Captcha Aggregator
|
||||
|
||||
form(id="loginForm")
|
||||
div(class="inputBox")
|
||||
label(for="username") Username
|
||||
|
|
Loading…
Reference in New Issue