0

I have some text that is appearing inside the body tag of my website because of some malformed integration. Just as a quick fix I would like to remove it from the front end. This is the text:

","author":"ccc","publisher":{"@type":"Organization","name":"ccc ccc ccc","logo":{"@type":"ImageObject","url":"https://ccc//cc/ccc//.png"}}}

I don't have access to the sourcecode, but would like to quickly get rid of this text using a script tag on the front end. Obviously the body tag contains a lot of other nodes so I'm not sure how to cut out just the text and keep the other divs inside the body tag in their place?

<body>
","author":"ccc","publisher":{"@type":"Organization","name":"ccc ccc ccc","logo":{"@type":"ImageObject","url":"https://ccc//cc/ccc//.png"}}}
<div class="dontremoveme">
<div>
<div class="dontremoveme">
<div>
<body>
Salman A
  • 262,204
  • 82
  • 430
  • 521
AGrush
  • 1,107
  • 13
  • 32

3 Answers3

2

Looking at the markup, I guess you can target document.body.firstChild element:

document.addEventListener("DOMContentLoaded", function() {
  var node = document.body.firstChild;
  node.parentNode.removeChild(node);
});
\r\n\r\n\r\n\r\n\r\n\r\n
<div class="dontremoveme">foo</div>
<div class="dontremoveme">bar</div>

It is possible to use a loop instead and remove all nodes from the beginning until you find a valid node, such as a wrapper div.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 1
    how i did it was i typed 'document.body.children' into chrome dev tools and found which nodes i wanted to remove.. then i removed them: var node2 = document.body.children[1]; var node3 = document.body.children[1].previousSibling; var node4 = document.body.children[1].nextSibling; node2.remove(); node3.remove(); node4.remove(); – AGrush Sep 03 '19 at 14:13
1

You can get all of your body like this :

In jQuery:

var inputString = $('body').html();
inputString = inputString.replace(/(\r\n|\r|\n){2,}/g, '$1\n');
$('body').html(inputString);

In Javascript

var inputString = document.getElementsByTagName('body')[0].innerHTML;
inputString = inputString.replace(/(\r\n|\r|\n){2,}/g, '$1\n');
document.getElementsByTagName('body')[0].innerHTML.html(inputString);
1

Maybe something like that:

// declaration :
clearMe = function(node) {
    if (node.nodeType == 3)
        node.textContent = node.textContent.trim();
    else
        for (let n in node.childNodes)
            clearMe(n);
}
// call:
clearMe(document.body);

My function is recursive and it does not force the reinterpretation of the html code, for example with the following code all events are lost

node.innerHTML = node.innerHTML; // <- every EventListener or node.onEvent are lost
mars073
  • 164
  • 6