Skip to content

Commit

Permalink
change clear format to nuinvest format
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus-Soares committed Dec 5, 2023
1 parent 1656ec5 commit 439ed6c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
6 changes: 3 additions & 3 deletions _locales/pt/messages.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"appName": {
"message": "Extrato da Clear em CSV"
"message": "Leitor do extrato da NuInvest"
},
"appDesc": {
"message": "Extensão para extrair o extrato da Clear para um arquivo CSV."
"message": "Extensão para extrair o extrato da NuInvest para um arquivo CSV."
},
"tabTitle": {
"message": "Converter Extrato da Clear para CSV"
"message": "Converter Extrato da NuInvest para CSV"
},
"convert": {
"message": "Converter"
Expand Down
18 changes: 9 additions & 9 deletions content-script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.method === "getExchangeStatement") {
var iframe = $.find("div.frame-holder > iframe")[0].contentWindow.document.body
var statement = $(iframe).find("div.exchange-statement").html()

sendResponse({ content: statement });
document.addEventListener("DOMContentLoaded", function () {
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.method === "getStatement") {
var table = $('#account-statement > div > div:nth-child(2) > div > table').html()
sendResponse({ content: table });
}
}
}
);
);
});
5 changes: 3 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"manifest_version": 3,
"default_locale": "en",
"action": {
"default_title": "Export Clear Statement to CSV",
"default_title": "Export NuInvest Statement to CSV",
"default_popup": "popup.html",
"default_icon": {
"16": "/images/icon16.png",
Expand All @@ -21,7 +21,8 @@
"content_scripts": [
{
"matches": [
"*://pro.clear.com.br/*"
"*://pro.clear.com.br/*",
"https://www.nuinvest.com.br/*"
],
"js": [
"thirdParty/jquery-3.6.0.min.js",
Expand Down
64 changes: 30 additions & 34 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,51 +52,47 @@ $(function () {

initTable();

function readStatements(tableContent) {
var operations = $("<div></div>")
.html(tableContent.content)
.find("div.table-content__item.pointer")
function readStatement(tableContent) {

var table = $("#resultTable").DataTable()
table.clear().draw()
for (let op of operations) {
// Check if the value is positive
if ($(op).find("div.value.positive.icon-positive").length > 0) {
var settlement = $(op)
.find("div.settlement > soma-caption.date.soma-caption.hydrated")
.text()
var value = $(op)
.find("div.value.positive.icon-positive > soma-caption.value.soma-caption.hydrated")
.text()
.replace("R$ ", "")
var description = $(op).find("div > soma-caption.description.soma-caption.hydrated")
.text()
.replace(/\s+/g, "")
.trim()

if (description.includes("RENDIMENTO")) {
const regexRendimentos = new RegExp("RENDIMENTO(.+)PAPEL(.+)", "g")
const [_, quantity, ticker] = regexRendimentos.exec(description)
var operations = $('<div></div>').html(tableContent).find('tbody tr')

table.row.add([settlement, ticker, quantity, value, "Rendimento"]).draw()
} else if (description.includes("JUROSS/CAPITAL")) {
const regexJuros = new RegExp("JUROSS\/CAPITAL([0-9]+)([A-Z0-9]+)", "g")
const [_, quantity, ticker] = regexJuros.exec(description)
operations.each(function (index, row) {
var date = $(row).find('td:nth-child(1)').text()
var description = $(row).find('td:nth-child(3)')
.text()
.replace(/\s+/g, "")
.trim()
var value = $(row).find('td:nth-child(4)')
.text()
.replace("R$ ", "")

table.row.add([settlement, ticker, quantity, value, "Juros s/ Capital"]).draw()
} else if (description.includes("DIVIDENDOS")) {
const regexDividendos = new RegExp("DIVIDENDOS([0-9]+)([A-Z0-9]+)")
const [_, quantity, ticker] = regexDividendos.exec(description)
if (description.includes("Rendimento")) {
const regexRendimentos = new RegExp("Rendimentos/([0-9]+)([A-Z][A-Z][A-Z][A-Z][0-9]+).*", "g")
const [_, quantity, ticker] = regexRendimentos.exec(description)

table.row.add([settlement, ticker, quantity, value, "Dividendos"]).draw()
}
table.row.add([date, ticker, quantity, value, "Rendimento"]).draw()
} else if (description.includes("Juros")) {
const regexJuros = new RegExp("Jurosdecapitalproprios/([0-9]+)([A-Z][A-Z][A-Z][A-Z][0-9]+)", "g")
const [_, quantity, ticker] = regexJuros.exec(description)

table.row.add([date, ticker, quantity, value, "Juros s/ Capital"]).draw()
} else if (description.includes("Dividendos")) {
const regexDividendos = new RegExp("Dividendoss/([0-9]+)([A-Z][A-Z][A-Z][A-Z][0-9]+)")
const [_, quantity, ticker] = regexDividendos.exec(description)

table.row.add([date, ticker, quantity, value, "Dividendos"]).draw()
}
}
});
}

$("#convert").click(function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { method: "getExchangeStatement" }, readStatements);
chrome.tabs.sendMessage(tabs[0].id, { method: "getStatement" }, function (response) {
readStatement(response.content);
});
});
})
});
});

0 comments on commit 439ed6c

Please sign in to comment.