const fs = require('fs');
let js = fs.readFileSync('app_final.js', 'utf8');

// Replace x.forEach( with (x || []).forEach(
// Only matches words and dots, e.g., currentItems, j.rows, inv.items
// Does not match function calls like filter().forEach( or querySelectorAll().forEach(
js = js.replace(/([\w\.]+)\.forEach\(/g, '($1 || []).forEach(');

fs.writeFileSync('app_final.js', js, 'utf8');
console.log('Patched all forEach safely!');
