Created
January 4, 2024 12:43
-
-
Save swvitaliy/7e3c6d362f2a9bcf45c71516dcb1e97b to your computer and use it in GitHub Desktop.
Userscript for creation a button "csv" for printing invest events into browser console as a csv (https://www.tinkoff.ru/invest/portfolios/events/)
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 tinkoff_invert_events_csv | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-01-04 | |
// @description Create a button "csv" for printing invest events into browser console as a csv | |
// @author swvitaliy | |
// @match https://www.tinkoff.ru/invest/portfolios/events/ | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=tinkoff.ru | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const csv = (data) => data.map((item) => JSON.stringify(item)) | |
.join('\n') | |
.replace(/(^\[)|(\]$)/mg, ''); | |
function generateCSV() { | |
const tables = document.querySelectorAll('table'); | |
console.assert(tables.length === 1, "should be exctly once table"); | |
/** | |
* @const DOMElement | |
*/ | |
const table = tables[0] | |
const tbody = table.childNodes[1] | |
const rows = tbody.childNodes; | |
const ans = [] | |
let curDate = null; | |
for (const row of rows) { | |
// console.assert(row.childNodes[0]) | |
if (!row.childNodes[0]) | |
continue; | |
if (row.childNodes[0].className?.indexOf('operationsDate') >= 0) { // read date | |
curDate = row.childNodes[0].innerText; | |
} else { // read transaction | |
const [time, description] = row.childNodes[0].innerText.split('\n'); | |
let fullDateTime = curDate + ' ' + time | |
let qty = row.childNodes[1].innerText; | |
qty = qty.replaceAll('\n', '') | |
const cost = row.childNodes[2].innerText.replaceAll('\n', ''); | |
const currency = cost && cost[cost.length-1] | |
ans.push([fullDateTime, description, qty, cost, currency]) | |
} | |
} | |
return ans | |
} | |
const prevButton = document.querySelector('button[data-qa-file="EventsCalendar"]') | |
function insertAfter(referenceNode, newNode) { | |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); | |
} | |
var csvBtn = document.createElement("button"); | |
csvBtn.innerHTML = "csv"; | |
csvBtn.onclick = function() { console.info(csv(generateCSV())) } | |
insertAfter(prevButton, csvBtn); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment