JeuPistesSarreguemines/require/js/qrscan.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-06-28 17:41:57 +02:00
import QrScanner from "./qr-scanner/qr-scanner.min.js";
import {puzzleSolve} from "./answer.js";
2023-06-28 17:41:57 +02:00
const video = document.querySelector("video");
const camList = document.querySelector("select");
// const fileSelector = document.querySelector("input[type='file']");
// Gestion de la langue :
var url_string = window.location.href;
var url = new URL(url_string);
var lg = url.searchParams.get("lg");
var team_id = url.searchParams.get("team");
var art_id = url.searchParams.get("id");
if (lg == null) {
lg = "en"; // Langue par défaut
// Recherche dans les cookies :
var cookies = decodeURIComponent(document.cookie);
if (cookies != "") {
// On part du principe qu'on n'a qu'un cookie, et que c'est celui de la langue
// 9 caractères pour "language", et 2 pour la langue
lg = cookies.substring(9, 11);
}
}
var cam_error = "cam_error";
if (lg == "en") {
var cam_error = "No camera detected !";
}
if (lg == "fr") {
var cam_error = "Aucune caméra détectée !";
}
2023-06-28 17:41:57 +02:00
function scanAction(result) {
puzzleSolve(result.data, team_id, art_id);
scanner.stop();
2023-06-28 17:41:57 +02:00
}
function checkCam(hasCamera) {
const errorP = document.querySelector("article p:nth-of-type(2)");
2023-06-28 17:41:57 +02:00
if (!hasCamera) {
errorP.innerHTML = "⚠️ " + cam_error;
2023-06-28 17:41:57 +02:00
}
else {
errorP.text = "";
}
}
const scanner = new QrScanner(
video,
result => scanAction(result),
{
highlightScanRegion: true,
highlightCodeOutline: true,
}
);
// Changement de caméra :
camList.addEventListener("change", event => {
scanner.setCamera(event.target.value);
});
// Démarrage du scan :
document.querySelector("button").addEventListener("click", () => {
scanner.start().then(() => {
QrScanner.listCameras(true).then(cameras => cameras.forEach(camera =>
{
const option = document.createElement("option");
option.value = camera.id;
option.text = camera.label;
camList.add(option);
}
));
});
QrScanner.hasCamera().then(hasCamera => checkCam(hasCamera));
});
// // Arrêt du scan :
// document.querySelector("nav button:nth-of-type(2)").addEventListener("click", () => {
// scanner.stop();
// });
// // Scan de fichier :
// fileSelector.addEventListener("change", () => {
// const file = fileSelector.files[0];
// if (file) {
// QrScanner.scanImage(file, {returnDetailedScanResult: true})
// .then(result => scanAction(result));
// }
// });