Skip to content

Commit

Permalink
Merge pull request #218 from roth-michael/master
Browse files Browse the repository at this point in the history
fixed bug where non-JSON text/html pages were polluted
  • Loading branch information
tulios authored Dec 5, 2018
2 parents 69d3cdc + 28d2177 commit bad9a49
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions extension/src/json-viewer/check-if-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var extractJSON = require('./extract-json');
var bodyModified = false;

function allTextNodes(nodes) {
return !Object.keys(nodes).some(function(key) {
Expand Down Expand Up @@ -39,12 +40,21 @@ function getPreWithSource() {
pre.textContent = textContent;
document.body.removeChild(childNode);
document.body.appendChild(pre);
bodyModified = true;
return pre;
}

return null
}

function restoreNonJSONBody() {
var artificialPre = document.body.lastChild;
var removedChildNode = document.createElement("text");
removedChildNode.textContent = artificialPre.textContent;
document.body.insertBefore(removedChildNode, document.body.firstChild);
document.body.removeChild(artificialPre);
}

function isJSON(jsonStr) {
var str = jsonStr;
if (!str || str.length === 0) {
Expand All @@ -69,6 +79,8 @@ function checkIfJson(sucessCallback, element) {
(isJSON(pre.textContent) || isJSONP(pre.textContent))) {

sucessCallback(pre);
} else if (bodyModified) {
restoreNonJSONBody();
}
}

Expand Down

0 comments on commit bad9a49

Please sign in to comment.