import sys path = "/home/slin/enduro-trails/prototype/static/app.js" with open(path, 'r') as f: content = f.read() # Replace the style.load approach with idle event which reliably fires old = """ map.setStyle(styleUrl); // Restore position after style loads map.once('style.load', () => { map.jumpTo({ center, zoom, bearing, pitch }); setTimeout(() => rebuildMapOverlays(), 100); });""" new = """ map.setStyle(styleUrl); // Restore position and overlays after style loads map.once('idle', () => { map.jumpTo({ center, zoom, bearing, pitch }); rebuildMapOverlays(); });""" if old not in content: print("ERROR: marker not found") sys.exit(1) content = content.replace(old, new) with open(path, 'w') as f: f.write(content) print("OK - switchMapStyle uses idle event")