forked from antux18/ChasseTresorPange
94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
// Ce programme effectue la capture du QR code
|
|
|
|
import QrScanner from "./qr-scanner/qr-scanner.min.js";
|
|
import {puzzleSolve} from "./answer.js";
|
|
|
|
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 !";
|
|
}
|
|
|
|
function scanAction(result) {
|
|
puzzleSolve(result.data, team_id, art_id);
|
|
scanner.stop();
|
|
}
|
|
|
|
function checkCam(hasCamera) {
|
|
const errorP = document.querySelector("article p:nth-of-type(2)");
|
|
if (!hasCamera) {
|
|
errorP.innerHTML = "⚠️ " + cam_error;
|
|
}
|
|
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("article 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));
|
|
// }
|
|
// });
|