简体   繁体   中英

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>

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM