Greasy Fork

Greasy Fork is available in English.

上海开大助手

提高了自己效率,希望能帮助到更多人

目前为 2024-12-06 提交的版本。查看 最新版本

// ==UserScript==
// @name         上海开大助手
// @namespace    http://tampermonkey.net/
// @homepage     https://101.133.228.70
// @description  提高了自己效率,希望能帮助到更多人
// @author       AchieveHF
// @version      1.0.1
// @match        *://*.shou.org.cn/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=shou.org.cn
// @grant        GM_setValue
// @grant        GM_getValue
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @require      https://unpkg.com/[email protected]/dist/layui.js
// @license MIT
// @run-at document-end
// ==/UserScript==

(function () {
    'use strict';
    const css = document.createElement('link');
    css.rel = 'stylesheet';
    css.href = '//unpkg.com/[email protected]/dist/css/layui.css';
    document.head.appendChild(css);
    const apiDomain = 'https://101.133.228.70/';

    let link = parseURLDetails();
    switch (link.path) {
        case '/study/assignment/preview.aspx':
            //插入工具
            $.ajax({
                url: apiDomain + '/api/getBody',//获取题目接口
                type: 'GET',
                data: {
                    path: link.path,
                },
                success: function (data) {
                    $('.e-q-t').append(data)
                }
            })
            //监听工具使用
            $('body').on('click', '.searchQuestion', function () {
                let q = getQuestion($(this).parent().parent())
                var index = layer.load(0, {shade: false});
                $.ajax({
                    url: apiDomain + '/api/searchQuestion',//获取题目接口
                    type: 'POST',
                    data: {
                        question: q,
                    },
                    success: (res) => {
                        layer.close(index);
                        if (res.code == 200) {
                            $(this).parent().find('.result').html('正确答案:' + res.data.answer)
                            layer.msg(res.msg)
                        } else {
                            layer.msg(res.msg)
                        }
                    }
                })
            })
            break;
        case '/scenter':
            //学习空间首页
            const personalInfo = {};
            const fieldMap = {
                "姓名": "name",
                "学号": "studentId",
                "专业": "major",
                "班级": "class",
                "学校": "school",
                "毕业学分": "totalCredits",
                "已修学分": "earnedCredits"
            };

            $(".info-area p").each(function () {
                const label = $(this).find(".info-label").text().replace(":", "").trim();
                const value = $(this).text().replace($(this).find(".info-label").text(), "").trim();
                if (fieldMap[label]) {
                    personalInfo[fieldMap[label]] = value;
                }
            });
            GM_setValue('userData', personalInfo)
            $.ajax({
                url: apiDomain + '/api/getBody',//获取题目接口
                type: 'GET',
                data: {
                    path: link.path,
                },
                success: function (data) {
                    $('.content-left').append(data)
                }
            })
            break;
        case '/study/assignment/history.aspx':
            //插入页面工具
            $.ajax({
                url: apiDomain + '/api/getBody',//获取题目接口
                type: 'GET',
                data: {
                    path: link.path,
                },
                success: function (data) {
                    $('.e-quest-header').before(data)
                }
            })
            //监听工具动作
            $('body').on('click', '.share', function () {
                //作业结果页面
                let q_a = [];
                //获取所有回答正确的题
                $('.e-q-t').each(function (index, element) {
                    //大题
                    if ($(element).find('.e-q-quest .e-q-quest').length > 0) {
                        if ($(element).find('.e-q-l .e-q-right').length > 0) {
                            let topic = $(element).find('.e-q-q .ErichText').first().html()
                            let topic_text = $(element).find('.e-q-q .ErichText').first().text().trim().replaceAll('\n', '')
                            $(element).find('.e-q-quest .e-q-quest').each((index, elem) => {
                                let q = {
                                    topic: topic,
                                    topic_text: topic_text,
                                    type: '',
                                    question: '',
                                    question_text: '',
                                    answer: '',
                                    answer_options: '',
                                }
                                q = getRightQuestion(elem, q, true);
                                pushQuestion(q_a, q, element)
                                // pushQuestion(q_a,q,element)
                            })
                        }
                    } else {
                        let q = {
                            topic: '',
                            topic_text: '',
                            type: '',
                            question: '',
                            question_text: '',
                            answer: '',
                            answer_options: '',
                        }
                        q = getRightQuestion(element, q);
                        if (q !== false) {
                            pushQuestion(q_a, q, element)
                        }
                    }

                })
                //发送请求记录到题库
                $.ajax({
                    url: apiDomain + '/api/saveQuestions',//记录题目接口
                    type: 'POST',
                    data: {
                        q_a: q_a,
                        params: link.params,
                        userData: GM_getValue('userData')
                    },
                    success: function (data) {
                        layer.msg(data.msg)
                    }
                })
            })
            break;
        case '/study/assignment-preview.aspx':
            break;
    }

    //提取题目
    function getRightQuestion(element, q, sureRight = false) {
        if ($(element).find('.e-checking-a').length > 0) {
            //判断题
            q.type = '判断题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')
            if ($(element).find('.e-q-l .e-q-right').length > 0) {
                //答对的
                q.answer = $(element).find('li.e-a.checked').text().trim().split(' ')[1]
                q.answer_options = $(element).find('li.e-a.checked').text().trim().charAt(0)
            } else {
                //答错的,记录正确答案
                q.answer = $(element).find('li.e-a:not(.checked)').text().trim().split(' ')[1]
                q.answer_options = $(element).find('li.e-a:not(.checked)').text().trim().charAt(0)
            }
        } else if ($(element).find('.e-choice-a').length > 0 && ($(element).find('.e-q-l .e-q-right').length > 0 || sureRight)) {
            //答对的单选题
            q.type = '选择题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')
            if ($(element).find('li.e-a.checked').length > 1) {
                //多选题
                $(element).find('li.e-a.checked').each((index, elem) => {
                    q.answer += $(elem).find('.ErichText').text().trim().replaceAll('\n', '') + '\n'
                    q.answer_options += $(elem).contents().eq(2).text().trim().charAt(0)
                })
            } else if ($(element).find('li.e-a.checked').length = 1) {
                q.answer = $(element).find('li.e-a.checked .ErichText').text().trim().replaceAll('\n', '')
                q.answer_options = $(element).find('li.e-a.checked').contents().eq(2).text().trim().charAt(0)
            }
        } else if ($(element).find('.e-blank-a').length > 0 && ($(element).find('.e-q-l .e-q-right').length > 0 || sureRight)) {
            //填空题
            q.type = '填空题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')
            q.answer = []
            $(element).find('li.e-a').each((index, elem) => {
                q.answer.push({
                    title: $(elem).find('.e-blank-e').text(),
                    answer: $(elem).find('input').val()
                })
            })
            q.answer_options = null
        } else if ($(element).find('.e-short-a').length > 0 && ($(element).find('.e-q-l .e-q-right').length > 0 || sureRight)) {
            //排序题
            q.type = '排序题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')
            q.answer = []
            $(element).find('.am-g .am-u-sm-5').first().find('.ErichText').each((index, elem) => {
                q.answer.push({
                    title: $(elem).text().trim().replaceAll('\n', ''),
                    answer: $(element).find('.am-g .am-u-sm-1 .e-choice-i').eq(index).text().trim().replaceAll('\n', '')
                })
            })
            q.answer_options = null
        } else {
            q = false;
        }
        return q;
    }

    function getQuestion(element) {
        let q = {
            topic: '',
            topic_text: '',
            type: '',
            question: '',
            question_text: ''
        }
        if ($(element).find('.e-checking-a').length > 0) {
            //判断题
            q.type = '判断题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')

        } else if ($(element).find('.e-choice-a').length > 0) {
            //答对的单选题
            q.type = '选择题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')
        } else if ($(element).find('.e-blank-a').length > 0) {
            //填空题
            q.type = '填空题';
            q.question = $(element).find('.e-q-q .ErichText').html()
            q.question_text = $(element).find('.e-q-q .ErichText').text().trim().replaceAll('\n', '')

        } else {
            q = false;
        }
        return q;
    }

    //题目筛查
    function pushQuestion(q_a, q, element) {
        if (q.question === undefined || q.answer === undefined || q.answer_options === undefined) {
            $.ajax({
                url: '/api/errorQuestion',//记录无法提取的题目接口
                type: 'POST',
                data: {
                    element: element.outerHTML,
                },
                success: function (data) {
                    return false
                }
            })
            console.log('无法提取的题目已记录', element, q)
        } else {
            q_a.push(q)
        }
    }

    //解析页面url
    function parseURLDetails() {
        // 获取完整的 URL
        const fullUrl = window.location.href;

        // 获取页面路径(不含参数)
        const pathname = window.location.pathname;

        // 使用 URLSearchParams 解析参数
        const params = new URLSearchParams(window.location.search);
        const queryParams = {};

        // 将参数拆分为键值对
        params.forEach((value, key) => {
            queryParams[key] = value;
        });

        // 返回结果对象
        return {
            url: fullUrl,       // 完整的网址
            path: pathname,     // 页面地址
            params: queryParams // 参数对象
        };
    }


})();