skybreak / BaiExamTip

// ==UserScript==
// @name         BaiExamTip
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  It's a plugin to get the tip for BAI questions. Abuse is not allowed.
// @author       Darren
// @match        https://lmgr.bai.org/*
// @icon        https://lmgr.bai.org/favicon.ico
// @grant        none
// @license MIT
// ==/UserScript==
(function () {
  'use strict';

  // Your code here...
  var BaiAnswerFinder = {
    mode: {
      mode1: 1,
      mode2: 2,
      unknown: -1,
    },
    showAnwser() {
      var anwser = this.getAnwser();
      this.renderPanel(anwser);
    },
    renderPanel(anwser) {
      var css = `<style>
        .anwserPanel{
              position: fixed;
              top: 200px;
              right: 20px;
              opacity: 0.8;
              background-color: #ece4f5;
              border-radius: 4px;
              width: 300px;
              height: 240px;
              z-index: 999;
        }
        .correctAnswer{
         font-weight:bold;
        }
        </style>`;
      var html = `<div class='anwserPanel'>
       <p class='anwserHead'>Correct Answser(click to refresh the answser):</p>
       <p class='correctAnswer'>${anwser}</p>
       </div>`;
      var anwserP = document.querySelector('.correctAnswer');
      if (anwserP) {
        anwserP.innerHTML = anwser;
      } else {
        var div = document.createElement('div');
        div.innerHTML = css + html;
        document.body.appendChild(div);
      }
    },
    getExamType() {
      if (window.cp && window.cp.CPProjInit) {
        return { type: this.mode.mode1, containerId: 'main_container' };
      } else {
        return { type: this.mode.mode2, containerId: 'assessment' };
      }
    },
    getAnwser() {
      var examType = this.getExamType();
      if (examType.type == this.mode.mode1) {
        return this.getAnwserByMode1();
      } else if (examType.type == this.mode.mode2) {
        return this.getAnwserByMode2();
      } else {
        console.error('Unknown model:use mode2');
        return this.getAnwserByMode1();
      }
    },
    getAnwserByMode1() {
      var id = document.querySelector('.cp-frameset input[type=checkbox],.cp-frameset input[type=radio]').closest('.cp-frameset').id;
      var a = cp.D[id];
      a = cp.getQuestionObject(a.sn ? a.sn : a.apsn);
      var result = a.getCorrectAnswerAsString();
      if (result.indexOf(';') > -1) {
        return result.split(';').map(m => String.fromCharCode(64 + parseInt(m)));;
      }
      return result;
    },
    getAnwserByMode2() {
      return arrOptions.filter(t => t.isCorrect).map(t => t.txtOption);
    },
    delay(delayMs, func) {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve(func());
        }, delayMs);
      });
    },
    async retry(retryTimes, delayMs, func) {
      var count = 0, stop = false;
      while (count < retryTimes && !stop) {
        console.log('try ' + count);
        stop = await this.delay(delayMs, func);
        count++;
      }
    },
    setup() {
      var examType = BaiAnswerFinder.getExamType();
      if (!examType.containerId) { return true; }
      var container = document.getElementById(examType.containerId);
      if (!container && document.readyState!='complete') return false;
      if (examType.type == BaiAnswerFinder.mode.mode1) {

      } else if (examType == BaiAnswerFinder.mode.mode2) {

      } else {

      }
       container.addEventListener("click", (e) => {
          BaiAnswerFinder.delay(100, () => BaiAnswerFinder.showAnwser());
        }, false);
      return true;
    }
  };

  BaiAnswerFinder.retry(100,1000,BaiAnswerFinder.setup);
    window.BaiAnswerFinder=BaiAnswerFinder;
})();