wsxy_handleExams

网上学院函数库:自动答题

目前為 2020-02-02 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/395966/769534/wsxy_handleExams.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          wsxy_handleExams
// @namespace     Vionlentmonkey
// @version       0.4
// @description   网上学院函数库:自动答题
// @require       https://greasyfork.org/scripts/395748-wsxy-getdata/code/wsxy_getData.js
// @grant         none
// ==/UserScript==

/** 最新知识库。
 * 年终考试题库在此公布,需要层层打开 iframe 最终获取 swf 地址,然后以 IE 打开,
 * 打印所有页,输出 PDF,二次打印输出 PDF,再用 Word 等进行 OCR。
 */
const openKnowledge = () => {
  const knowledges = document.querySelectorAll('#knowledgeType a[title][href="#"]');
  for (let knowledge of knowledges) {
    const kURL =
      location.origin +
      '/sfxzwsxy/jypxks/modules/learn/document/learn/document_learn_text.jsp?fkNodePk=' +
      knowledge
        .getAttribute('onclick')
        .split('(')[1]
        .split(')')[0];
    //console.log(kURL);
    knowledge.href = kURL;
    knowledge.onclick = '';
    knowledge.target = '_blank';
  }
};

// 清理两处“参加考试”按钮,使其点击时在新标签页打开考题及答案。
const handelExamList = async exams => {
  for (let exam of exams) {
    const examURL = location.origin + '/sfxzwsxy/' + exam.getAttribute('onclick').split("'")[1];
    exam.href = examURL;
    exam.onclick = '';
    exam.target = '_blank';
    let course_pk = '';
    let answerURL = '';
    // 在线考试 - 课程考试 iframe
    if (examURL.includes('course_pk=')) {
      coursePk = examURL.split('course_pk=')[1].split('&')[0];
      answerURL =
        location.origin +
        '/sfxzwsxy//jypxks/modules/train/course/subject_list.jsp?coursePk=' +
        coursePk +
        '&op=view';
    }
    // 首页 - 考试提醒 - 课程考试
    else if (exam.getAttribute('title')) {
      const csDataObj = await getCourseData();
      const exam_courses = csDataObj.exam_courses;
      const title = exam.getAttribute('title');
      for (let e of exam_courses) {
        if (e.course_name === title) {
          course_pk = String(e.course_pk);
          answerURL =
            location.origin +
            '/sfxzwsxy//jypxks/modules/train/course/subject_list.jsp?coursePk=' +
            course_pk +
            '&op=view';
        }
      }
    }
    exam.addEventListener('click', () => {
      GM_openInTab(answerURL, true);
      GM_notification('答案已同步在隔壁标签页打开。\n暂请考试结束后手动关闭。');
    });
  }
};

// 打开考卷后自动答题交卷
const autoExamineTest = async () => {
  // 本考试所有试题
  let topics = document.getElementsByClassName('topic-tms');
  for await (let topic of topics) {
    // 题号
    let pkid = topic.querySelector('a[pkid]').getAttribute('pkid');
    // 本题答案
    let subjectDataMap = await getSubjectData(pkid);
    // 本题选项
    let options = topic.querySelectorAll('.tms-Right-wrong > p > a');
    for (let option of options) {
      let optionText = option.textContent.trim();
      if (subjectDataMap.get('questionType') === '判断题') {
        if (option.textContent.trim() === subjectDataMap.get('judgementAnswer')) {
          option.click();
        }
      } else {
        // 选择题选项内容带着序号与空格,如“A ”,故获取第三个字符开始的子串
        if (subjectDataMap.get(optionText.substring(2)) === '是') {
          option.click();
        }
      }
    }
  }
  // 交卷
  if (document.getElementsByClassName('subline _submit').length === 1) {
    document.getElementsByClassName('subline _submit')[0].click();
  }
  // 确认
  if (document.getElementsByClassName('layui-layer-btn0').length === 1) {
    document.getElementsByClassName('layui-layer-btn0')[0].click();
  }
};