The main problem is that I can't figure out what parts of the HTML are important
Before attempting to post, learn technique for figuring this out.
Here is a simple way to start: first, make sure the HTML is syntactically valid (all the opening tags match up in the appropriate way with a corresponding close tag, and are nested in a way that makes sense).
Then, try deleting a complete tag (from the opening to the matching close), and viewing the result again. Aim for something that will remove close to half of the document, if possible. Does the problem still occur? If yes, then we have already successfully improved the example. If no, then the removed part is apparently necessary to cause the problem. Put it back, and try removing something else. As a shortcut, try removing everything else at the same level, first (to see if the problem is specific to that first tag, or if there need to be multiple tags in the same context to cause the problem, or just what).
Keep going until you have removed as much as you can figure out how to remove, while still demonstrating the problem. Work from larger to smaller removals, if possible. After simplifying the tag structure, see if you can do things like shortening the text within tags, or simplifying tag attributes, while still showing the problem.
Also read https://stackoverflow.com/help/minimal-reproducible-example and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/.
As another hint: Do you expect the page to look a certain way, if JavaScript is disabled? Is the result wrong if you do that? Does that demonstrate the problem you want to ask about? If so, then we don't need the JavaScript to reproduce the problem, therefore, we don't need the JavaScript in the code example.
On the other hand: it seems like the point of the JavaScript is to make some changes to some tag attributes, and possibly add some content. Does it make the changes that you want it to? (Load the page and look at the resulting DOM in the browser's inspector; does it look like the JavaScript did what you wanted it to do?)
Based on that, I see two likely possibilities:
the question is about the result of running the Javascript - so instead of showing us the original, show us the DOM that you get (and, again, try to reduce that).
the question is about the JavaScript, because the DOM ends up wrong. So, the next step is to try to debug the JavaScript itself - carefully check, step by step, what happens when that code runs; and see where that differs from your expectation.