let links = '<ul>'
let counter = 0
document.querySelectorAll('a[name]').forEach(el => links += `<li><a href="#${el.name}">${counter++}</a></li>`)
document.querySelectorAll('[id]').forEach(el => links += `<li><a href="#${el.id}">${counter++}</a></li>`)
document.body.insertAdjacentHTML('afterbegin', links + '</ul>')