Greasy Fork

Greasy Fork is available in English.

开发者:成都东软学院||4S平台试卷信息显示

进入做题页面后,右上角会显示试卷的相关信息

当前为 2024-11-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         开发者:成都东软学院||4S平台试卷信息显示
// @license      MIT
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  进入做题页面后,右上角会显示试卷的相关信息
// @author       You
// @match        *://sep.study.neuedu.com/#/paper/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建悬浮窗的函数
    function createFloatingWindow() {
        var window = document.createElement('div');
        window.id = 'floatingWindow';
        window.style.position = 'fixed';
        window.style.top = '10px';
        window.style.right = '10px';
        window.style.width = '300px';
        window.style.height = '400px';
        window.style.backgroundColor = 'white';
        window.style.border = '1px solid black';
        window.style.zIndex = '9999';
        window.style.overflow = 'auto';
        document.body.appendChild(window);
        // 添加列表项
        var title = document.createElement('h2');
        title.textContent = '试卷信息';
        window.appendChild(title);
        // 添加提示信息
        var info = document.createElement('p');
        info.textContent = '试卷信息加载中...';
        window.appendChild(info);
        // 题目信息列表
        var list = document.createElement('ul');
        window.appendChild(list);
        return window;
    }
    
    // 题目列表删除函数
    function removeListInfo() {
        var floatingWindow = document.getElementById('floatingWindow');
        // 将提示更改为'试卷信息已经完成加载'
        var info = floatingWindow.getElementsByTagName('p')[0];
        info.textContent = '试卷信息已经完成加载';
        // 删除列表
        var list = floatingWindow.getElementsByTagName('ul')[0];
        while (list.firstChild) {
            list.removeChild(list.firstChild);
        }
    }

    // 题目列表添加函数
    function addListInfo(data) {
        var floatingWindow = document.getElementById('floatingWindow');
        // 添加一项data到ul末尾 
        var list = floatingWindow.getElementsByTagName('ul')[0];
        var li = document.createElement('li');
        li.textContent = data;
        list.appendChild(li);
        
    }

    // 发起GET请求并处理响应的函数
    function fetchPaperInfo() {
        // 获取当前URL的哈希部分
        var hash = window.location.hash;
        // 去除哈希前的'#'符号
        var hashContent = hash.replace(/^#/, '');
        // 以'/'分割字符串
        var parts = hashContent.split('/');
        // 获取第一项和第二项,注意检查数组长度以避免越界
        var firstPart = parts[2] || ''; // 第一项
        var secondPart = parts[3] || ''; // 第二项
        //console.log('第一项:', firstPart);
        //console.log('第二项:', secondPart);
        var params = 'candidateId='+firstPart+'&arrangementId='+secondPart;
        var token = localStorage.getItem('token'); // 从本地存储中获取Token
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://sep.study.neuedu.com/se-tool-gateway/biz/test/student/test/paper/info?' + params, true);
        xhr.setRequestHeader('Token', token); // 设置请求头中的Token
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var response = JSON.parse(xhr.responseText);
                displayPaperInfo(response);
            } else if (xhr.readyState == 4) {
                document.getElementById('floatingWindow').innerHTML = 'Failed to fetch data: ' + xhr.status;
            }
        };
        xhr.send();
    }

    // 显示试卷信息的函数
    function displayPaperInfo(data) {
        var testNum = 0;
        removeListInfo();
        for (var type in data['data']){
            for(var question in data['data'][type]['studentPartQuestionList']){
                testNum++;
                var questionJson = data['data'][type]['studentPartQuestionList'][question];
                //var result = "题目:"+questionJson['stem']+" 答案:";
                var testTitle = questionJson['stem'];
                if(testTitle.length>10){
                    testTitle = testTitle.substring(0,10)+"...";
                }
                addListInfo(testNum+"、题目:"+testTitle);
                for(var ans in questionJson['selectReferenceAnswerList']){
                    for(var choose in questionJson['questionOptionList']){
                        if(questionJson['selectReferenceAnswerList'][ans] == questionJson['questionOptionList'][choose]['id']){
                            //result += choose['questionOption']+"</br>";
                            var _listShow = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
                            addListInfo(_listShow[choose]+"."+questionJson['questionOptionList'][choose]['questionOption']);
                        }
                    }
                    console.log(ans);
                }
            }
        }
    }

    // 页面加载完成后执行
    window.addEventListener('load', function() {
        var floatingWindow = createFloatingWindow();
        fetchPaperInfo();
    });
})();