山海|安全微伴高分脚本|大学生新生入学考试安全教育答题

安全微伴高分脚本,旨在帮助大学生新生入学考试安全教育答题。安全微伴考试脚本功能介绍:1. 自动查询当前问题的答案,2. 为多选题和单选题自动选择正确答案,3. 自动切换下一题。

// ==UserScript==
// @name         山海|安全微伴高分脚本|大学生新生入学考试安全教育答题
// @author       山海不爱玩
// @version      1.6
// @license      GPL-3.0
// @description  安全微伴高分脚本,旨在帮助大学生新生入学考试安全教育答题。安全微伴考试脚本功能介绍:1. 自动查询当前问题的答案,2. 为多选题和单选题自动选择正确答案,3. 自动切换下一题。
// @author       山海
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mycourse.cn
// @match        https://weiban.mycourse.cn/*
// @match        http://weiban.mycourse.cn/*
// @grant        GM_xmlhttpRequest
// @namespace    http://tampermonkey.net/
// ==/UserScript==

(function () {
  'use strict';

  // Function to create the "查询答案" button
  function createButton() {
    const button = document.createElement('button');
    button.innerHTML = '查询答案';
    button.style.cssText = `
      position: fixed;
      bottom: 10px;
      right: 10px;
      z-index: 9999;
      width: 100px;
      height: 40px;
      background-color: #4CAF50; /* Green */
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      font-size: 16px;
      outline: none;
      transition: background-color 0.3s, transform 0.1s;
    `;
    button.onmouseover = function() { this.style.backgroundColor = '#45a049'; };
    button.onmouseout = function() { this.style.backgroundColor = '#4CAF50'; };
    return button;
  }

  // Function to create an info box for displaying messages
function createInfoBox() {
  const infoBox = document.createElement('div');
  infoBox.style.cssText = `
    position: fixed;
    top: 10px;
    left: 10px; /* 更改为 left 而不是 right */
    z-index: 9999;
    background-color: #fff;
    border: 1px solid #ccc;
    padding: 15px;
    max-width: 300px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    display: none;
    border-radius: 5px;
    font-size: 14px;
    line-height: 1.5;
  `;
  return infoBox;
}


  // Function to extract question data from the current page
  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 to send a request to an external API
  function sendRequest(type, questionText, infoBox) {
    infoBox.innerText = '查询中...';
    infoBox.style.backgroundColor = 'lightyellow';
    infoBox.style.display = 'block';

    const apiUrl = `http://answer.bmct.cn/query_answer.php`;
    GM_xmlhttpRequest({
      method: 'POST',
      url: apiUrl,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: `question=${encodeURIComponent(questionText)}`,
      onload: function (response) {
        handleApiResponse(response, type, infoBox, questionText);
      },
      onerror: function (error) {
        handleApiError(error, infoBox);
      },
    });
  }

  // Function to handle the API response
  function handleApiResponse(response, type, infoBox, questionText) {
    const data = JSON.parse(response.responseText);
    if (data.success === true) {
      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答案:${answers}\n状态: 已回答`;
        infoBox.style.backgroundColor = 'lightgreen';
      } else {
        infoBox.innerText = '未知类型问题,不处理';
        infoBox.style.backgroundColor = 'mistyrose';
      }
    } else {
      handleApiError(data.message + '可以手动做完这道题后点击【下一题】继续使用【查询答案】,特殊题目答案:【三年以下】【二O三五年】', infoBox);
    }
  }

  // Function to handle API errors
  function handleApiError(error, infoBox) {
    infoBox.innerText = `API请求失败: ${error}`;
    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) {
      sendRequest(questionData['type'], questionData['questionText'], infoBox);
    }
  });
})();