AGrush
AGrush

Reputation: 1167

How to remove unwanted text from html

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>

Upvotes: 0

Views: 1451

Answers (3)

Salman Arshad
Salman Arshad

Reputation: 272106

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.

Upvotes: 2

mars073
mars073

Reputation: 164

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

Upvotes: 1

Mohamed El Mrabet
Mohamed El Mrabet

Reputation: 633

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);

Upvotes: 1

Related Questions