From c23e9ccd7494bb4c9e2673484765913f5430a4a8 Mon Sep 17 00:00:00 2001 From: Tee Date: Sat, 28 Sep 2019 05:58:19 -0400 Subject: [PATCH 1/6] Add check and alert for larger JSON files --- src/js/content.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/js/content.js b/src/js/content.js index b9b7a1d..5bcf76f 100644 --- a/src/js/content.js +++ b/src/js/content.js @@ -115,7 +115,13 @@ function insertFormatOptionBar() { function ready() { // First, check if it's plain text and exit if not const plainText = getTextFromTextOnlyDocument(); - if (!plainText || plainText.length > 3000000) { + if (!plainText) { + port.disconnect(); + return; + } + // Second, check if length is larger than 3MB + if (plainText.length > 3000000) { + alert('JSON Formatter Error: Cannot parse JSON larger than 3MB'); port.disconnect(); return; } From eee7f4da576fedd52b1ba480e916e48709ad8b5f Mon Sep 17 00:00:00 2001 From: Tee Date: Sat, 28 Sep 2019 09:03:54 -0400 Subject: [PATCH 2/6] Refactored text length check --- src/js/content.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/js/content.js b/src/js/content.js index 5bcf76f..d5acf4b 100644 --- a/src/js/content.js +++ b/src/js/content.js @@ -115,13 +115,9 @@ function insertFormatOptionBar() { function ready() { // First, check if it's plain text and exit if not const plainText = getTextFromTextOnlyDocument(); - if (!plainText) { - port.disconnect(); - return; - } - // Second, check if length is larger than 3MB - if (plainText.length > 3000000) { - alert('JSON Formatter Error: Cannot parse JSON larger than 3MB'); + if (!plainText || plainText.length > 300000) { + // If there is plain text and it's over 3MB, send alert + plainText && alert('JSON Formatter Error: Cannot parse JSON larger than 3MB'); port.disconnect(); return; } From a7587c1e27fae1ea403708b9f27ccc50e7c38da6 Mon Sep 17 00:00:00 2001 From: Tee Date: Sat, 28 Sep 2019 09:05:27 -0400 Subject: [PATCH 3/6] Correct max length --- src/js/content.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/content.js b/src/js/content.js index d5acf4b..0bc4f21 100644 --- a/src/js/content.js +++ b/src/js/content.js @@ -115,7 +115,7 @@ function insertFormatOptionBar() { function ready() { // First, check if it's plain text and exit if not const plainText = getTextFromTextOnlyDocument(); - if (!plainText || plainText.length > 300000) { + if (!plainText || plainText.length > 3000000) { // If there is plain text and it's over 3MB, send alert plainText && alert('JSON Formatter Error: Cannot parse JSON larger than 3MB'); port.disconnect(); From 2714179abc2198ac90141704c9361cbc5e51889b Mon Sep 17 00:00:00 2001 From: Tee Date: Sat, 28 Sep 2019 21:25:09 -0400 Subject: [PATCH 4/6] Add check for empty objects - Display empty objects and arrays without formatting --- src/js/background.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/js/background.js b/src/js/background.js index 760132f..4d9aafc 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -87,6 +87,12 @@ listen((port, msg) => { port.postMessage(['NOT JSON', 'technically JSON but not an object or array']); port.disconnect(); return; + } + // If it's an empty object or array, display without the formatting + else if (Object.entries(obj).length === 0 || obj.length === 0) { + port.postMessage(['NOT JSON', 'empty objects']); + port.disconnect(); + return; } // And send it the message to confirm that we're now formatting (so it can show a spinner) From 037fec3b2b146809d7f28ebd2564a0642df01f0d Mon Sep 17 00:00:00 2001 From: Tee Date: Sat, 28 Sep 2019 21:29:19 -0400 Subject: [PATCH 5/6] Revert "Add check for empty objects" This reverts commit 2714179abc2198ac90141704c9361cbc5e51889b. --- src/js/background.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 4d9aafc..760132f 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -87,12 +87,6 @@ listen((port, msg) => { port.postMessage(['NOT JSON', 'technically JSON but not an object or array']); port.disconnect(); return; - } - // If it's an empty object or array, display without the formatting - else if (Object.entries(obj).length === 0 || obj.length === 0) { - port.postMessage(['NOT JSON', 'empty objects']); - port.disconnect(); - return; } // And send it the message to confirm that we're now formatting (so it can show a spinner) From 7700bce430b56b3415eae7251c7006c54fa102ea Mon Sep 17 00:00:00 2001 From: Tee Date: Sat, 28 Sep 2019 21:33:36 -0400 Subject: [PATCH 6/6] Add check for empty objects and arrays - Display empty objects and arrays without formatting --- src/js/background.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/js/background.js b/src/js/background.js index 760132f..1b54ec9 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -87,6 +87,12 @@ listen((port, msg) => { port.postMessage(['NOT JSON', 'technically JSON but not an object or array']); port.disconnect(); return; + } + // If there's an empty object or array, return JSON as is + else if (Object.entries(obj).length === 0 || obj.length === 0) { + port.postMessage(['NOT JSON', 'empty object or array']); + port.disconnect(); + return; } // And send it the message to confirm that we're now formatting (so it can show a spinner)