Skip to content

Commit

Permalink
Merge pull request #5 from leonampd/fix-extension
Browse files Browse the repository at this point in the history
Fix extension
  • Loading branch information
gushonorato authored Oct 7, 2020
2 parents 71ec4c4 + a05d231 commit 0888280
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 49 deletions.
111 changes: 67 additions & 44 deletions extrato-nubank.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(function() {

$(function() {

function startOfx() {
const startOfx = () => {
return `
OFXHEADER:100
DATA:OFXSGML
Expand All @@ -20,37 +19,32 @@ NEWFILEUID:NONE
<BANKTRANLIST>`;
}

function endOfx() {
return `
const endOfx = () =>
`
</BANKTRANLIST>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>`;

}

function bankStatement(date, amount, description) {
return `
const bankStatement = (date, amount, description) =>
`
<STMTTRN>
<TRNTYPE>OTHER</TRNTYPE>
<DTPOSTED>${date}</DTPOSTED>
<TRNAMT>${amount}</TRNAMT>
<MEMO>${description}</MEMO>
</STMTTRN>`;
}

function normalizeAmount(text) {
return text.replace('.', '').replace(',','.');
}
const normalizeAmount = (text) =>
text.replace('.', '').replace(',','.');

function normalizeDay(date) {
return date.split(' ')[0]
}
const normalizeDay = (date) =>
date.split(' ')[0];

function normalizeMonth(date) {
var month = date.split(' ')[1]
var months = {
const normalizeMonth = (date) => {
const month = date.split(' ')[1]
const months = {
'Jan': '01',
'Fev': '02',
'Mar': '03',
Expand All @@ -64,49 +58,78 @@ NEWFILEUID:NONE
'Nov': '11',
'Dez': '12'
}

return months[month];
}

function normalizeYear(date) {
var dateArray = date.split(' ');
const normalizeYear = (date) => {
const dateArray = date.split(' ');
if (dateArray.length > 2) {
return '20'+dateArray[2];
} else {
return new Date().getFullYear();
};
}

function normalizeDate(date) {
return normalizeYear(date)+normalizeMonth(date)+normalizeDay(date);
const normalizeDate = (date) =>
normalizeYear(date)+normalizeMonth(date)+normalizeDay(date);

const exportOfx = (ofx) => {
const openMonth = " " + document.querySelector('md-tab.ng-scope.active .period').textContent.trim();
const period = normalizeYear(openMonth) + "-" + normalizeMonth(openMonth);
link = document.createElement("a");
link.setAttribute("href", 'data:application/x-ofx,'+encodeURIComponent(ofx));
link.setAttribute("download", "nubank-" + period + ".ofx");
link.click();
}

function generateOfx() {
var ofx = startOfx();
const generateOfx = () => {
let ofx = startOfx();

$('.charge:visible').each(function(){
var date = normalizeDate($(this).find('.time').text());
var description = $(this).find('.description').text();
var amount = normalizeAmount($(this).find('.amount').text());
document.querySelectorAll('.charge:not([style=\'display:none\'])').forEach(function(charge){
const date = normalizeDate(charge.querySelector('.time').textContent);
const description = charge.querySelector('.description').textContent.trim();
const amount = normalizeAmount(charge.querySelector('.amount').textContent);

ofx += bankStatement(date, amount, description);
});

ofx += endOfx();
exportOfx(ofx);
}

var openMonth = " " + $($.find('md-tab.ng-scope.active .period')[0]).text().trim();
var period = normalizeYear(openMonth) + "-" + normalizeMonth(openMonth);
link = document.createElement("a");
link.setAttribute("href", 'data:application/x-ofx,'+encodeURIComponent(ofx));
link.setAttribute("download", "nubank-" + period + ".ofx");
link.click();
const createExportButton = () => {
const button = document.createElement('button');

button.classList.add('nu-button');
button.classList.add('secondary');
button.setAttribute('role', 'gen-ofx');
button.textContent = "Exportar para OFX";

button.addEventListener('click', generateOfx)

return button;
}

$(document).on('DOMNodeInserted', ".summary .nu-button:not(:contains('OFX'))", function () {
if ($(".summary.open [role=\"gen-ofx\"]").length > 0) {
return;
}
$('<button class="nu-button secondary" role="gen-ofx">Exportar para OFX</button>')
.insertAfter('.summary.open .nu-button')
.click(generateOfx);
});
});
const exportOfxButtonAlreadyExists = () =>
document.querySelectorAll(".summary.open [role=\"gen-ofx\"]").length > 0

const insertExportButtonCallback = (mutationList, observer) => {
if(mutationList == undefined || exportOfxButtonAlreadyExists()) return;

const generateBoletoButton = document.querySelector('.summary.open .nu-button');
if (generateBoletoButton == undefined) return;

const exportOfxButton = createExportButton();
generateBoletoButton.parentNode.appendChild(exportOfxButton);

observer.disconnect();
}

const targetElement = document.querySelector('.bills-browser');
const config = { attributes: true, childList: true, subtree: true }

const observer = new MutationObserver(insertExportButtonCallback);
observer.observe(targetElement, config)
})();

Loading

0 comments on commit 0888280

Please sign in to comment.