How to pass e-mail address as URL parameter from an HubSpot Form

If you’re embedding the form on your page I would do it like this.

<!-- this is the standard form embed code --> 
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
 hbspt.forms.create({
 portalId: "[PORTAL ID]",
 formId: "[FORM ID]"
 });
 // add custom script after it
 let array = [];
 window.addEventListener('message', event => {
	if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
	 const fields = ['firstname', 'lastname', 'email', 'phone', 'hs_language'];
	 event.data.data.forEach(x => {
	 let found = fields.find(y => y === x.name);
	 if (found) {
	 let object = {
	 name: found,
	 value: x.value
	 };
	 array.push(object);
	 }
	 });
	}
	if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
	 let firstname = array.find(x => x.name === 'firstname').value;
	 let lastname = array.find(x => x.name === 'lastname').value;
	 let email = array.find(x => x.name === 'email').value;
	 let phone = array.find(x => x.name === 'phone').value;
	 let lang = array.find(x => x.name === 'hs_language').value;
	 window.location = "[URL TO REDIRECT TO HERE]?firstName=" + encodeURIComponent( firstname ) +"&lastName=" + encodeURIComponent( lastname ) +"&email=" + encodeURIComponent( email ) + "&phone=" + encodeURIComponent( phone ) + "&language=" + encodeURIComponent( lang );
	}
 });
</script>

Edit on 28/10/22 : changed the above code after it stopped working. Not clear why it stopped.