fuck易班100分
// ==UserScript==
// @name fuck易班100分
// @version 1.1
// @description 易班100分模板
// @author renren0103
// @license MIT
// @namespace renren@temp
// @icon https://tva1.sinaimg.cn/large/007X1OJgly1h1h8wqvbpnj308c086aah.jpg
// @match https://app.uyiban.com/anstorm/client/
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==
/**
* 拦截fetch请求
*/
const fetchHook = () => {
const originFetch = fetch;
unsafeWindow.fetch = (url, options) => {
return originFetch(url, options).then(async (response) => {
if (url.indexOf("getQuestion") != -1) {
autoCommit(response.clone())
}
if (url.indexOf("answer") != -1) {
answerRecorder(response.clone(), options)
}
return response
})
}
}
/**
* 自动提交
* @param {请求} response
*/
const autoCommit = (response) => {
/**
* 异步回调导致无法拆分
*/
response.body.getReader().read().then(res => {
let data = JSON.parse(Utf8ArrayToStr(res.value))
let id = data.data.Id
let answer = []
answer = answerReader(id)
setTimeout(() => {
if (answer != undefined) {
console.log("匹配到答案:" + answer);
setTimeout(() => {
answer.forEach(answerRaw => {
let answerList = getAnswerListFromDom()
for (let index = 0; index < answerList.length; index++) {
const answerElement = answerList[index];
const answerText = getAnswerTextFromElement(answerElement)
if (answerText == answerRaw) {
answerElement.click()
}
}
setTimeout(() => {
autoNext()
}, 500)
});
}, 1000)
} else {
console.log("未匹配到答案!随机提交");
let answerList = getAnswerListFromDom()
answerList[0].click()
answerList[1].click()
autoNext()
}
}, 1500);
})
}
const autoNext = () => {
let nextBtn = document.getElementsByClassName("am-button-primary")
if (nextBtn.length != 0) {
nextBtn[0].click();
if (nextBtn[0] != undefined) {
nextBtn[0].click();
}
}
}
const answerReader = (id) => {
return JSON.parse(window.localStorage.getItem(id))
}
const answerRecorder = (response, request) => {
response.body.getReader().read().then(res => {
let data = JSON.parse(Utf8ArrayToStr(res.value))
let id = getQuestionId(request.body)
let answer = findAnswerByOption(data.data.Answer)
storeAnswer(id, answer)
})
}
const findAnswerByOption = (options) => {
let answers = []
if (options.indexOf("A") != -1) {
answers.push(findAnswerFromDom(0))
}
if (options.indexOf("B") != -1) {
answers.push(findAnswerFromDom(1))
}
if (options.indexOf("C") != -1) {
answers.push(findAnswerFromDom(2))
}
if (options.indexOf("D") != -1) {
answers.push(findAnswerFromDom(3))
}
if (options.indexOf("E") != -1) {
answers.push(findAnswerFromDom(4))
}
if (options.indexOf("F") != -1) {
answers.push(findAnswerFromDom(5))
}
if (options.indexOf("G") != -1) {
answers.push(findAnswerFromDom(6))
}
if (options.indexOf("H") != -1) {
answers.push(findAnswerFromDom(7))
}
return JSON.stringify(answers)
}
const getAnswerListFromDom = () => {
return document.getElementsByClassName("am-flexbox")
}
const getQuestionId = (s) => {
return s.substring(s.indexOf("&QuestionId=") + 12, s.indexOf("&Answer="));
}
const findAnswerFromDom = (index) => {
let answerList = getAnswerListFromDom()
return getAnswerTextFromElement(answerList[index])
}
const getAnswerTextFromElement = (element) => {
let spans = element.getElementsByTagName("span")
return spans[2].innerText
}
const storeAnswer = (key, value) => {
window.localStorage.setItem(key, value)
}
const Utf8ArrayToStr = (array) => {
let out, i, len, c;
let char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
out += String.fromCharCode(c);
break;
case 12: case 13:
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
(function () {
fetchHook()
})();