【超星】并发执行,自动做题

// ajax Promise 封装
function ajaxAsync(options) {
    return new Promise((resolve, reject) => {
        let { success, error } = options;
        delete options['success'], options['error'];
        $.ajax(options)
            .done(function (res) {
                success && success(res)
                resolve(res);
            }).fail(function (xhr) {
                error && error(xhr);
                resolve(xhr.responseJSON && xhr.responseJSON.message);
            });
    });
}


async function getPromise(question) {
    return ajaxAsync({
        type: 'post',
        url: 'https://gaolihai.top/query',
        data: {
            type: 4,
            pwd: '****',
            question,
            reJson: false
        }
    });
}

function getQuestion() {
    let arr = [];
    let questions = $('#iframe').contents().find('iframe').contents().find('iframe').contents().find('.TiMu .Zy_TItle');
    questions.each((i, val) => arr.push(val.innerText
        .replace(/[\s|(|)]/g, '')
        .replace(/^[0-9]*/g, '')
        .replace(/【.*】/g, '')));
    return arr;
}

function getDom() {
    let arr = [];
    $('#iframe').contents().find('iframe').contents().find('iframe').contents().find('.TiMu .Zy_TItle')
        .each((i, val) => {
            arr.push($(val).parent().find('.Zy_ulBottom,.Zy_ulTop'))
        });
    return arr;
}

function goToTest() {
    $('.tabtags').children().each((i, val) => {
        if (val.innerText == '章节测验') {
            val.click();
        }
    });
}

function fullChar2halfChar(str) {
    var result = '';
    for (i = 0; i < str.length; i++) {
        code = str.charCodeAt(i);//获取当前字符的unicode编码
        if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符
        {
            result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
        } else if (code == 12288)//空格
        {
            result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
        } else {
            result += str.charAt(i);
        }
    }
    return result;
}

function run() {
    goToTest()
    setTimeout(async () => {
        // 并发
        let answers = await Promise.all(getQuestion().map(val => getPromise(val)));

        // 串行
        let answers = [];
        for (question of getQuestion()) {
            console.log(answers.push(await getPromise(question)));
        }

        let doms = getDom();

        // 题目遍历
        let isAllSelected = true;
        doms.forEach((question, index) => {
            let answer = answers.shift()['answer'];
            console.log(`第 ${index + 1} 题:` + answer);
            // 选项遍历
            let isSelected = false;
            if (answer.indexOf('√') != -1 || answer.indexOf('×') != -1 || (answer == '对' || answer == '错') || (answer == '正确' || answer == '错误')) {
                // 判断题
                $(question).children().each(function (_, option) {
                    if ($(option).find('.ri').length == 1 && (answer == '√' || answer == '对' || answer == '正确')) {
                        // 正确答案
                        $(option).find('input').click();
                        isSelected = true;
                    } else if ($(option).find('.wr').length == 1 && (answer == '×' || answer == '错' || answer == '错误')) {
                        $(option).find('input').click();
                        isSelected = true;
                    }
                });
                if (!isSelected) {
                    console.log(`第 ${index + 1} 题未匹配,请手动选择。`);
                    isAllSelected = false;
                }
            } else {
                // 选择题
                $(question).children().each(function (_, option) {
                    if (fullChar2halfChar($(option).text()).indexOf(fullChar2halfChar(answer)) != -1) {

                        // 正确答案
                        $(option).find('input').click();
                        isSelected = true;
                    }
                });
                if (!isSelected) {
                    console.log(`第 ${index + 1} 题未匹配,请手动选择。`);
                    isAllSelected = false;
                }
            }
            if (isAllSelected) {
                $('#iframe').contents().find('iframe').contents().find('iframe').contents().find('.Btn_blue_1').click();
                setTimeout(() => $('#iframe').contents().find('iframe').contents().find('iframe').contents().find('.bluebtn').not('#submitBackOk').click(), 300);

            }
        });
    }, 1500);
}

async function test() {
    let an = await getPromise('太极拳');
    console.log(an);

}
run();

你可能感兴趣的:(随便写点,js)