中检网自动学习

中国检察教育培训网络学院全自动学习课程,只需自行进去班级中即可。

目前為 2024-08-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         中检网自动学习
// @namespace    https://github.com/chiupam
// @version      0.7
// @description  中国检察教育培训网络学院全自动学习课程,只需自行进去班级中即可。
// @author       chiupam
// @match        https://www.sppet.cn/portal/play.do*
// @match        https://www.sppet.cn/examine/index.html*
// @grant        none
// @license      GNU GPLv3
// ==/UserScript==

(async function () {
  'use strict';

  // 定义一个等待函数
  async function delay(seconds) {
      return new Promise(resolve => setTimeout(resolve, seconds * 1000));
  }

  // 封装获取按钮的函数
  async function getButton(xpath, n = 5) {
    let button;
    for (let i = 0; i < n; i++) {
      button = document.evaluate(
        xpath,
        document,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE, null
      ).singleNodeValue;

      // 如果成功获取元素,等待1秒退出循环
      if (button) {
        console.log(`第 ${i + 1} 次尝试:找到按钮`);
        console.log(button.textContent.trim());
        await delay(1);
        break;
      }

      // 等待2秒后重试
      console.log(`第 ${i + 1} 次尝试:未找到按钮,2秒后重试`);
      await delay(2);
    }
    return button;
  }

  if (window.location.href.includes('stady_detail')) {
    // 在学习详情页面执行以下操作
    console.log('当前在课程学习详情页面');

    const intervalId = setInterval(function() {
      // 获取所有继续学习的按钮元素
      const study = document.querySelectorAll('.btn');
      // 获取所有进度条的元素
      const progress = document.querySelectorAll('.el-progress__text');
      // 获取所有课程的名称
      const name = document.querySelectorAll('.text_title.oneEllipsis');

      for (let i = 0; i < progress.length; i++) {
        let progressText = progress[i].textContent.trim(); // 课程进度
        
        console.log(`序列: ${i + 1} | 进度: ${progressText}`)
        console.log(`${name[i].textContent.trim()}`)

        if (parseFloat(progressText) < 100) {
          console.log(`课程未学完,开始学习`);
          study[i].click();
          break; // 找到并点击按钮后,停止遍历
        } else {
          console.log(`已学完, 跳过`);
        };
      };

      clearInterval(intervalId); // 找到目标元素后,停止轮询

    }, 3000); // 每隔3秒检查一次

  } else {
    // 在播放页面执行以下操作

    // 获取第一个视频元素
    let video = document.querySelector('video');

    // 尝试获取按钮,最多尝试5次
    let button = await getButton('/html/body/div[2]/div[3]/div[2]');

    // 如果找到了按钮元素,则执行点击操作
    if (button) {
      button.click();

      // 将音量调整为0
      video.volume = 0;

      // 视频暂停时自动开始播放
      if (video.paused) video.play();

      // 等待5秒钟开始读取现在的学习时长
      await delay(5);
      let totalDuration = video.duration;

      // 循环检测视频是否已经播放完毕
      var checkInterval = setInterval(function() {
        // 获取当前视频播放音量并静音
        if (video.volume != 0) video.volume = 0;

        // 检查视频是否暂停并自动播放
        if (video.paused) video.play();

        // 获取当前视频播放时间
        if (video.currentTime >= totalDuration) {
          // 视频播放完毕后关闭窗口
          clearInterval(checkInterval);
          // 设置 localStorage,通知第一个页面执行刷新
          localStorage.setItem('refresh', 'true');
          // 关闭当前窗口
          window.close();
        }
      }, 5000); // 每5秒检查一次
    }
  }
})();

// 在第一个页面中,添加以下脚本用于检测 localStorage 变化并执行刷新
if (window.location.href.includes('stady_detail')) {
  window.addEventListener('storage', function(event) {
    if (event.key === 'refresh' && event.newValue === 'true') {
      localStorage.removeItem('refresh');
      location.reload();
    }
  });
}