Domik942,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<script>
var testHtml = '<head><title>second page</title><script>alert("hello 111");</scr'+'ipt></head><body><p>test script</p><script>alert("hello 222");</sc'+'ript><div>the 7 tag</div><div>the 8 tag</div><div>the 9 tag</div></body>';
var parser = new DOMParser();
var doc = parser.parseFromString(testHtml, 'text/html');
var htmlOld = document.querySelector('html');
var htmlNew = doc.querySelector('html');
document.replaceChild(htmlNew, htmlOld);
document.querySelectorAll('script').forEach(el => {
var s = document.createElement('script');
s.text = el.text;
el.parentNode.replaceChild(s, el)
}
)
</script>
</body>
</html>