1
1
Fork 0

Setup QS
All checks were successful
Linting / Javadoc (push) Successful in 9s

This commit is contained in:
Dominik Säume 2024-05-23 19:18:48 +02:00
parent 69fb65deb7
commit 488dd75e29
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
7 changed files with 156 additions and 113 deletions

18
.gitea/workflows/lint.yml Normal file
View file

@ -0,0 +1,18 @@
name: "Linting"
on:
push:
branches:
- main
jobs:
javadoc:
name: "Javadoc"
runs-on: "ubuntu-latest"
container:
image: "git.euph.dev/actions/runner-js-latest:latest"
steps:
- name: "Checkout"
uses: "https://git.euph.dev/actions/checkout@v3"
- name: "Prettier"
run: npx prettier src/ types/ --check --log-level=error

10
.prettierrc.json Normal file
View file

@ -0,0 +1,10 @@
{
"useTabs": false,
"tabWidth": 4,
"arrowParens": "avoid",
"bracketSameLine": false,
"singleQuote": true,
"semi": true,
"trailingComma": "none",
"endOfLine": "lf"
}

2
Makefile Normal file
View file

@ -0,0 +1,2 @@
lint:
@npx prettier src/ types/ --write

View file

@ -3,22 +3,22 @@
* @param {Array<Answer>} answerData
* @returns
*/
function answerQuestion(answerData){
const question = document.querySelector(".question:not(.hidden)");
function answerQuestion(answerData) {
const question = document.querySelector('.question:not(.hidden)');
if (!question) {
return;
}
const questionTextDom = question.querySelector(".questionText .mattext");
const questionTextDom = question.querySelector('.questionText .mattext');
if (!questionTextDom) return;
const questionText = questionTextDom.textContent.trim();
const answersDom = question.querySelector("ul.coreContent");
const answersDom = question.querySelector('ul.coreContent');
if (!answersDom) return;
const answers = answersDom.children;
for (let answer of Array.from(answers)) {
const input = answer.querySelector("input");
const input = answer.querySelector('input');
if (!input) continue;
input.checked = false;
}
@ -29,7 +29,7 @@ function answerQuestion(answerData){
}
for (const answer of correctAnswers) {
const input = answer.querySelector("input");
const input = answer.querySelector('input');
if (!input) continue;
input.checked = true;
}
@ -51,7 +51,12 @@ function findAnswers(answerData, questionText, answers) {
if (matchAnswer(questionText.trim(), entry.question.trim())) {
for (let availableAnswer of answers) {
for (let possibleAnswer of entry.answers) {
if (matchAnswer(availableAnswer.textContent.trim(), possibleAnswer)) {
if (
matchAnswer(
availableAnswer.textContent.trim(),
possibleAnswer
)
) {
correctAnswers.push(availableAnswer);
}
}
@ -64,9 +69,9 @@ function findAnswers(answerData, questionText, answers) {
function matchAnswer(textA, textB) {
const replaceRegex = /[^\w]/gi;
textA = textA.replace(replaceRegex, "");
textB = textB.replace(replaceRegex, "");
return (textA === textB);
textA = textA.replace(replaceRegex, '');
textB = textB.replace(replaceRegex, '');
return textA === textB;
}
window.answerQuestion = answerQuestion;

View file

@ -5,20 +5,20 @@ const QUESTION_REGEX = /^[0-9]+\. (.*)$/;
* @param {string} [answerURL=""] - The URL to fetch answers from.
* @returns {Promise<Array<Answer>>} A Promise that resolves with the fetched answers.
*/
function fetchAnswers(answerURL = "") {
function fetchAnswers(answerURL = '') {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
method: 'GET',
url: answerURL,
headers: {
"Content-Type": "text/html",
'Content-Type': 'text/html'
},
onload: function (response) {
parseAnswers(response, resolve);
},
onerror: function (error) {
reject(error);
},
}
});
});
}
@ -50,11 +50,14 @@ function parseAnswers(response, resolve) {
*/
function getAllAnswersElement(response) {
const parser = new DOMParser();
const virtualDOM = parser.parseFromString(response.responseText, "text/html");
const virtualDOM = parser.parseFromString(
response.responseText,
'text/html'
);
let answersElement = virtualDOM.querySelector(".pf-content");
let answersElement = virtualDOM.querySelector('.pf-content');
if (!answersElement) {
answersElement = virtualDOM.querySelector(".thecontent");
answersElement = virtualDOM.querySelector('.thecontent');
}
return answersElement;
}
@ -68,7 +71,7 @@ function getAllAnswersElement(response) {
function parseAnswerElement(index, element, allAnswersElement) {
// Check for Possible Tags
if (
!(element.tagName === "P" || element.tagName === "STRONG") ||
!(element.tagName === 'P' || element.tagName === 'STRONG') ||
!element.innerHTML
) {
return;
@ -76,7 +79,7 @@ function parseAnswerElement(index, element, allAnswersElement) {
// Get Question Element
/** @type {Element} */
let questionElement = element.querySelector("strong");
let questionElement = element.querySelector('strong');
if (questionElement === null) {
if (!element.textContent) {
return;
@ -92,11 +95,11 @@ function parseAnswerElement(index, element, allAnswersElement) {
// Get Awsners
const answersElement = getAnswersElement(index, allAnswersElement);
if (answersElement === null || answersElement.tagName !== "UL") return;
if (answersElement === null || answersElement.tagName !== 'UL') return;
return {
question: questionText,
answers: getAnswers(answersElement),
answers: getAnswers(answersElement)
};
}
@ -118,7 +121,7 @@ function parseQuestion(questionElement) {
function getAnswersElement(index, allAnswersElement) {
let answersElement = allAnswersElement.children[index + 1];
if (answersElement.tagName === "P") {
if (answersElement.tagName === 'P') {
answersElement = allAnswersElement.children[index + 2];
}
return answersElement;
@ -130,9 +133,11 @@ function getAnswersElement(index, allAnswersElement) {
*/
function getAnswers(answersElement) {
const answers = [];
for (let answerDom of Array.from(answersElement.querySelectorAll("strong"))) {
for (let answerDom of Array.from(
answersElement.querySelectorAll('strong')
)) {
let answerText = answerDom.textContent.trim();
if (answerText.endsWith("*")) {
if (answerText.endsWith('*')) {
answerText = answerText.substring(0, answerText.length - 1);
}
answers.push(answerText);

View file

@ -14,24 +14,27 @@
// @author Dominik Säume
// ==/UserScript==
const URL_STORAGE_KEY = "itexamanswers.net URL";
const URL_STORAGE_KEY = 'itexamanswers.net URL';
/** @type {Array<Answer>} */
let answerData;
window.addEventListener("keydown", async (event) => {
switch(event.key){
case "p":
window.addEventListener('keydown', async event => {
switch (event.key) {
case 'p':
const oldAnswersURL = GM_getValue(URL_STORAGE_KEY);
const newAnswersURL = prompt("Please input the answer url (itexamanswers.net)", oldAnswersURL);
const newAnswersURL = prompt(
'Please input the answer url (itexamanswers.net)',
oldAnswersURL
);
GM_setValue(URL_STORAGE_KEY, newAnswersURL);
answerData = await window.fetchAnswers(newAnswersURL);
break;
case "n":
document.getElementById("next").click();
case 'n':
document.getElementById('next').click();
break;
case "a":
case 'a':
window.answerQuestion(answerData);
break;
}

View file

@ -3,5 +3,5 @@
* @returns {string}
*/
function GM_getValue(storageKey) {
return "";
return '';
}