S
S
Stop_Loll2021-10-05 12:08:34
JavaScript
Stop_Loll, 2021-10-05 12:08:34

How to remove all META tags and other information from the page received by the XMLHttpRequest() method?

Hello, dear masters and craftswomen, I ran into a very interesting problem, when parsing a page through XMLHttpRequest(), all information about the page remains, this is logical, but the question is how to remove it?
Below is the code and screenshot of the problem:

document.addEventListener('DOMContentLoaded', function LoadComments() {
(function LoadComments() {
var CommentsSite = new XMLHttpRequest();
CommentsSite.open("GET","/com.html="+location.pathname+"", true);
CommentsSite.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
    	var g = new DOMParser().parseFromString(this.responseText, "text/html");
     var g = CommentsSite.responseText;
     var repcen = 'post"><span>';
     g = g.replace('post"><div>', repcen);
     repcen = '';
     g = g.replace('<div class="top">Комментарии</div>', repcen);
     g = g.replace('">далее</a>', repcen);
     g = g.replace('<br /><a href="/">Назад</a><br /><div class="btm"><a href="'+location.origin+'">На главную</a></div>', repcen);
     g = g.replace('<div class="ddd"><a href="/">Вернуться назад</a></div>', repcen);
   g = g.replace('<div class="user_ddd"><a href="/">Вернуться назад</a><a href="/user/lb.html">Личный кабинет</a></div>', repcen);
     repcen = '<body><div class="comments__block">';
     g = g.replace('<body><div>', repcen);          
     repcen = '<span class="common">';
     g = g.replace(/<div class="common">/g, repcen);
     repcen = 'span><br ';
     g = g.replace(/div><br /g, repcen);
     repcen = '<div class="log_reg_block" style="display:none;">';
     g = g.replace(/<div class="log_reg_block">/g, repcen);
     repcen = '<form action="/comments';
     g = g.replace('<form action="comments', repcen);
     repcen = '<a href="/comments';
     g = g.replace('<a href="comments', repcen);
     g = g.replace(/<a href="ank.html(.*?)">(.*?)</g, '<a href="/ank.html$1">$2<');


    document.getElementById("s_cos").innerHTML = g;
    }
  }
CommentsSite.send(null);
})()}, false);

This is the problem that pops up when the function is launched, below is a screenshot:
615c14f8ad980334495872.png
That is, the Meta tag is first displayed, the connection to the site styles, etc., but it is removed, leaving this part untouched.

Ps I tried to delete the Head, it didn't work, I tried to delete it through an empty "repcen", it also doesn't work, I tried to display only the Div with the name "comments__block", another problem. I’m not asking you to write a ready-made solution, but just to hint at least in which direction to dig and what to do, I’ve been suffering with this problem for the second day, but I can’t solve everything, yes, the function works, but it aesthetically spoils the view by cloning another head. Thank you all in advance, I'll be waiting for your answers!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VA_ic2b, 2021-10-06
@Stop_Loll

let html = document.createElement( 'html' );
html.innerHTML = CommentsSite;
Now you can manipulate DOM elements. For example html.getElementsByTagName('div')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question