Greasy Fork is available in English.

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

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

// ==UserScript==
// @name         山海|安全微伴高分脚本|大学生新生入学考试安全教育答题
// @author       山海不爱玩
// @version      1.4
// @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.position = 'fixed';
    button.style.bottom = '0';
    button.style.right = '0';
    button.style.zIndex = '9999';
    button.style.width = '60px';
    button.style.backgroundColor = 'lightgreen';
    button.style.height = '60px';
    button.style.border = '0px';

    return button;
  }

  // Function to create an info box for displaying messages
  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 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?type=${type}&question=${questionText}`;

    GM_xmlhttpRequest({
      method: 'GET',
      url: apiUrl,
      headers: {
        'Content-Type': 'application/json',
      },
      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, infoBox);
    }
  }

  // Function to handle API errors
  function handleApiError(error, infoBox) {
    infoBox.innerText = `API请求失败: ${error}`;
    infoBox.style.backgroundColor = 'mistyrose';
  }

  // Create the "查询答案" button and info box
  const button = createButton();
  const infoBox = createInfoBox();
  document.body.appendChild(button);
  document.body.appendChild(infoBox);

  // Add a click event listener to the button
  button.addEventListener('click', function () {
    const questionData = getQuestionData();
    if (questionData) {
      const { type, questionText } = questionData;
      sendRequest(type, questionText, infoBox);
    }
  });
})();