From 28d217748eaf1ceb41b5449a3b9292778b666cff Mon Sep 17 00:00:00 2001 From: roth-michael Date: Tue, 4 Dec 2018 13:56:03 -0500 Subject: [PATCH] fixed bug where non-JSON text/html pages were polluted --- extension/src/json-viewer/check-if-json.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/extension/src/json-viewer/check-if-json.js b/extension/src/json-viewer/check-if-json.js index 3b554e67..5160fde7 100644 --- a/extension/src/json-viewer/check-if-json.js +++ b/extension/src/json-viewer/check-if-json.js @@ -1,4 +1,5 @@ var extractJSON = require('./extract-json'); +var bodyModified = false; function allTextNodes(nodes) { return !Object.keys(nodes).some(function(key) { @@ -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) { @@ -69,6 +79,8 @@ function checkIfJson(sucessCallback, element) { (isJSON(pre.textContent) || isJSONP(pre.textContent))) { sucessCallback(pre); + } else if (bodyModified) { + restoreNonJSONBody(); } }