// ==UserScript== // @name WJX producer // @namespace https://blog.csdn.net/SundaySmarty // @version 1.0.4 // @description Make producing wjx easier // @author SundaySmarty // @match https://www.wjx.cn/mysojump/questionnairemng/designnew.aspx* // @icon https://www.wjx.cn/images/commonImgPC/logo@2x.png // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; function sleep(time){ return new Promise(resolve => setTimeout(resolve, time)); } Array.prototype.remove = function(val) { var index = this.indexOf(val); if(index > -1) { this.splice(index, 1); } }; function getRadioValue(radioName){ let radio = document.getElementsByName(radioName); for(let i = 0; i < radio.length; i++){ if(radio[i].checked) { return radio[i].value; } } } // Basic structure let producerButton = document.createElement("button"); producerButton.textContent = "WJX producer"; producerButton.style.paddingLeft = "20px"; producerButton.style.paddingRight = "20px"; producerButton.style.height = "30px"; producerButton.style.textAlign = "center"; producerButton.style.background = "thistle"; producerButton.style.color = "white"; producerButton.style.border = "1px solid thistle"; producerButton.style.borderRadius = "4px"; producerButton.style.position = "fixed"; producerButton.style.top = "0"; producerButton.style.right = "0"; producerButton.style.zIndex = "2000"; producerButton.addEventListener("click", clickProducerButton); let producer = document.createElement("div"); producer.style.width = "40%"; producer.style.height = "100%"; producer.style.background = "white"; producer.style.position = "fixed"; producer.style.top = "0"; producer.style.right = "0"; producer.style.display = "none"; producer.style.zIndex = "1000"; let producerContainer = document.createElement("div"); producerContainer.style.width = "80%"; producerContainer.style.height = "80%"; producerContainer.style.padding = "10%"; producerContainer.style.overflowY = "auto"; producerContainer.style.overflowX = "hidden"; let producerHeaderContainer = document.createElement("div"); producerHeaderContainer.style.width = "90%"; producerHeaderContainer.style.textAlign = "center"; producerHeaderContainer.style.margin = "5%"; let producerHeader = document.createElement("p"); producerHeader.innerHTML = "WJX producer"; producerHeader.style.fontSize = "24px"; producerHeader.style.fontWeight = "bold"; producerHeader.style.color = "black"; let contentContainer = document.createElement("div"); contentContainer.style.width = "100%"; let usageButton = document.createElement("button"); usageButton.textContent = "Usage"; usageButton.style.paddingLeft = "20px"; usageButton.style.paddingRight = "20px"; usageButton.style.height = "30px"; usageButton.style.textAlign = "center"; usageButton.style.background = "thistle"; usageButton.style.color = "white"; usageButton.style.border = "1px solid thistle"; usageButton.style.borderRadius = "4px"; usageButton.style.position = "fixed"; usageButton.style.top = "0"; usageButton.style.zIndex = "3000"; usageButton.addEventListener("click", clickUsageButton); let usage = document.createElement("div"); usage.style.width = "40%"; usage.style.height = "100%"; usage.style.background = "white"; usage.style.position = "fixed"; usage.style.top = "0"; usage.style.right = "0"; usage.style.display = "none"; usage.style.zIndex = "2500"; let usageContainer = document.createElement("div"); usageContainer.style.width = "80%"; usageContainer.style.height = "80%"; usageContainer.style.padding = "10%"; usageContainer.style.overflowY = "auto"; usageContainer.style.overflowX = "hidden"; let usageHeaderContainer = document.createElement("div"); usageHeaderContainer.style.width = "90%"; usageHeaderContainer.style.textAlign = "center"; usageHeaderContainer.style.margin = "5%"; let usageHeader = document.createElement("p"); usageHeader.innerHTML = "WJX producer usage"; usageHeader.style.fontSize = "24px"; usageHeader.style.fontWeight = "bold"; usageHeader.style.color = "black"; let usageContentContainer = document.createElement("div"); usageContentContainer.style.width = "100%"; document.body.appendChild(producerButton); document.body.appendChild(producer); producer.appendChild(producerContainer); producerContainer.appendChild(producerHeaderContainer); producerHeaderContainer.appendChild(producerHeader); producerContainer.appendChild(contentContainer); producer.appendChild(usageButton); producer.appendChild(usage); usage.appendChild(usageContainer); usageContainer.appendChild(usageHeaderContainer); usageHeaderContainer.appendChild(usageHeader); usageContainer.appendChild(usageContentContainer); writePInUsage('A textbox will appear when you enter WJX Producer.'); writePInUsage('The format of your input in this textbox is "S/N of your question; number of choices; score of each question; available choices"(no quotation marks).'); writePInUsage('For example, if you enter "1~2,5;4;1;ABCDEFGH", then Question 1, 2 and 5 will be produced. Each of the three questions will have 4 choices and a score of 1. The available choices of question 1 and 5 will be ABCD and the available choices of question 2 will be EFGH.'); writeImgInUsage("https://s6.imgcdn.dev/rSdRe.png"); writePInUsage('After clicking Confirm Button, you can set answers for each of your questions as shown below.'); writeImgInUsage("https://s6.imgcdn.dev/rSWUC.png"); let canFocus = false; function clickProducerButton() { if(producer.style.display == "none") { producer.style.display = "block"; producerButton.textContent = "Close"; if(canFocus) { MCNumInput.focus(); } } else { producer.style.display = "none"; producerButton.textContent = "WJX producer"; } } function clickUsageButton() { if(usage.style.display == "none") { usage.style.display = "block"; usageButton.textContent = "Close"; producerButton.style.display = "none"; } else { usage.style.display = "none"; usageButton.textContent = "Usage"; producerButton.style.display = "block"; if(canFocus) { MCNumInput.focus(); } } } function writePInUsage(text) { let p = document.createElement("p"); p.style.width = "90%"; p.style.margin = "5%"; p.style.fontSize = "18px"; p.style.color = "black"; p.style.wordBreak = "normal"; p.innerHTML = text; usageContentContainer.appendChild(p); } function writeImgInUsage(imgURL) { let imgContainer = document.createElement("div"); imgContainer.style.textAlign = "center"; let img = document.createElement("img"); img.setAttribute("src", imgURL); img.style.width = "80%"; usageContentContainer.appendChild(imgContainer); imgContainer.appendChild(img); } let testQuestionBar = document.getElementById("divKaoShiT"); let MCNumInputList = []; if(testQuestionBar.style.display != "none") { let MCHeaderContainer = document.createElement("div"); MCHeaderContainer.style.width = "90%"; MCHeaderContainer.style.margin = "5%"; let MCHeader = document.createElement("p"); MCHeader.innerHTML = "Multiple Choice"; MCHeader.style.fontWeight = "bold"; MCHeader.style.fontSize = "20px"; MCHeader.style.color = "black"; contentContainer.appendChild(MCHeaderContainer); MCHeaderContainer.appendChild(MCHeader); let MCNumInputContainer = document.createElement("div"); MCNumInputContainer.style.width = "90%"; MCNumInputContainer.style.margin = "5%"; var MCNumInput = document.createElement("input"); MCNumInput.setAttribute("type", "text"); MCNumInput.style.width = "100%"; MCNumInput.style.height = "30px"; MCNumInput.style.fontSize = "20px"; contentContainer.appendChild(MCNumInputContainer); MCNumInputContainer.appendChild(MCNumInput); MCNumInputList.push(MCNumInput); var singleMultiContainer = document.createElement("div"); singleMultiContainer.style.width = "90%"; singleMultiContainer.style.margin = "5%"; var single = document.createElement("input"); single.setAttribute("type", "radio"); single.setAttribute("id", "single"); single.setAttribute("name", "MC"); single.setAttribute("value", 0); single.setAttribute("checked", true); let singleLabel = document.createElement("label"); singleLabel.innerHTML = "Single Answer "; singleLabel.style.color = "black"; singleLabel.style.fontSize = "15px"; singleLabel.setAttribute("for", "single"); var multi = document.createElement("input"); multi.setAttribute("type", "radio"); multi.setAttribute("id", "multi"); multi.setAttribute("name", "MC"); multi.setAttribute("value", 1); let multiLabel = document.createElement("label"); multiLabel.innerHTML = "Multi Answer "; multiLabel.style.color = "black"; multiLabel.style.fontSize = "15px"; multiLabel.setAttribute("for", "multi"); contentContainer.appendChild(singleMultiContainer); singleMultiContainer.appendChild(single); singleMultiContainer.appendChild(singleLabel); singleMultiContainer.appendChild(multi); singleMultiContainer.appendChild(multiLabel); var submitButtonContainer = document.createElement("div"); submitButtonContainer.style.width = "90%"; submitButtonContainer.style.margin = "5%"; submitButtonContainer.style.display = "flex"; submitButtonContainer.style.flexDirection = "column"; submitButtonContainer.style.alignItems = "center"; var confirmButton = document.createElement("button"); confirmButton.style.paddingLeft = "20px"; confirmButton.style.paddingRight = "20px"; confirmButton.style.height = "30px"; confirmButton.style.marginBottom = "3%"; confirmButton.textContent = "Confirm"; confirmButton.addEventListener("click", clickConfirmButton); var backButton = document.createElement("button"); backButton.style.display = "none"; backButton.style.paddingLeft = "20px"; backButton.style.paddingRight = "20px"; backButton.style.height = "30px"; backButton.style.marginBottom = "3%"; backButton.textContent = "Back"; backButton.addEventListener("click", clickBackButton); contentContainer.appendChild(submitButtonContainer); submitButtonContainer.appendChild(confirmButton); submitButtonContainer.appendChild(backButton); var covering = document.createElement("div"); covering.style.width = "100%"; covering.style.height = "100%"; covering.style.background = "grey"; covering.style.opacity = "0.4"; covering.style.position = "fixed"; covering.style.top = "0"; covering.style.left = "0"; covering.style.display = "none"; covering.style.zIndex = "800"; document.body.appendChild(covering); producer.addEventListener("keydown", function(e) { if(e.keyCode === 13) {clickConfirmButton();} }); canFocus = true; } let pos = 0; let numOfChoice = 0; let score = 0; let startChoice = []; let MCInputList = []; let continuousMC = []; let scoreAvailable = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 60, 70, 80, 90, 100]; function clickConfirmButton() { if(pos == 0) { let stop = false; MCNumInput.value = MCNumInput.value.replace(/\s*/g,""); // isEmpty if(MCNumInput.value.length == 0) stop = true; let input = MCNumInput.value.split(";"); // isFourPart if(!stop && input.length != 4) {stop = true; window.alert("Wrong Input!");} if(!stop) { // isPartEmpty for(let i = 0; i < input.length; i++) { if(input[i] == "") { stop = true; window.alert("Wrong Input!"); break; } } if(!stop) { // lastThreePart if(isNaN(Number(input[1])) || isNaN(Number(input[2])) || !(Number(input[1]) >= 2) || !(Number.isInteger(Number(input[1]))) || !(scoreAvailable.indexOf(Number(input[2])) >= 0) || !(/^[a-zA-Z]+$/.test(input[3]))) { stop = true; window.alert("Wrong Input!"); } if(!stop) { numOfChoice = Number(input[1]); score = Number(input[2]); input[3] = input[3].toUpperCase(); startChoice = input[3].split(""); // lastPartNum if(startChoice.length % numOfChoice != 0) { stop = true; window.alert("Wrong Input!"); } if(!stop) { // isFirstPartEmpty let MCNumSection = input[0].split(','); for(let i = 0; i < MCNumSection.length; i++) { if(MCNumSection[i] == "") { stop = true; window.alert("Wrong Input!"); break; } } if(!stop) { // checkOverlap continuousMC = []; let checkOverlapList = []; for(let i = 0; i < MCNumSection.length; i++) { let range = MCNumSection[i].split('~'); if(range.length == 1) { if(range[0] == "" || isNaN(Number(range[0])) || !(Number.isInteger(Number(range[0]))) || Number(range[0]) <= 0) { stop = true; window.alert("Wrong Input!"); break; } else { if(!checkOverlapList.includes(Number(range[0]))) checkOverlapList.push(Number(range[0])); else { stop = true; window.alert("Wrong Input!"); break; } continuousMC.push([Number(range[0]), 1]); } } else if(range.length == 2) { if(range[0] == "" || range[1] == "" || isNaN(Number(range[0])) || isNaN(Number(range[1])) || !(Number.isInteger(Number(range[0]))) || !(Number.isInteger(Number(range[1]))) || Number(range[1]) < Number(range[0]) || Number(range[0]) <= 0) { stop = true; window.alert("Wrong Input!"); break; } else { for(let j = Number(range[0]); j <= Number(range[1]); j++) { if(!checkOverlapList.includes(j)) checkOverlapList.push(j); else { stop = true; window.alert("Wrong Input!"); break; } } if(!stop) { let nAtALine = 15; if(getRadioValue("MC") == 1) nAtALine = 5; let j = Number(range[0]); while(j + nAtALine - 1 <= Number(range[1])) { continuousMC.push([j, nAtALine]); j += nAtALine; } if(Number(range[1]) >= j) continuousMC.push([j, Number(range[1]) - j + 1]); } } } else { stop = true; window.alert("Wrong Input!"); break; } } if(!stop) { pos++; MCNumInput.style.display = "none"; backButton.style.display = "block"; singleMultiContainer.style.display = "none"; MCInputList = []; continuousMC = continuousMC.sort(function(a, b){return a[0] - b[0];}); for(let j = 0; j < continuousMC.length; j++) { let MCTitleContainer = document.createElement("div"); MCTitleContainer.style.width = "90%"; MCTitleContainer.style.margin = "5%"; let MCTitle = document.createElement("p"); MCTitle.style.color = "black"; MCTitle.style.fontSize = "18px"; if(continuousMC[j][1] == 1) MCTitle.innerHTML = continuousMC[j][0]; else MCTitle.innerHTML = continuousMC[j][0] + '~' + (continuousMC[j][0] + continuousMC[j][1] - 1); let MCInputContainer = document.createElement("div"); MCInputContainer.style.width = "90%"; MCInputContainer.style.margin = "5%"; let MCInput = document.createElement("input"); MCInput.setAttribute("type", "text"); MCInput.style.width = "100%"; MCInput.style.height = "30px"; MCInput.style.fontSize = "20px"; contentContainer.insertBefore(MCTitleContainer, submitButtonContainer); MCTitleContainer.appendChild(MCTitle); contentContainer.insertBefore(MCInputContainer, submitButtonContainer); MCInputContainer.appendChild(MCInput); MCInputList.push(MCInput); } for(let k = 0; k < MCInputList.length; k++) { let prev = k - 1; let next = k + 1; if(prev < 0) prev += MCInputList.length; if(next == MCInputList.length) next = 0; // eslint-disable-next-line no-loop-func MCInputList[k].addEventListener("keydown", function(e) { if(e.keyCode === 38) { MCInputList[prev].focus(); } else if(e.keyCode === 40) { MCInputList[next].focus(); } }); } covering.style.display = "block"; confirmButton.disabled = true; backButton.disabled = true; for(let i = 0; i < MCInputList.length; i++) MCInputList[i].disabled = true; (async () => { let lastPos = await clearQuestion(); await addMC((continuousMC[continuousMC.length - 1][0] + continuousMC[continuousMC.length - 1][1] - 1) - lastPos, getRadioValue("MC")); await setScoreAndNum(numOfChoice, score, continuousMC); })() .then(() => { MCNumInput.value = ""; covering.style.display = "none"; confirmButton.disabled = false; backButton.disabled = false; for(let i = 0; i < MCInputList.length; i++) MCInputList[i].disabled = false; MCInputList[0].focus(); }) } } } } } } } else if(pos == 1) { let stop = false; let numOfQuestion = 0; let choiceNumList = []; if(getRadioValue("MC") == 0) { for(let i = 0; i < MCInputList.length; i++) { MCInputList[i].value = MCInputList[i].value.replace(/\s*/g,""); if(MCInputList[i].value.length != continuousMC[i][1] || !(/^[a-zA-Z]+$/.test(MCInputList[i].value))) { stop = true; window.alert("Wrong Input!"); break; } if(!stop) { MCInputList[i].value = MCInputList[i].value.toUpperCase(); let tempNumList = []; for(let j = 0; j < MCInputList[i].value.length; j++) { let flag = false; for(let k = 0; k < numOfChoice; k++) { if(MCInputList[i].value[j] == startChoice[(numOfQuestion % (startChoice.length / numOfChoice)) * numOfChoice + k]) { flag = true; tempNumList.push(k); } } if(!flag) { stop = true; window.alert("Wrong Input!"); break; } numOfQuestion++; } choiceNumList.push(tempNumList); } MCInputList[i].value = MCInputList[i].value.replace(/(.{5})/g, '$1 '); MCInputList[i].value = MCInputList[i].value.trim(); } } if(getRadioValue("MC") == 1) { for(let i = 0; i < MCInputList.length; i++) { MCInputList[i].value = MCInputList[i].value.replace(/\s*/g,""); MCInputList[i].value = MCInputList[i].value.toUpperCase(); let multiList = MCInputList[i].value.match(/\[[A-Z]{2,}\]/g); let allList = MCInputList[i].value.match(/\[.+?\]/g); if(multiList == null || multiList.length != continuousMC[i][1] || multiList.length != allList.length) { stop = true; window.alert("Wrong Input!"); break; } if(!stop) { let tempNumList = []; for(let j = 0; j < multiList.length; j++) { let subTempNumList = []; for(let n = 1; n < multiList[j].length - 1; n++) { let flag = false; for(let k = 0; k < numOfChoice; k++) { if(multiList[j][n] == startChoice[(numOfQuestion % (startChoice.length / numOfChoice)) * numOfChoice + k]) { flag = true; subTempNumList.push(k); } } if(!flag) { stop = true; window.alert("Wrong Input!"); break; } } tempNumList.push(subTempNumList); numOfQuestion++; } choiceNumList.push(tempNumList); } } } if(!stop) { setAnswer(continuousMC, choiceNumList, getRadioValue("MC")); } } } async function activateQuestion() { let question = document.getElementsByClassName("div_question"); for(let i = 0; i < question.length; i++) { if(question[i].getAttribute("title") === null || question[i].getAttribute("title") == "") { question[i].children[0].dispatchEvent(new MouseEvent("mouseover",{"bubbles": true})); await sleep(1); question[i].children[0].click(); await sleep(1); question[i].children[0].click(); await sleep(1); } } } async function clearQuestion() { let question = document.getElementsByClassName("div_question"); let i = question.length - 1; let lastPos = 0; await activateQuestion() .then(() => { while(i >= 0) { if((question[i].getAttribute("title") === null || question[i].getAttribute("title") == "") && question[i].children[0].children[0].children[0].textContent != "") { if(parseFloat(question[i].children[0].children[0].children[0].textContent) < continuousMC[0][0]) { lastPos = parseFloat(question[i].children[0].children[0].children[0].textContent); break; } question[i].children[0].children[2].children[3].children[0].children[2].dispatchEvent(new MouseEvent("mouseover",{"bubbles": true})); question[i].children[0].children[2].children[3].children[0].children[2].children[0].dispatchEvent(new MouseEvent("click",{"bubbles": true})); } else break; i--; } }); return lastPos; } async function addMC(n, opt) { let questionTypeList = testQuestionBar.children[1].children[1]; let MC1 = questionTypeList.children[0].children[0]; let MC2 = questionTypeList.children[2].children[0]; if(opt == 0) { for(let i = 0; i < n; i++) { MC1.click(); } } if(opt == 1) { for(let i = 0; i < n; i++) { MC2.click(); } } } async function setScoreAndNum(num, score, MCList) { let question = document.getElementsByClassName("div_question"); await activateQuestion() .then(() => { let k = 0; let numOfQuestion = 0; for(let i = 0; i < MCList.length; i++) { for(let j = MCList[i][0]; j < MCList[i][0] + MCList[i][1]; j++) { while(true) { if((question[k].getAttribute("title") === null || question[k].getAttribute("title") == "") && question[k].children[0].children[0].children[0].textContent != "") { if(question[k].children[0].children[0].children[0].textContent == j) { question[k].children[0].click(); let scoreSelect = question[k].children[1].children[0].children[1].children[2].children[3].children[0].children[0].children[1]; let choice = question[k].children[1].children[0].children[3].children[1].children[0].children[1].children[0].children[0]; let addChoice = choice.children[1].children[0].children[1]; scoreSelect.value = score; scoreSelect.dispatchEvent(new Event("change", {"bubbles": true})); for(let i = 0; i < num - 2; i++) { addChoice.click(); } for(let i = 0; i < num; i++) { let choiceInput = choice.children[i + 1].children[0].children[0]; choiceInput.value = startChoice[(numOfQuestion % (startChoice.length / num)) * num + i]; } question[k].children[0].click(); numOfQuestion++; break; } } k++; } } } if(numOfQuestion == 1) question[k].children[0].click(); }); } function setAnswer(MCList, choiceNumList, opt) { let question = document.getElementsByClassName("div_question"); let k = 0; if(opt == 0) { let pos = 0; for(let i = 0; i < MCList.length; i++) { let numOfQuestion = 0; for(let j = MCList[i][0]; j < MCList[i][0] + MCList[i][1]; j++) { while(true) { if((question[k].getAttribute("title") === null || question[k].getAttribute("title") == "") && question[k].children[0].children[0].children[0].textContent != "") { if(question[k].children[0].children[0].children[0].textContent == j) { question[k].children[0].click(); let selected = question[k].children[0].children[1].children[1].children[1].getElementsByTagName("li"); for(let n = 0; n < selected.length; n++) if(selected[n].children[0].children[0] !== null && typeof selected[n].children[0].children[0] !== "undefined") pos = n; let choice = question[k].children[1].children[0].children[3].children[1].children[0].children[1].children[0].children[0]; if(choiceNumList[i][numOfQuestion] != pos) choice.children[choiceNumList[i][numOfQuestion] + 1].children[5].children[0].children[1].click(); question[k].children[0].click(); numOfQuestion++; break; } } k++; } } } } if(opt == 1) { for(let i = 0; i < MCList.length; i++) { let numOfQuestion = 0; for(let j = MCList[i][0]; j < MCList[i][0] + MCList[i][1]; j++) { while(true) { if((question[k].getAttribute("title") === null || question[k].getAttribute("title") == "") && question[k].children[0].children[0].children[0].textContent != "") { if(question[k].children[0].children[0].children[0].textContent == j) { question[k].children[0].click(); let selected = question[k].children[0].children[1].children[1].children[1].getElementsByTagName("li"); let choice = question[k].children[1].children[0].children[3].children[1].children[0].children[1].children[0].children[0]; for(let n = 0; n < selected.length; n++) { if(selected[n].children[0].children[0] !== null && typeof selected[n].children[0].children[0] !== "undefined") { choice.children[n + 1].children[5].children[0].children[1].click(); } } for(let x = 0; x < choiceNumList[i][numOfQuestion].length; x++) { choice.children[choiceNumList[i][numOfQuestion][x] + 1].children[5].children[0].children[1].click(); } question[k].children[0].click(); numOfQuestion++; break; } } k++; } } } } } function clickBackButton() { pos--; MCNumInput.style.display = "block"; backButton.style.display = "none"; singleMultiContainer.style.display = "block"; for(let i = 0; i < MCInputList.length; i++) { const a = MCInputList[i].parentNode; const b = a.previousElementSibling; const c = a.parentNode; c.removeChild(a); c.removeChild(b); } MCInputList = []; } })();