I was using HTMLRewriter to select each element. This works but the issue is it gets stuck in an infinite loop. Now there is also a twist. If log the index directly:
$(".item").each((index, element) => {
console.log("index", index);
});
It logs:
index 0
index 1
But if I use this and try to log the element content
$(".item").each((index, element) => {
console.log("index", index);
console.log($(element).text().trim());
});
It gets stuck in an infinite loop and logs the element value multiple time and also the index gets increased. I am pretty sure it's an issue with .each() method. Here's the code of the .each() function:
each(callback: (index: number, element: string) => void) {
let index = 0;
this.beerio.rewriter
.on(this.selector, {
element: (element) => {
callback(index++, element.tagName);
},
})
.transform(this.beerio.raw_html);
return this;
}