Greasy Fork

来自缓存

Greasy Fork is available in English.

中研企课堂全能小助手

视频自动播放,自动答题并下载答案

当前为 2024-03-01 提交的版本,查看 最新版本

// ==UserScript==
// @name         中研企课堂全能小助手
// @namespace    [email protected]
// @version      0.0.3
// @description  视频自动播放,自动答题并下载答案
// @author       Caosh
// @match        https://ent.toujianyun.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=toujianyun.com
// @grant        GM_addElement
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  getUrlType();

  // 判断网址类型
  function getUrlType() {
    var url = window.location.href;
    if (url.includes("course")) {
      // 课程目录
      addAutoExamButton();
    } else if (url.includes("report")) {
      // 做题记录
      if (GM_getValue("report-use") == "yes") {
        setTimeout(() => {
          gotoAnswer();
        }, 2000);
      }
    } else if (url.includes("answer")) {
      // 答案
      if (GM_getValue("answer-use") == "yes") {
        setTimeout(() => {
          getAnswer();
        }, 2000);
      }
    } else if (url.includes("exercise")) {
      // 考试
      if (GM_getValue("exercise-use") == "yes") {
        setTimeout(() => {
          autoExam();
        }, 2000);
      }
    } else if (url.includes("lesson")) {
      // 视频播放页
      setTimeout(() => {
        autoDisplayVideo();
      }, 2000);
    }
  }

  // 添加自动考试按钮
  function addAutoExamButton() {
    var buttons = document.querySelector("div[class='info-study clearfix']");
    var autoExamButton = GM_addElement(buttons, "button", {
      id: "auto-exam-button",
      textContent: "自动答题",
    });
    // 设置按钮样式
    autoExamButton.classList.add("btn", "btn-lg", "btn-orange", "filleted");
    autoExamButton.addEventListener("click", startAutoExam);
  }

  // 开始自动考试
  function startAutoExam() {
    // 考试列表
    var examList = document.querySelectorAll(
      "[class*='new-detail-list-label detail-list-exercise']"
    );
    if (examList && examList.length != 0) {
      GM_setValue("exercise-use", "no");
      GM_setValue("report-use", "no");
      GM_setValue("answer-use", "no");
      // 所有考试答案
      GM_setValue("all-exam-answer", "");
      // 当前考试答案
      GM_setValue("current-exam-answer", "");
      // 是否已经取得答案
      GM_setValue("has-answer", "yes");
      // 遍历所有考试
      traversalExam(0, examList);
    }
  }

  // 自动进入考试页面/答题记录页面,并获取答案
  function traversalExam(index, examList) {
    if (index <= examList.length) {
      // 是否已经返回结果
      if (GM_getValue("has-answer") === "yes") {
        var allExamAnswer = GM_getValue("all-exam-answer");
        // 当前进行的考试答案
        var currentExamAnswer = GM_getValue("current-exam-answer");
        GM_setValue("all-exam-answer", allExamAnswer + currentExamAnswer);
        // 下一场考试节点
        var nextExamButton = examList[index];
        if (nextExamButton) {
          GM_setValue("current-exam-answer", "");
          GM_setValue("has-answer", "no");
          GM_setValue("exercise-use", "yes");
          GM_setValue("report-use", "yes");
          nextExamButton.click();
          traversalExam(index + 1, examList);
        } else {
          // 保存文件名
          var fileName1 =
            document.getElementsByClassName("info-title")[0].innerText + ".txt";
          // 课程考试结束
          saveAnswer(GM_getValue("all-exam-answer"), fileName1);
          GM_setValue("all-exam-answer", "");
          GM_setValue("current-exam-answer", "");
          GM_setValue("has-answer", "");
          GM_setValue("exercise-use", "no");
          GM_setValue("report-use", "no");
          alert("当前课程已完成");
        }
      } else {
        // 正在等待结果
        setTimeout(() => {
          traversalExam(index, examList);
        }, 2000);
      }
    } else {
      // 保存文件名
      var fileName2 =
        document.getElementsByClassName("info-title")[0].innerText + ".txt";
      // 课程考试结束
      saveAnswer(GM_getValue("all-exam-answer"), fileName2);
      GM_setValue("all-exam-answer", "");
      GM_setValue("current-exam-answer", "");
      GM_setValue("has-answer", "");
      GM_setValue("exercise-use", "no");
      GM_setValue("report-use", "no");
      alert("当前课程已完成");
    }
  }

  // 保存文本到本地
  function saveAnswer(text, fileName) {
    var a = document.createElement("a");
    a.style.display = "none";
    a.href = "data:text/plain;charset=utf-8," + encodeURIComponent(text);
    a.download = fileName;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  }

  // 从答题记录页进入答案查看页
  function gotoAnswer() {
    var answerButton = document
      .getElementsByClassName("table table-hover table-list")[0]
      .getElementsByTagName("tbody")[0]
      .getElementsByTagName("a")[1];
    GM_setValue("answer-use", "yes");
    GM_setValue("report-use", "no");
    answerButton.click();
  }

  // 自动考试(默认选择A选项后提交)
  function autoExam() {
    var questionList = document.getElementsByClassName("question-wrapper");
    if (questionList.length != 0) {
      for (var i = 0; i < questionList.length; i++) {
        var question = questionList[i];
        var optionA = question.getElementsByTagName("input")[0];
        if (optionA) {
          optionA.click();
        }
      }
    }
    var submitButton = document.getElementsByClassName(
      "btn btn-blue J-save-btn"
    )[0];
    if (submitButton) {
      GM_setValue("answer-use", "yes");
      submitButton.click();
    }
    GM_setValue("exercise-use", "no");
  }

  // 获取答案
  function getAnswer() {
    var result = "";
    // 序号-选项映射
    const map = new Map();
    map
      .set(0, "A")
      .set(1, "B")
      .set(2, "C")
      .set(3, "D")
      .set(4, "E")
      .set(5, "F")
      .set(6, "G")
      .set(7, "H")
      .set(8, "I")
      .set(9, "J");
    // 题目列表
    var questionList = document.getElementsByClassName("question-wrapper");
    if (questionList.length == 0) {
      GM_setValue("current-exam-answer", "未获取到题目列表");
      GM_setValue("has-answer", "yes");
      GM_setValue("answer-use", "no");
      window.close();
    } else {
      for (var i = 0; i < questionList.length; i++) {
        // 题目对象
        var question = questionList[i];
        // 题目
        var questionTitle =
          question.getElementsByClassName("question-title")[0].innerText + "\n";
        // 选项
        var option = "";
        // 选项列表
        var questionOptionList = question.getElementsByTagName("li");
        for (var j = 0; j < questionOptionList.length; j++) {
          var questionOption =
            map.get(j) + ":" + questionOptionList[j].innerText;
          option = option + questionOption + "\n";
        }
        // 答案
        var questionAnswer =
          question.getElementsByClassName("question-answer")[0].innerText +
          "\n";
        result = result + questionTitle;
        result = result + option;
        result = result + questionAnswer;
      }
      GM_setValue("current-exam-answer", result);
      GM_setValue("has-answer", "yes");
      GM_setValue("answer-use", "no");
      window.close();
    }
  }

  // 自动播放视频
  function autoDisplayVideo() {
    setInterval(function () {
      // 获取正在播放的视频
      var current_video = document.getElementsByTagName("video")[0];
      // 判断视频是否暂停
      if (current_video.paused) {
        // 如果视频暂停
        // 获取弹窗中的选项([1]为A选项)
        var check_option = document.getElementsByTagName("input")[1];
        // 判断是否存在该选项
        if (check_option) {
          // 选择该选项
          check_option.checked = true;
          // 点击提交
          var submit_button = document.getElementsByClassName(
            "btn btn-blue J-save-answer J-submit-answer"
          )[0];
          if (submit_button) {
            submit_button.click();
          }
          // 点击继续学习
          var continue_button = document.getElementsByClassName(
            "btn btn-blue J-submit-answer btn-continue"
          )[0];
          if (continue_button) {
            continue_button.click();
          }
        }
        // 如果视频仍旧处于暂停状态
        if (current_video.paused) {
          // 判断视频是否播放完毕
          if (
            Math.trunc(current_video.currentTime) ==
            Math.trunc(current_video.duration)
          ) {
            // 跳转下一视频
            console.log("视频播放结束,跳转下一视频");
            nextVideo();
          } else {
            // 恢复播放
            console.log("视频播放暂停,恢复播放");
            current_video.play();
          }
        }
      }
    }, 3000);
  }

  // 获取当前视频的位置
  function getPosition(element, element_list) {
    for (var i = 0; element != element_list[i]; i++) {}
    return i;
  }

  // 播放下一个视频
  function nextVideo() {
    // 当前播放视频URL
    var currentVideoUrl = window.location.href;
    // 视频播放列表
    var videoList = document.querySelectorAll(
      "a.catalogue-item.new-catalogue-item"
    );
    // 当前播放视频在列表中的序号
    var currentPosition = getPosition(currentVideoUrl, videoList);
    if (currentPosition + 1 >= videoList.length) {
      console.log("课程学习完毕");
      alert("课程学习完毕");
    } else {
      // 下一个视频的URL地址
      var nextVideoUrl = videoList[currentPosition + 1].href;
      // 跳转下一个视频
      window.location.href = nextVideoUrl;
    }
  }
})();