From fc5304667b20db5d455c452545f760f7ffade737 Mon Sep 17 00:00:00 2001 From: mrfry Date: Tue, 30 Mar 2021 10:41:13 +0200 Subject: [PATCH] Finding question / answer node containers more dinamically --- stable.user.js | 109 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 17 deletions(-) diff --git a/stable.user.js b/stable.user.js index 9d01736..84f7096 100755 --- a/stable.user.js +++ b/stable.user.js @@ -351,6 +351,7 @@ getQuizData() .then(readQuestions => { if (readQuestions.length === 0) { + warn('readQuestions length is zero, no questions found on page!') ShowMessage( texts.unableToParseTestQuestion, @@ -401,20 +402,68 @@ }) } + const questionNodeVariants = { + formFirst: { + criteria: () => { + const questionNodes = Array.from( + document.getElementsByTagName('form')[0].childNodes[0].childNodes + ) + // test: e2c01ff4-d97a-4ab9-8f7f-e28812541097 + const notOnlyTextNodes = questionNodes.every(node => { + return node.tagName !== undefined + }) + return notOnlyTextNodes && questionNodes.length > 0 + }, + getter: () => { + return Array.from( + document.getElementsByTagName('form')[0].childNodes[0].childNodes + ) + }, + }, + formSecond: { + criteria: () => { + const questionNodes = Array.from( + document.getElementsByTagName('form')[1].childNodes[0].childNodes + ) + return questionNodes.length > 0 + }, + getter: () => { + return Array.from( + document.getElementsByTagName('form')[1].childNodes[0].childNodes + ) + }, + }, + } + + function getQuestionNodes() { + try { + let questionNodes + Object.keys(questionNodeVariants).some(key => { + const variant = questionNodeVariants[key] + if (variant.criteria()) { + questionNodes = variant.getter() + if (questionNodes.length === 0) { + warn( + `question nodes ${key} criteria was true, but result is an empty array!` + ) + } else { + return true + } + } + }) + return questionNodes + } catch (e) { + warn('Error in getQuestionNodes') + warn(e) + } + } + function getQuizData() { return new Promise(resolve => { // TODO: dropdown in question // TODO: get possible answers too const promises = [] - let questionNodes = Array.from( - document.getElementsByTagName('form')[0].childNodes[0].childNodes - ) - - if (questionNodes.length === 0) { - questionNodes = Array.from( - document.getElementsByTagName('form')[1].childNodes[0].childNodes - ) - } + const questionNodes = getQuestionNodes() let i = 0 while ( @@ -667,17 +716,43 @@ // : Result page processing functions {{{ + const resultNodeVariants = questionNodeVariants + + function getResultNodes() { + try { + let resultNodes + Object.keys(resultNodeVariants).some(key => { + const variant = resultNodeVariants[key] + if (variant.criteria()) { + resultNodes = variant.getter() + if (resultNodes.length === 0) { + warn( + `result nodes ${key} criteria was true, but result is an empty array!` + ) + } else { + return true + } + } + }) + return resultNodes + } catch (e) { + warn('Error in getResultNodes') + warn(e) + } + } + function getQuiz() { return new Promise(resolve => { const promises = [] - let questionNodes = Array.from( - document.getElementsByTagName('form')[0].childNodes[0].childNodes - ) - if (questionNodes.length === 0) { - questionNodes = Array.from( - document.getElementsByTagName('form')[1].childNodes[0].childNodes - ) - } + const questionNodes = getResultNodes() + // let questionNodes = Array.from( + // document.getElementsByTagName('form')[0].childNodes[0].childNodes + // ) + // if (questionNodes.length === 0) { + // questionNodes = Array.from( + // document.getElementsByTagName('form')[1].childNodes[0].childNodes + // ) + // } let i = 0 while (i < questionNodes.length && questionNodes[i].tagName === 'DIV') {