Skip to content

Commit 28d2177

Browse files
authored
fixed bug where non-JSON text/html pages were polluted
1 parent 69d3cdc commit 28d2177

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

extension/src/json-viewer/check-if-json.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var extractJSON = require('./extract-json');
2+
var bodyModified = false;
23

34
function allTextNodes(nodes) {
45
return !Object.keys(nodes).some(function(key) {
@@ -39,12 +40,21 @@ function getPreWithSource() {
3940
pre.textContent = textContent;
4041
document.body.removeChild(childNode);
4142
document.body.appendChild(pre);
43+
bodyModified = true;
4244
return pre;
4345
}
4446

4547
return null
4648
}
4749

50+
function restoreNonJSONBody() {
51+
var artificialPre = document.body.lastChild;
52+
var removedChildNode = document.createElement("text");
53+
removedChildNode.textContent = artificialPre.textContent;
54+
document.body.insertBefore(removedChildNode, document.body.firstChild);
55+
document.body.removeChild(artificialPre);
56+
}
57+
4858
function isJSON(jsonStr) {
4959
var str = jsonStr;
5060
if (!str || str.length === 0) {
@@ -69,6 +79,8 @@ function checkIfJson(sucessCallback, element) {
6979
(isJSON(pre.textContent) || isJSONP(pre.textContent))) {
7080

7181
sucessCallback(pre);
82+
} else if (bodyModified) {
83+
restoreNonJSONBody();
7284
}
7385
}
7486

0 commit comments

Comments
 (0)