Last active
October 19, 2022 14:54
-
-
Save kgslotwinski/8c637a4c488cef71133971dd32b64683 to your computer and use it in GitHub Desktop.
CSV Importer - Tampermonkey script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name CSVImporter | |
// @version 1.0 | |
// @description Tampermonkey script for importing CSV to web forms. Set desired @match and configure events functions. | |
// @author Konrad "Tree" Słotwiński | |
// @match https://www.google.com/* | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// ==/UserScript== | |
const CSVI_STORE = "csvi_store"; | |
const CSVI_STATUS = "csvi_status"; | |
const CSVI_EVENT_IMPORT = "csvi_import"; | |
const CSVI_EVENT_RECORD = "csvi_record"; | |
const CSVI_EVENT_STATUS = "csvi_status"; | |
document.addEventListener(CSVI_EVENT_IMPORT, function (e) { | |
//Custom event after CSV import | |
alert("Import"); | |
window.location.replace("https://www.google.com/"); | |
}); | |
document.addEventListener(CSVI_EVENT_RECORD, function (e) { | |
// Custom event on record import | |
alert(`CSVI Importing: (${e.detail.store}) ${e.detail.record}`); | |
GM_setValue(CSVI_STATUS, 1); | |
window.location.replace("https://www.google.com/"); | |
}); | |
document.addEventListener(CSVI_EVENT_STATUS, function (e) { | |
// Custom event on set status | |
alert(`CSVI Status: ${e.detail.status}`); | |
GM_setValue(CSVI_STATUS, 0); | |
window.location.replace("https://www.google.com/"); | |
}); | |
(function () { | |
'use strict'; | |
const data = GM_getValue(CSVI_STORE); | |
const status = GM_getValue(CSVI_STATUS); | |
if (status) { | |
document.dispatchEvent(new CustomEvent(CSVI_EVENT_STATUS, { | |
detail: {status: status} | |
})); | |
} else if (Array.isArray(data) && data.length > 0) { | |
document.dispatchEvent(new CustomEvent(CSVI_EVENT_RECORD, { | |
detail: {store: data.length, record: data.shift()} | |
})); | |
GM_setValue(CSVI_STORE, data); | |
} else { | |
let input = document.createElement("input"); | |
input.type = "file"; | |
Object.assign(input.style, { | |
display: "block", | |
position: "relative", | |
"z-index": 10000 | |
}); | |
input.addEventListener("input", function (value) { | |
let reader = new FileReader(); | |
reader.readAsText(value.target.files[0]); | |
reader.onload = function () { | |
GM_setValue(CSVI_STORE, CSVToArray(reader.result)); | |
document.dispatchEvent(new Event(CSVI_EVENT_IMPORT)) | |
}; | |
}); | |
document.body.prepend(input); | |
} | |
})(); | |
function CSVToArray(data, delimiter = ";") { | |
let objPattern = new RegExp(`(\\${delimiter}|\\r?\\n|\\r|^)(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|([^\"\\${delimiter}\\r\\n]*))`, "gi"); | |
let array = [ | |
[] | |
]; | |
let arrMatches = null; | |
while (arrMatches = objPattern.exec(data)) { | |
let strMatchedDelimiter = arrMatches[1]; | |
let strMatchedValue; | |
if (strMatchedDelimiter.length && strMatchedDelimiter !== delimiter) { | |
array.push([]); | |
} | |
if (arrMatches[2]) { | |
strMatchedValue = arrMatches[2].replace(new RegExp("\"\"", "g"), "\""); | |
} else { | |
strMatchedValue = arrMatches[3]; | |
} | |
array[array.length - 1].push(strMatchedValue); | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment