// ==UserScript== // @name 山海安全微伴答案脚本 // @namespace http://tampermonkey.net/ // @version 1.2 // @license GPL-3.0 // @description 安全微伴考试答题脚本,大学生新生入学考试安全教育答案查询脚本 // @author 山海 // @icon https://www.google.com/s2/favicons?sz=64&domain=mycourse.cn // @match https://weiban.mycourse.cn/* // @grant GM_xmlhttpRequest // @downloadURL none // ==/UserScript== (function () { 'use strict'; function createButton() { const button = document.createElement('button'); button.innerHTML = '查询答案'; button.style.position = 'fixed'; button.style.bottom = '0'; button.style.right = '0'; button.style.zIndex = '9999'; button.style.width = '35px'; button.style.backgroundColor = 'lightgreen' button.style.height = '50px'; button.style.border = '0px'; return button; } function createInfoBox() { const infoBox = document.createElement('div'); infoBox.style.position = 'fixed'; infoBox.style.top = '10px'; infoBox.style.right = '10px'; infoBox.style.zIndex = '9999'; infoBox.style.backgroundColor = 'white'; infoBox.style.border = '1px solid #ccc'; infoBox.style.padding = '10px'; infoBox.style.maxWidth = '300px'; infoBox.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)'; infoBox.style.display = 'none'; // Initially hidden return infoBox; } function getQuestionData() { const typeElement = document.querySelector('.quest-category'); const questionElement = document.querySelector('.quest-stem'); if (!typeElement || !questionElement) { console.error('找不到问题类型或问题内容的元素'); return null; } const type = typeElement.innerText; const questionText = questionElement.innerText.substring(3); return { type, questionText }; } function sendRequest(type, questionText, infoBox) { infoBox.innerText = '查询中...'; infoBox.style.backgroundColor = 'lightyellow'; infoBox.style.display = 'block'; GM_xmlhttpRequest({ method: 'GET', url: `http://answer.bmct.cn/query_answer.php?type=${type}&question=${questionText}`, headers: { 'Content-Type': 'application/json', }, onload: function (response) { const data = JSON.parse(response.responseText); if (data.success === true) { handleApiResponse(data, type, infoBox,questionText); } else { infoBox.innerText = `API请求失败: ${data.message}`; infoBox.style.backgroundColor = 'mistyrose'; } }, }); } function handleApiResponse(data, type, infoBox,questionText) { if (type === '多选题' || type === '单选题') { const answers = data.answer.split('||||'); const optionElements = document.querySelectorAll('.quest-option-top'); for (const answer of answers) { for (const optionElement of optionElements) { const optionText = optionElement.innerText.substring(2); if (optionText === answer) { optionElement.click(); break; } } } document.getElementsByClassName('mint-button-text')[2].click(); infoBox.innerText = `问题: ${questionText}\n答案: ${data.answer.split("||||")}\n状态: 已回答`; infoBox.style.backgroundColor = 'lightgreen'; } else { infoBox.innerText = '未知类型问题,不处理'; infoBox.style.backgroundColor = 'mistyrose'; } } const button = createButton(); const infoBox = createInfoBox(); document.body.appendChild(button); document.body.appendChild(infoBox); button.addEventListener('click', function () { const questionData = getQuestionData(); if (questionData) { const { type, questionText } = questionData; sendRequest(type, questionText, infoBox); } }); })();