Skip to content

Commit

Permalink
Fix JSON parsing error issue when data loading from a slow responding…
Browse files Browse the repository at this point in the history
… server (fixes #82)
  • Loading branch information
lahmatiy committed Sep 8, 2022
1 parent 69f4972 commit d8bd59e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## next

* Fixed JSON parsing error issue when data loading from a slow responding server (#82)

## 1.13.2 (06-09-2022)

* Fixed copy to clipboard in FireFox
Expand Down
19 changes: 17 additions & 2 deletions src/content/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let loaded = document.readyState === 'complete';
let loadedTimer;
let pre = null;
let preCursor;
let prevCursorValue = '';
let initialPreDisplay = null;
let preloader = null;
let pushChunk = () => {};
Expand Down Expand Up @@ -73,7 +74,14 @@ const flushData = (settings) => {

while (true) {
const isFirstChunk = preCursor === undefined;
const chunkNode = isFirstChunk ? pre.firstChild : preCursor.nextSibling;
const chunkNode = isFirstChunk
? pre.firstChild
// In some cases a browser appends new content to an existing text node
// instead of creating new one. In this case, we are using the same text node
// as on previous iteration and slice appended content as a chunk content.
: preCursor.nodeValue !== prevCursorValue
? preCursor
: preCursor.nextSibling;

if (!chunkNode) {
if (isFirstChunk && (loaded || pre.nextSibling)) {
Expand All @@ -100,13 +108,20 @@ const flushData = (settings) => {
}
}

pushChunk(chunkNode.nodeValue);
pushChunk(
chunkNode === preCursor
// slice a new content from a chunk node in case a content
// was appended to an existing text node
? chunkNode.nodeValue.slice(prevCursorValue.length)
: chunkNode.nodeValue
);
} else {
// bailout: not a text node -> a complex markup is not a JSON
throw raiseBailout();
}

preCursor = chunkNode;
prevCursorValue = preCursor.nodeValue;
}
};

Expand Down

0 comments on commit d8bd59e

Please sign in to comment.