Skip to content

Instantly share code, notes, and snippets.

@itsJarrett
Created February 11, 2023 17:03
Show Gist options
  • Save itsJarrett/70d7fca15a781c76581313c289769aa7 to your computer and use it in GitHub Desktop.
Save itsJarrett/70d7fca15a781c76581313c289769aa7 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Upload CSV to Website
// @namespace jaboice.me
// @version 0.1
// @description Upload a CSV file to fill out a form and add items to cart
// @author JABOICE
// @match https://test.com/
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Create a button for uploading the CSV
const btn = document.createElement('input');
btn.type = 'file';
btn.accept = '.csv';
btn.style.display = 'block';
btn.addEventListener('change', handleFileUpload);
document.body.appendChild(btn);
function handleFileUpload(event) {
const reader = new FileReader();
reader.onload = processCSV;
reader.readAsText(event.target.files[0]);
}
function processCSV(event) {
const csv = event.target.result;
const rows = csv.split('\n');
// Loop through each row of the CSV
for (let i = 0; i < rows.length; i++) {
const columns = rows[i].split(',');
// Fill out the form on the website using the columns from the CSV
const form = document.querySelector('form');
form.querySelector('input[name="name"]').value = columns[0];
form.querySelector('input[name="quantity"]').value = columns[1];
form.querySelector('input[name="price"]').value = columns[2];
// Submit the "Add to Cart" button
form.querySelector('input[type="submit"]').click();
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment