Skip to content

Instantly share code, notes, and snippets.

@smichaelsen
Created February 19, 2022 12:12
Show Gist options
  • Save smichaelsen/60cff820b591866fd3dea2a4f3abf5a5 to your computer and use it in GitHub Desktop.
Save smichaelsen/60cff820b591866fd3dea2a4f3abf5a5 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Paypal YNAB Export
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.paypal.com/mep/dashboard
// @icon https://www.google.com/s2/favicons?domain=paypal.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const ROWS_SELECTOR = '[data-testid="allTileJSON"] tr';
const DATE_SELECTOR = 'td:nth-child(1) div[role="button"]';
const DESCRIPTION_SELECTOR = 'td:nth-child(2) a';
const AMOUNT_SELECTOR = 'td:nth-child(3) div[role="button"]';
const gatherData = () => {
const data = [];
data.push('"Date";"Payee";"Category";"Memo";"Outflow";"Inflow"')
document.querySelectorAll(ROWS_SELECTOR).forEach((row) => {
const date = row.querySelector(DATE_SELECTOR).innerText;
const description = row.querySelector(DESCRIPTION_SELECTOR).innerText;
const amount = row.querySelector(AMOUNT_SELECTOR).innerText;
const inflow = amount.indexOf('-') === -1 ? amount : '0';
const outflow = amount.indexOf('-') !== -1 ? amount : '0';
data.push(`"${date}";"${description}";"";"";"";"${amount}"`);
});
return data.join("\n");
};
const performExport = () => {
const csv = gatherData();
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const filename = "ynab-" + year + "-" + month + "-" + day + ".csv";
// create a temporary download link
let input = document.createElement("a");
input.setAttribute('href', 'data:text/csv;charset=UTF-8,' + encodeURIComponent(csv));
input.setAttribute('download', filename);
input.style.display = 'none';
// ...and click it to trigger the download
document.body.appendChild(input);
input.click();
document.body.removeChild(input);
};
const addExportButton = () => {
const button = document.createElement('button');
button.innerText = 'Export to YNAB';
button.style.position = 'fixed';
button.style.top = '250px';
button.style.right = '50px';
button.style.backgroundColor = '#4495D7';
button.style.border = 'none';
button.style.color = '#fff';
button.style.padding = '0 10px';
button.style.height = '40px';
button.style.fontWeight = 'bold';
button.style.borderRadius = '3px';
button.onclick = performExport;
document.querySelector('body').append(button);
};
window.setTimeout(addExportButton, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment