Skip to content

Instantly share code, notes, and snippets.

@andymagill
Created February 20, 2025 17:53
Show Gist options
  • Save andymagill/f254d840c80633528d83f04b20d47517 to your computer and use it in GitHub Desktop.
Save andymagill/f254d840c80633528d83f04b20d47517 to your computer and use it in GitHub Desktop.
Extract Product data from Jotform
function extractProductData() {
const products = [];
const productItems = document.querySelectorAll('.form-product-item');
productItems.forEach(item => {
const productId = item.getAttribute('pid');
const name = item.querySelector('.form-product-name')?.textContent?.trim() || ''; // Adjust selector. Use ? to avoid errors if element is missing.
const description = item.querySelector('.form-product-description')?.textContent?.trim() || ''; // Adjust selector.
const price = item.querySelector('.form-product-detail span span')?.textContent?.trim() || ''; // Adjust selector.
const image = item.querySelector('.image_area img')?.src || ''; // Adjust selector. Assumes image is in an <img> tag.
products.push({
productId,
name,
description,
price,
image
});
});
return products;
}
function convertToCSV(data) {
const csvRows = [];
const headers = Object.keys(data[0]);
csvRows.push(headers.join(','));
for (const row of data) {
const values = headers.map(header => {
const escaped = ('' + row[header]).replace(/"/g, '""');
return `"${escaped}"`;
});
csvRows.push(values.join(','));
}
return csvRows.join('\n');
}
function downloadCSV(csv, filename) {
const blob = new Blob([csv], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('hidden', '');
a.setAttribute('href', url);
a.setAttribute('download', filename);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
const products = extractProductData();
const csv = convertToCSV(products);
downloadCSV(csv, 'products.csv');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment