From 28fffa5bc1dceabf05fbd5c55770029cfa77b641 Mon Sep 17 00:00:00 2001 From: cedricp-ign <117263014+cedricp-ign@users.noreply.github.com> Date: Mon, 24 Jun 2024 10:55:47 +0200 Subject: [PATCH] Update gpao.js to check if json file is ok to avoid crash --- resources/vendor/ign/gpao.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/resources/vendor/ign/gpao.js b/resources/vendor/ign/gpao.js index 3f7e17a..036ff65 100644 --- a/resources/vendor/ign/gpao.js +++ b/resources/vendor/ign/gpao.js @@ -16,13 +16,26 @@ function sendProject(json){ }); } +//Fonction qui définit si le json est correcte +function isJsonString(str) { + try { + JSON.parse(str); + } catch (e) { + alert('JSON invalide') + return false; + } + return true; +} + // Fonction qui importe un projet via un fichier json function jsonChanged(file) { var reader = new FileReader(); reader.addEventListener('load', function(e) { // contents of file in variable var json = e.target.result; - sendProject(json); + if(isJsonString(json)) { + sendProject(json); + } }); // read as text file reader.readAsText(file); @@ -52,7 +65,9 @@ function txtChanged(file, tags) { reader.addEventListener('load', function(e) { var text = e.target.result.split(/\r\n|\n/); var json = createJson(file.name, text, tags); - sendProject(json); + if(isJsonString(json)) { + sendProject(json); + } }); // read as text file reader.readAsText(file); @@ -304,4 +319,4 @@ function validateTags(domTags, btnSubmit) { btn.disabled = false; } } -} \ No newline at end of file +}