And here it is.
I changed it compared to the previous version, it now looks through each line. This means you shouldn't have more than one heading on a single line, although you could easily fix that, too. I just didn't feel like it.
This script should also be more readable, though it could still take a lot of improvement. But I ran out of time.
The ids start with the h1 tag, though. Made more sense to me.