(Caveat: I have neither run your code nor tested the below fully)
It seems likely that you are experiencing the same behavior reported in this question: How to get the text of multiple elements split by delimiter using jQuery?
If that assessment is accurate, code like the below, which is adapted from that question's accepted answer, may be of use
const text = $("body").children(":not(header, footer, script)").map(function(){return $(this).text()}).get().join(' ')
Basically, the idea is to intersperse a space between text from different elements; under your current approach, that content is being joined without a delimiter by the call to .text().