Skip to content

Instantly share code, notes, and snippets.

@valtoni
Created January 18, 2020 22:48
Show Gist options
  • Save valtoni/f9626f42e653a6ab5c4463baa9ea1435 to your computer and use it in GitHub Desktop.
Save valtoni/f9626f42e653a6ab5c4463baa9ea1435 to your computer and use it in GitHub Desktop.
spendee.com: Download csv with transactions loaded in screen (greasemonkey)
// ==UserScript==
// @name Spendee Button Donwload CSV
// @version 0.1
// @description Download CSV transactions
// @match https://app.spendee.com/wallet/*/transactions
// @copyright 2020+, Valtoni Boaventura
// ==/UserScript==
console.info("CSV Button foi ativado para a página");
function captureText(item, selector) {
var domElement = item.querySelector(selector);
if (domElement === undefined || domElement === null) return '***' + selector + '***';
return item.querySelector(selector).textContent;
}
function captureTitle(row) {
return captureText(row, "._1SwE");
}
function captureValue(row) {
return captureText(row, "._3Tj5");
}
function formatDate(date) {
var seconds = date.getSeconds();
seconds = seconds < 10 ? '0'+seconds : seconds;
var hours = date.getHours();
hours = hours < 10 ? '0'+hours : hours;
var minutes = date.getMinutes();
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = "" + hours + minutes + seconds;
return "" + date.getFullYear() + date.getMonth()+1 + date.getDate() + "_" + strTime;
}
// Function to download data to a file
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
function downloadcsv() {
var date = 'XX/XX/XXX';
var csv_content = "";
function csvrow(row) {
if (row.childElementCount == 0) {
date = row.textContent;
}
else {
csv_content += date + ";" + captureTitle(row) + ";" + captureValue(row) + "\n";
}
}
csv_content = "";
// ._1kYs é uma linha de data
const DATE_SELECTOR = '._1kYs';
// .LB4I._3J0T é uma linha de valor
const ROW_SELECTOR = '.LB4I._3J0T';
// Preenche o csv_content
document.querySelectorAll(DATE_SELECTOR + ',' + ROW_SELECTOR).forEach(csvrow);
// title
transactions_title = captureText(document, "._2jLI");
download(csv_content, formatDate(new Date()) + "_" + transactions_title + ".csv", "csv");
}
function rafAsync() {
return new Promise(resolve => {
requestAnimationFrame(resolve); //faster than set time out
});
}
function checkElement(selector) {
if (document.querySelector(selector) === null) {
return rafAsync().then(() => checkElement(selector));
} else {
return Promise.resolve(document.querySelector(selector));
}
}
window.addEventListener('load', function () {
// Barra onde fica o botão de adicionar item
const SELETOR_BARRA = "._3fS2._1GHs";
checkElement(SELETOR_BARRA).then((element) => {
var bar = document.querySelector(SELETOR_BARRA);
console.log(bar);
// Botão de adicionar item (será clonado)
const SELETOR_BOTAO_ADICIONAR = "button.egUi";
checkElement(SELETOR_BOTAO_ADICIONAR).then((element) => {
var buttonAddTransaction = bar.querySelector(SELETOR_BOTAO_ADICIONAR);
console.log(buttonAddTransaction);
var buttonDownload = buttonAddTransaction.cloneNode(true);
buttonDownload.querySelector(".A4xM").innerHTML = "Baixar CSV";
// Adiciona na barra o botão clonado
bar.appendChild(buttonDownload);
// Muda o "onClick" para baixar arquivo
buttonDownload.onclick = function() {
downloadcsv();
};
console.info("Botão de download adicionado");
});
});
console.info("Terminou de carregar a página");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment