-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (87 loc) · 2.76 KB
/
Copy pathscript.js
File metadata and controls
103 lines (87 loc) · 2.76 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
document.addEventListener('DOMContentLoaded', () => {
shuffleCaptchaGrid();
});
document.querySelectorAll('.captcha-item').forEach(item => {
item.addEventListener('click', () => {
onClickedImage(item);
})
})
let currentTargetIndex = 0;
function handleClick(action) {
if (action === 'refresh') {
shuffleCaptchaGrid();
} else if (action === 'headphones') {
// headphones action
} else if (action === 'info') {
// info action
} else if (action === 'answer') {
onSendAnswer();
}
}
function shuffleCaptchaGrid() {
const grid = document.querySelector('.captcha-grid');
const items = Array.from(grid.children);
items.forEach(item => {
item.classList.remove('selected');
})
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
grid.appendChild(items[j]);
}
}
function onClickedImage(item) {
item.classList.toggle('selected');
}
function onSendAnswer() {
answer = getSelectedImages();
sendAnswer(answer);
}
function getSelectedImages() {
const selectedItems = document.querySelectorAll('.captcha-item.selected img');
const filenames = new Set();
selectedItems.forEach(img => {
const src = img.getAttribute('src');
const filename = src.substring(src.lastIndexOf('/') + 1, src.lastIndexOf('.'));
filenames.add(filename);
})
console.log(filenames);
return filenames;
}
function sendAnswer(filenames) {
const filenamesArray = Array.from(filenames);
fetch('ENDPOINT-URL/validateAnswer', {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({ items: filenamesArray})
})
.then(response => response.text())
.then(result => {
console.log(result);
if (result === 'True') {
console.log('Correct');
updateVisibility(true);
} else {
console.log('Incorrect');
updateVisibility(false);
}
})
.catch(error => {
console.error('Error:', error);
});
}
function updateVisibility(isCorrect) {
console.log(isCorrect);
const captchaContainer = document.querySelector('.captcha-container');
const correctAnswerContainer = document.querySelector('.answer-container.correct');
const incorrectAnswerContainer = document.querySelector('.answer-container.incorrect');
// captchaContainer.style.visibility = 'collapse';
if (isCorrect) {
correctAnswerContainer.style.visibility = 'visible';
incorrectAnswerContainer.style.visibility = 'collapse';
} else {
correctAnswerContainer.style.visibility = 'collapse';
incorrectAnswerContainer.style.visibility = 'visible';
}
}