115 lines
4.3 KiB
JavaScript
115 lines
4.3 KiB
JavaScript
/*
|
|
* Stairway.js: a simple JavaScript privacy quiz
|
|
* Copyright © 2021 luca0N!
|
|
*
|
|
* This file is part of Stairway.js.
|
|
*
|
|
* Stairway.js is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Stairway.js is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Stairway.js. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
* E-mail contact: <luca0n@luca0n.com>.
|
|
*/
|
|
|
|
/*
|
|
* 2021-05-27
|
|
* by luca0N! <https://www.luca0n.com>
|
|
*/
|
|
|
|
// Attach setUpStrings() to onReady() in order to execute the main code as soon as possible. This overwrites onReady().
|
|
onReady = setUpStrings;
|
|
|
|
/**
|
|
* Replaces dummy strings with localized messages.
|
|
* @since 2021-05-27
|
|
*/
|
|
function setUpStrings(){
|
|
// Hide JavaScript warning message.
|
|
document.getElementById('javascript-warning').style['display'] = 'none';
|
|
let warning = document.getElementById('warning');
|
|
warning.style['display'] = 'unset';
|
|
// Get JSON file which contains all of the challenges required.
|
|
sendGetRequest('privacy-quiz.en.json', function(text){
|
|
let quiz = JSON.parse(text);
|
|
|
|
// Set up UI elements based on quiz metadata.
|
|
let quizTitle = document.getElementById('quiz-title');
|
|
quizTitle.innerText = quiz.title;
|
|
quizTitle.style['display'] = 'unset';
|
|
let quizAuthor = document.getElementById('quiz-author');
|
|
quizAuthor.innerText = quiz.publisher;
|
|
quizAuthor.style['display'] = 'unset';
|
|
|
|
let contents = quiz.contents;
|
|
let elQuiz = document.getElementById('quiz'); // quiz div element.
|
|
|
|
for (let x = 0; x < contents.length; x++){
|
|
//console.log(contents[x].title);
|
|
let itemDiv = document.createElement('div');
|
|
itemDiv.className = 'section-question';
|
|
let itemTitle = document.createElement('p');
|
|
itemTitle.innerText = contents[x].title;
|
|
itemTitle.className = 'question';
|
|
|
|
itemDiv.appendChild(itemTitle);
|
|
|
|
if (contents[x].type === 'multiple' || contents[x].type === 'single'){
|
|
// Generate a checkbox for each choice.
|
|
for (let y = 0; y < contents[x].choices.length; y++){
|
|
if (contents[x].choices.length != contents[x].values.length){
|
|
console.error('Unable to add choice #' + (y + 1) + ' for question #' + (x + 1) + ': ' +
|
|
contents[x].choices.length > contents[x].values.length ? 'missing values' : 'extra values found');
|
|
continue;
|
|
}
|
|
|
|
let questionInput = document.createElement('input');
|
|
questionInput.type = contents[x].type === 'multiple' ? 'checkbox' : 'radio';
|
|
if (contents[x].type === 'single')
|
|
questionInput.name = 'sec.' + x + '.chg'; // choice group
|
|
let currentId = 'sec.' + x + '.ch.' + y; // section, choice
|
|
questionInput.id = currentId;
|
|
let questionLabel = document.createElement('label');
|
|
questionLabel.htmlFor = currentId;
|
|
questionLabel.innerText = contents[x].choices[y];
|
|
|
|
itemDiv.append(questionInput);
|
|
itemDiv.append(questionLabel);
|
|
if (y < contents[x].choices.length)
|
|
itemDiv.append(document.createElement('br')); // Add a line break if this is not the last question.
|
|
}
|
|
if (contents[x].type === 'single'){
|
|
// This is a 'single' choice type question.
|
|
// Add a button that clears the current question selection.
|
|
let questionClear = document.createElement('button');
|
|
questionClear.innerText = 'Clear';
|
|
questionClear.onclick = function() {
|
|
for (let y = 0; y < contents[x].choices.length; y++) // Iterate through all input tags.
|
|
document.getElementById('sec.' + x + '.ch.' + y).checked = false; // Uncheck it.
|
|
}
|
|
itemDiv.append(questionClear); // Append the button to the item dit.
|
|
}
|
|
|
|
} else console.warn('Warning: unknown type "' + contents[x].type + '" for question #' + (x + 1));
|
|
|
|
elQuiz.appendChild(itemDiv);
|
|
}
|
|
// Add the finish button.
|
|
let finish = document.createElement('button');
|
|
finish.innerText = 'Finish!';
|
|
finish.onclick = function(){
|
|
document.location = 'final.html';
|
|
}
|
|
elQuiz.appendChild(finish);
|
|
warning.style['display'] = 'none';
|
|
});
|
|
}
|