Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't obj.length === 0 be sufficient?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. obj.length === 0 would only work for the array since arrays have a length property, but not objects. (This can also be verified by trying object.hasOwnProperty('length');

Since [Object.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) returns an array of key value pairs, we can use that to see if an object has any content. (you could do this with .keys() too. I tend to use .entries() for semantics).

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)
Expand Down
2 changes: 2 additions & 0 deletions src/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ function ready() {
// First, check if it's plain text and exit if not
const plainText = getTextFromTextOnlyDocument();
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();
return;
}
Expand Down