Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
2.4 kB
3
Indexable
Never
document.addEventListener("DOMContentLoaded", function() {
    document.getElementById('csv-file').addEventListener('change', function(event) {
        const file = event.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                const contents = e.target.result;
                // Process the CSV file contents here
                parseCSV(contents);
            };
            reader.readAsText(file);
        }
    });

    function parseCSV(csv) {
        Papa.parse(csv, {
            header: true,
            complete: function(results) {
                const data = results.data;
                populateForm(data);
            }
        });
    }


    function updateForm(returnObj) {
        let pcTextBox = document.getElementById("PCTextBox");
        if (returnObj.ndcUpc) {
            pcTextBox.value = returnObj.pc;
            pcTextBox.dispatchEvent(new Event('change'));
        }
    
        let unitCaseDropdown = document.getElementById("OpenCase");
        if (returnObj.unitCase) {
            unitCaseDropdown.value = returnObj.unitCase
        }

        let quantityInput = document.getElementById("tbxQuantity");
        if (returnObj.quantity) {
            quantityInput.value = returnObj.quantity;
            quantityInput.dispatchEvent(new Event('change'));
        }

        let button = document.getElementById('btnSubmitEntries');
        button.click();
    }

    async function populateForm(data) {
        if (data.length > 0) {
            for (let i = 0; i < data.length; i++) {
                let rowData = data[i];

                let parsedData = Object.values(rowData);

                let returnObj = {
                    pc: parsedData[0],
                    unitCase: parsedData[1],
                    quantity: parsedData[2]
                };


                chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
                    chrome.scripting.executeScript(
                        {
                            target: { tabId: tabs[0].id },
                            func: updateForm,
                            args: [returnObj]
                        }
                    );
                });
            }
        }
    }
});
Leave a Comment