脚本助手--安全微伴刷课插件(2024年新界面)

安全微伴刷课助手(2024年新界面)

// ==UserScript==
// @name         脚本助手--安全微伴刷课插件(2024年新界面)
// @version      1.0.3
// @description  安全微伴刷课助手(2024年新界面)
// @author       hhxcaz
// @match      *://weiban.mycourse.cn/*
// @match      https://mcwk.mycourse.cn/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant 		 none
// @run-at       document-end
// @license GPL-v3
// @namespace https://greasyfork.org/users/737775
// ==/UserScript==


let taskNumber = 0;

(function () {
  "use strict";

  const addHistoryEvent = function (type) {
    var originalMethod = history[type];
    return function () {
      var recallMethod = originalMethod.apply(this, arguments);
      var e = new Event(type);
      e.arguments = arguments;
      window.dispatchEvent(e);
      return recallMethod;
    };
  };
  history.pushState = addHistoryEvent("pushState");
  history.replaceState = addHistoryEvent("replaceState");

  var getVal = function (fun1, fun2) {
    let id = setInterval(() => {
      var val = fun1();
      if (val.length) {
        clearInterval(id);
        fun2(val);
      }
    }, 100);
  };
  var start = function (e) {
    $(function () {
      setTimeout(() => {
        //console.log(window.location);
        // 第一阶段 在主页
        if (window.location.hash === "#/") {
          getVal(
            () => {
              return $("div.task-block");
            },
            (res) => {
              //console.log(res);
              setTimeout(() => {
                res[0].click();
              }, 200);
            }
          );
        }
        // 第二阶段 在学习任务
        var reg = /course.*projectId.*projectType.*/;

        if (window.location.hash.match(reg)) {
          getChapterList();
        }
      }, 1000);
    });
  };
  window.addEventListener("pushState", start);
  window.addEventListener("popstate", () => {
    location.reload();
  });
  //console.log("脚本执行");
  // 第四阶段 此时在异域iframe中
  if (window.location.href.indexOf("mcwk.mycourse.cn/course/") != -1) {
    // console.log(window.location.href);
    $(function () {
      setTimeout(() => {
        try {
          console.log(exportRoot.currentFrame);
        } catch (e) {}
        try {
          var userid = getQueryString("userCourseId");
          var jiaoxuejihuaid = getQueryString("tenantCode");
          var finishWxHost = document.referrer
            .replace("http://", "")
            .replace("https://", "")
            .split("/")[0];
          if (
            document.referrer == "" ||
            document.referrer == null ||
            document.referrer == undefined
          ) {
            finishWxHost = "weiban.mycourse.cn";
          }

          var webUrl = window.location.href;
          function getRecordUrl(url) {
            if (url.indexOf("open.mycourse.cn") > 0) {
              return `https://open.mycourse.cn/proteus/usercourse/finish.do`;
            } else {
              return `https://weiban.mycourse.cn/pharos/usercourse/v1/${methodToken}.do`;
            }
          }
          var finishWxUrl = getRecordUrl(webUrl);
          if (finishWxHost.indexOf("218.25.139.161") > 0) {
            //finishWxUrl = "http://"+finishWxHost+"/pharos/usercourse/finish.do";
            finishWxUrl =
              "https://" + finishWxHost + "/pharos/usercourse/finish.do";
          }

          var finishData = { userCourseId: userid, tenantCode: jiaoxuejihuaid };

          $.ajax({
            async: false,
            url: finishWxUrl,
            type: "GET",
            dataType: "jsonp",
            data: finishData,
            timeout: 5000,

            success: function (data) {
              backToList();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {},
          });
        } catch (e) {
          alert("报了啥错误" + e);
        }
      }, 10000);
    });
  } else if (window.location.href.indexOf("weiban.mycourse.cn/") != -1) {
    //console.log(window.location.href);
    $(function () {
      start();
    });
  }

  // Your code here...
})();

// 获取章节列表
function getChapterList() {
  console.log(taskNumber,' taskNumber');
  // 获取章节 列表
  const list = document.querySelectorAll(".img-texts-list .img-texts-item");
  const clickList = [];
  for (let i = 0; i < list.length; i++) {
    const item = list[i];
    // console.log(item, !item.className.includes('passed'));
    if (!item.className.includes("passed")) clickList.push(item);
  }
  if (clickList.length > 0) {
    clickList[0].click();
  } else {
    // 获取.van-collapse-item 元素后面的.van-collapse-item兄弟元素
    taskNumber += 1;
    const next = document.querySelector(
      ".van-collapse-item:nth-child(" + taskNumber + ")"
    );
    if (next) {
      next.querySelector(".van-cell__title").click();
      setTimeout(getChapterList, 1000);
    }
    
  }
}