matthew36 / Auto Volt

// ==UserScript==
// @name         Auto Volt
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  help you do the UST vocab list homework
// @author       programmer G7
// @match        http://volt.cle.ust.hk/mw/stu/index.php
// @grant        none
// @copyright 2020, matthew36 (https://openuserjs.org/users/matthew36)
// @license MIT
// ==/UserScript==

var vocabList = [];
var result = "";
var currentID = 0;
function getVoltResult(){
    return document?.getElementById("quick_reference_frame")?.contentDocument?.getElementsByTagName("body")[0]?.getElementsByTagName("div")[0];
}
function getVocabName(){
	return getVoltResult()?.getElementsByTagName("b")[0]?.innerText;
}
function getPartOfSpeech(){
	return getVoltResult()?.getElementsByTagName("sub")[0]?.innerText;
}
function getDefinition(){
	return getVoltResult()?.getElementsByTagName("b")[1]?.innerText;
}
function getExample(){
	return getVoltResult()?.getElementsByTagName("i")[0]?.innerText;
}

function getTableInCurrentPage(currentID){
	return `${currentID+1}	${getVocabName()}	\"Definition: ${getDefinition()} \n\nExample sentence: ${getExample()} \n\nWhat I've learned: ${getVocabName()} is ${getPartOfSpeech()}\"	Vocab List`;
}

function setInput(str){
	var input = document.getElementById("word");
	input.value = str;
}
function clickSearchButton(){
	var button = document.getElementById("button");
	button.click();
}
function search(str){
	setInput(str);
	clickSearchButton();
}
function copyToClipboard(str){
    const el = document.createElement('textarea');
    el.value = str;
    el.setAttribute('readonly', '');
    el.style.position = 'absolute';
    el.style.left = '-9999px';
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
}
function addNextVocabToResult(){
    var vocab = vocabList[currentID];
    if(vocab==undefined) {
        console.log("completed vocab list searching");
        console.log(result);
        copyToClipboard(result);
        alert("completed volt, the result have been copied in your clipboard, please paste it into the google sheet then copy and paste it into your google doc")
        return;
    }
    search(vocab);//search the vocab
    setTimeout(function () {
        result += getTableInCurrentPage(currentID)+"\n";//add it into result
        currentID++;
        addNextVocabToResult(currentID);

      }, 1000);
}
function generateVocabListHomework(){
    resetAll();
    AskForVocabList();
    addNextVocabToResult();
}
function AskForVocabList(){
    var input = prompt("vocab List", "please use enter to split each vocab")
    if(input == undefined) return;
    if(input.length <= 0) return;
	vocabList = input.split("\n");
}
function resetAll(){
    vocabList = [];
    result = "";
    currentID = 0;
}
(function() {
    'use strict';
    // Your code here...
    var el = document.createElement("button");
    el.value = "DO HOMEWORK"
    el.innerText = "DO VocabList homework"
    el.style.width = innerWidth+"px"
    el.style.height = "300px"
    el.onclick = generateVocabListHomework
    document.body.appendChild(el);
})();