// Add this code to your Squarespace site's Code Injection section
document.addEventListener('DOMContentLoaded', function() {
// Get the form and specific elements by their IDs
const contextTextarea = document.getElementById('textarea-yui_3_17_2_1_1553888888520_3747-field');
const fileInput = document.querySelector('.L2HjhvlRhFfYLLRHVKoH');
const form = contextTextarea?.closest('form');
if (form && contextTextarea && fileInput) {
form.addEventListener('submit', async function(e) {
e.preventDefault();
// Get the context from the textarea
const context = contextTextarea.value;
// Get the first selected PDF file (if any)
const files = fileInput.files;
let pdfContent = '';
if (files.length > 0) {
// Convert the first file to base64
try {
pdfContent = await convertFileToBase64(files[0]);
} catch (error) {
console.error('Error converting file:', error);
alert('Error processing the uploaded file. Please try again.');
return;
}
} else {
alert('Please select a file to upload.');
return;
}
// Prepare the request body
const requestBody = {
body: {
context: context,
pdfContent: pdfContent
}
};
try {
// Show loading state
const submitButton = form.querySelector('input[type="submit"]');
if (submitButton) {
submitButton.disabled = true;
submitButton.value = 'Submitting...';
}
// Send the request to your API
const response = await fetch('https://7mulbiba1l.execute-api.eu-west-2.amazonaws.com/dev/process-claim', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'PostmanRuntime/7.42.0',
'Accept': '*/*',
'x-api-key': 'YEhgJTblyb7sDXCi8zdSc7aqVplxZzrt7tBTOrSm'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
console.log('Success:', result);
// Show success message and reset form
alert('Form submitted successfully!');
form.reset();
} catch (error) {
console.error('Error:', error);
alert('There was an error submitting the form. Please try again.');
} finally {
// Reset submit button state
const submitButton = form.querySelector('input[type="submit"]');
if (submitButton) {
submitButton.disabled = false;
submitButton.value = 'Submit'; // or whatever the original text was
}
}
});
} else {
console.error('Could not find form elements. Please check the IDs.');
}
});
// Helper function to convert file to base64
function convertFileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
// Get the base64 string without the data URL prefix
const base64String = reader.result
.replace('data:', '')
.replace(/^.+,/, '');
resolve(base64String);
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
}