forked from kevduc/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoinbasePortfolioGains.user.js
81 lines (63 loc) · 3.53 KB
/
CoinbasePortfolioGains.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// ==UserScript==
// @name Coinbase Portfolio Gains
// @version 1.1.3-2021-05-17
// @description Shows Coinbase portfolio gain/loss
// @author KevDuc; tweaked by SMcCandlish
// @namespace https://github.com/SMcCandlish/Coinbase_Gains_TamperMonkey_Script
// @homepageURL https://github.com/SMcCandlish/Coinbase_Gains_TamperMonkey_Script
// @downloadURL https://raw.githubusercontent.com/SMcCandlish/Coinbase_Gains_TamperMonkey_Script/master/CoinbasePortfolioGains.user.js
// @updateURL https://raw.githubusercontent.com/SMcCandlish/Coinbase_Gains_TamperMonkey_Script/master/CoinbasePortfolioGains.user.js
// @supportURL https://github.com/SMcCandlish/Coinbase_Gains_TamperMonkey_Script/issues
// @match https://www.coinbase.com/*
// @grant none
// @run-at document-end
// @icon https://www.coinbase.com/img/favicon/favicon-128x128.png
// ==/UserScript==
// Based on original at: https://github.com/kevduc/userscripts
(function() {
'use strict';
const totalInvestment = 1770.34; // Change this to the total amount you invested (in your local currency)
const currencySymbol = `$`; // Change this to whatever you need, like £.
// Helper functions
const pause = (milli) => new Promise((resolve, reject) => setTimeout(resolve, milli));
const waitForTruthy = async (func, milli = 200) => {
let result;
while(!(result = func())) await pause(milli);
return result;
}
document.querySelectorWhenLoaded = async (query) => await waitForTruthy(() => document.querySelector(query));
// -------------------------
let totalInvestmentHistory = JSON.parse(localStorage.getItem('totalInvestmentHistory'));
// No previous investment
if (totalInvestmentHistory === null) totalInvestmentHistory = [];
const previousTotalInvestment = totalInvestmentHistory[0];
// totalInvestment value changed
if (totalInvestment !== 0 && totalInvestment !== previousTotalInvestment) {
totalInvestmentHistory.unshift(totalInvestment);
localStorage.setItem('totalInvestmentHistory', JSON.stringify(totalInvestmentHistory));
}
const invested = totalInvestmentHistory[0] || 0;
function updateROI(balance, roi) {
const value = parseFloat(balance.innerText.replace(/[$£€,]/g,''));
const profit = value - invested;
roi.style.color = profit > 0 ? 'green' : 'red';
roi.innerText = `${profit > 0 ? '+' : '-'}${currencySymbol}${Math.abs(profit).toFixed(2)} (${(100*profit/invested).toFixed(2)}%)`;
}
async function init() {
const balance = await document.querySelectorWhenLoaded('h1[class*="Balance__BalanceHeader"], h1[class*="Balance__BalanceHeader-ek4zs2-3"]');
const roi = document.createElement('h2');
roi.className = balance.className;
roi.style = `font-size: large; text-align: center;`;
roi.id = "balanceROI-tampermonkey";
const periodSelector = await document.querySelectorWhenLoaded('div[class*="PeriodSelector__SelectorContainer-sc-1w75yzt-0"]');
periodSelector.insertAdjacentElement('beforebegin', roi);
const periodSelector2 = await document.querySelectorWhenLoaded('div[class*="PeriodSelector__SelectorContainer-sc-1w75yzt"]');
periodSelector2.insertAdjacentElement('beforebegin', roi);
const balanceTextNode = balance.firstChild;
const update = () => updateROI(balance, roi);
const observer = new MutationObserver(update);
observer.observe(balanceTextNode, { characterData: true });
update();
}
init();
})();