-
Notifications
You must be signed in to change notification settings - Fork 1
/
click-click.js
90 lines (77 loc) · 2.56 KB
/
click-click.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
/* eslint-disable no-var */
var lang = 'en'
var timeout = 0
var shouldAutoClick = true
var fetchQuestions = async () => {
try {
const data = await fetch(`https://iron-man-sat.s3-ap-southeast-1.amazonaws.com/fe-questions-${lang}.json`, {
headers: {
accept: 'application/json, text/plain, */*',
},
method: 'GET',
})
return data.json()
} catch (err) {
console.log(err.message)
}
}
async function run() {
const allQuestions = []
const fetchRes = await fetchQuestions()
allQuestions.push(...fetchRes)
const questionNumberList = document.querySelector('div.question-list').children
var fromComponentQuestions = []
var xTag = document.querySelector('div.flex.test-view.w-full')
var reactInternalInstanceKey = Object.keys(xTag).find((key) => key.startsWith('__reactInternalInstance$'))
xTag[reactInternalInstanceKey].return.memoizedProps.questions.forEach((ques) => {
const obj = Object.fromEntries(Array.from(ques))
const choices = []
obj.choices.forEach((c) => choices.push(Object.fromEntries(Array.from(c))))
obj.choices = choices
fromComponentQuestions.push(obj)
})
if (questionNumberList.length !== fromComponentQuestions.length) {
console.log(
"Questions don't match: ",
'provided: ',
questionNumberList.length,
', ',
'fetched: ',
fromComponentQuestions.length
)
return
}
var wait = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, timeout)
})
}
for (const questionNumber of questionNumberList) {
questionNumber.querySelector('a').click()
const providedAnswers = document.querySelector('div.question-answers div.ant-radio-group-outline').children
const question = fromComponentQuestions[Number(questionNumber.innerText) - 1]
const questionID = question.id
const ans = allQuestions.find((q) => String(q.id) === String(questionID))
if (!ans) {
console.log(`There's no answer for question ${questionNumber.innerText}`)
} else {
const ans_order = question.choices.findIndex((item) => item.id === ans.answer_id)
console.log(`Answer for question ${questionNumber.innerText}: ${ans_order}`)
if (timeout) {
await wait()
}
if (shouldAutoClick) {
providedAnswers[ans_order].querySelector('input').click()
}
}
// await wait(1000)
}
if (!shouldAutoClick) {
for (const questionNumber of questionNumberList) {
questionNumber.querySelector('a div.question-item').classList.remove('not-answer-bg')
}
}
}
run()