Skip to main content

上岗证脚本

提交测试后,打开开发者工具,进入Console,复制下面代码显示答案。

(function highlightCorrectAnswers() {
    // 先清除之前可能残留的高亮
    document.querySelectorAll('[id^="answer_item_"]').forEach(function (el) {
        el.style.backgroundColor = '';
        el.style.fontWeight = '';
    });

    var scriptTags = document.querySelectorAll('script');
    // key: 唯一题目ID(如 answer_baff3686...),value: { questionNumber, questionText, answerIds: [] }
    var resultMap = new Map();
    var order = []; // 记录题目ID出现的顺序,保证输出顺序与页面一致

    scriptTags.forEach(function (scriptTag) {
        var scriptContent = scriptTag.innerText;
        if (!scriptContent) return;

        var regex = /setRightAnswer\('(.*?)',\s*'(answer_item_.*?)'\);/g;
        var match;

        while ((match = regex.exec(scriptContent)) !== null) {
            var uniqueQuestionId = match[1];   // 唯一标识,用于分组,不展示
            var answerItemId = match[2];

            if (!resultMap.has(uniqueQuestionId)) {
                // 第一次遇到这道题,提取题号和题干
                var li = scriptTag.closest('li.cs-test-item');
                var questionNumber = '?';
                var questionText = '';

                if (li) {
                    var questionEl = li.querySelector('.cs-test-question');
                    if (questionEl) {
                        var numberSpan = questionEl.querySelector('span');
                        questionNumber = numberSpan ? numberSpan.innerText.trim() : '?';

                        var questionClone = questionEl.cloneNode(true);
                        questionClone.querySelectorAll('span').forEach(function (s) { s.remove(); });
                        questionText = questionClone.innerText.trim().replace(/^[、,,.\s]+/, '');
                    }
                } else {
                    questionText = '(未找到题目容器)';
                }

                resultMap.set(uniqueQuestionId, {
                    questionNumber: questionNumber,
                    questionText: questionText,
                    answerIds: []
                });
                order.push(uniqueQuestionId);
            }

            resultMap.get(uniqueQuestionId).answerIds.push(answerItemId);
        }
    });

    var questionCount = 0;
    var answerCount = 0;

    order.forEach(function (uniqueQuestionId) {
        var data = resultMap.get(uniqueQuestionId);
        var answerTexts = [];

        data.answerIds.forEach(function (id) {
            var el = document.getElementById(id);
            if (el) {
                el.style.backgroundColor = 'yellow';
                el.style.fontWeight = 'bold';
                answerTexts.push(el.innerText.trim());
                answerCount++;
            } else {
                console.warn('⚠️ 未找到元素 #' + id);
            }
        });

        if (answerTexts.length > 0) {
            // 根据答案数量自动判断单选/多选,而不是依赖 class 名称
            var typeLabel = answerTexts.length > 1 ? '多选' : '单选';
            console.log(
                '【' + typeLabel + '】第' + data.questionNumber + '题:' + data.questionText +
                ' → 正确答案: ' + answerTexts.join('、')
            );
            questionCount++;
        }
    });

    console.log('共标记 ' + questionCount + ' 道题,' + answerCount + ' 个正确答案选项。');
})();