Skip to content

Commit d8bd59e

Browse files
committed
Fix JSON parsing error issue when data loading from a slow responding server (fixes #82)
1 parent 69f4972 commit d8bd59e

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## next
2+
3+
* Fixed JSON parsing error issue when data loading from a slow responding server (#82)
4+
15
## 1.13.2 (06-09-2022)
26

37
* Fixed copy to clipboard in FireFox

src/content/init.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let loaded = document.readyState === 'complete';
66
let loadedTimer;
77
let pre = null;
88
let preCursor;
9+
let prevCursorValue = '';
910
let initialPreDisplay = null;
1011
let preloader = null;
1112
let pushChunk = () => {};
@@ -73,7 +74,14 @@ const flushData = (settings) => {
7374

7475
while (true) {
7576
const isFirstChunk = preCursor === undefined;
76-
const chunkNode = isFirstChunk ? pre.firstChild : preCursor.nextSibling;
77+
const chunkNode = isFirstChunk
78+
? pre.firstChild
79+
// In some cases a browser appends new content to an existing text node
80+
// instead of creating new one. In this case, we are using the same text node
81+
// as on previous iteration and slice appended content as a chunk content.
82+
: preCursor.nodeValue !== prevCursorValue
83+
? preCursor
84+
: preCursor.nextSibling;
7785

7886
if (!chunkNode) {
7987
if (isFirstChunk && (loaded || pre.nextSibling)) {
@@ -100,13 +108,20 @@ const flushData = (settings) => {
100108
}
101109
}
102110

103-
pushChunk(chunkNode.nodeValue);
111+
pushChunk(
112+
chunkNode === preCursor
113+
// slice a new content from a chunk node in case a content
114+
// was appended to an existing text node
115+
? chunkNode.nodeValue.slice(prevCursorValue.length)
116+
: chunkNode.nodeValue
117+
);
104118
} else {
105119
// bailout: not a text node -> a complex markup is not a JSON
106120
throw raiseBailout();
107121
}
108122

109123
preCursor = chunkNode;
124+
prevCursorValue = preCursor.nodeValue;
110125
}
111126
};
112127

0 commit comments

Comments
 (0)