The problem is that you are using a NodeList:
var pres = document.getElementsByTagName('pre');
for (var i = 0; i < pres.length; i++) {
NodeLists are "live" collections, that is they change. At the beginning you have one pre tag so pres.length
returns 1. But within the loop you add pre tags. So pres.length
changes as well. Hence your loop runs again and again.