jerous' ::1

Just a quick script that adds some information to tables on \

  • kraken.com
  • bittrex.com

Download

Read the entire post to view the source.

  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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// ==UserScript==
// @name        trade-scripts
// @author jerous
// @include https://www.kraken.com/u/trade
// @include https://www.kraken.com/u/history/ledger
// @include https://bittrex.com/Balance
// @include https://bittrex.com/History
// @include https://www.binance.com/userCenter/depositWithdraw.html
// @downloadURL https://jerous.org/att/2017/07/31/kraken-rates/trade-scripts.js
// @version     1.4
// ==/UserScript==

// Version history:
// 1.0 initial version
// 1.1 update columns when table is updated
// 1.2 also modify history ledger table
// 1.3 include bittrex stuff
// 1.4 include simple Binance stuff

function e(id) {  return document.getElementById(id); }
function ce(t) { return document.createElement(t); }
function ct(t) { return document.createTextNode(t); }

function log(x) { console.log(x); }
function getElementsByXpath(path) {  return document.evaluate(path, document, null, XPathResult.ANY_TYPE, null); }
function getElementByXpath(path) { return getElementsByXpath(path).singleValue; }

function myRound(x, digits) { return Math.round(x*Math.pow(10,digits))/Math.pow(10,digits); }

function kraken_updateBalances() {
  var tbl = e('DataTables_Table_2');
  
  // Check if the header has enough columns
  var thead_tr0 = tbl.children[0].children[0];
  if (thead_tr0.childElementCount == 3) {
    var col = document.createElement('th');
    col.appendChild(ct('EUR'));
    thead_tr0.appendChild(col);
  }
  
  var convert=function(el) {
    var n1=parseFloat(el.children[1].getAttribute('sort'));
    var n2=parseFloat(el.children[2].getAttribute('sort'));
    if (el.childElementCount==3) { el.appendChild(ce('td')); }
    el.children[3].innerHTML=Math.round(n1*n2*100)/100;
  };
  [].forEach.call(tbl.getElementsByClassName('odd'), convert);
  [].forEach.call(tbl.getElementsByClassName('even'), convert);
}


function kraken_updateLedger(table) {
  var tbl = e(table);
  
  // Check if the header has enough columns
  var thead_tr0 = tbl.children[0].children[0];
  if (thead_tr0.childElementCount == 7) {
    var col = document.createElement('th');
    col.appendChild(ct('Rate'));
    thead_tr0.appendChild(col);
  }
  
  var convert=function(el) {
    var x1=el.children[4].textContent.replace(',','').replace('-','');
    var x2=el.nextElementSibling.children[4].textContent.replace(',','').replace('-','');
    var n1=parseFloat(x1.substring(1));
    var n2=parseFloat(x2.substring(1));
    if (el.childElementCount==7) { el.appendChild(ce('td')); }
    el.children[7].innerHTML=Math.round(n1/n2*100)/100+' '+x1.substring(0,1)+'/'+x2.substring(0,1);
  };
  [].forEach.call(tbl.getElementsByClassName('odd'), convert);
  [].forEach.call(tbl.getElementsByClassName('even'), convert);
}







// sym_value_columns: [symbol-name, total value in symbol-name] -- will use localStorage to calculate the est values
// btc_column: [total value in BTC]
// choose exactly one of sym_value_columns and btc_column
function bittrex_updateTable(tableId, normalNumColumns, sym_value_columns, btc_column) {
  var tbl=e(tableId);
  if (!tbl) { return; }
  
  var thead_tr0 = tbl.children[0].children[0];
  var tbody = tbl.children[1];
  
  var nRows=tbody.childElementCount;
  
  if (thead_tr0.childElementCount==normalNumColumns) {
    // Table is not edited!
    var col = document.createElement('th');
    col.appendChild(ct('Est. USD Value'));
    thead_tr0.appendChild(col);
  }
  if (tbody.children[0].childElementCount==normalNumColumns) {
    for(var i=0; i<nRows; i++) {
      tbody.children[i].appendChild(ce('td'));
    }
  }
  
  var btc_usd=parseFloat(localStorage.getItem("btc_usd"));
  var usd_eur=parseFloat(localStorage.getItem("usd_eur"));
  
  if (btc_column.length==1) {
    for(var i=0; i<nRows; i++) {
        var est_btc=parseFloat(tbody.children[i].children[btc_column[0]].textContent);
        tbody.children[i].children[normalNumColumns].textContent=
          myRound(est_btc*btc_usd,2)+'$ ('+myRound(est_btc*btc_usd*usd_eur,0)+'€)';
    }
  } else if (sym_value_columns.length==2) {
    for(var i=0; i<nRows; i++) {
        var sym=tbody.children[i].children[sym_value_columns[0]].textContent.toLowerCase();
        var sym_value=parseFloat(tbody.children[i].children[sym_value_columns[1]].textContent);
      	
      	if (localStorage.getItem(sym+"_btc")) {
          var sym_btc=parseFloat(localStorage.getItem(sym+"_btc"));
          var est_btc=sym_value*sym_btc;
          tbody.children[i].children[normalNumColumns].textContent=
            myRound(est_btc,6)+'฿'+
            ' (≈'+myRound(est_btc*btc_usd,0)+'$'+
            '  ≈'+myRound(est_btc*btc_usd*usd_eur,0)+'€)';
        } else {
          tbody.children[i].children[normalNumColumns].textContent='(No conversion data)';
        }
    }
  }
}

function bittrex_updateBalances() {
  if (window.location.href.startsWith('https://bittrex.com/Balance')) {
  	localStorage.clear();
  	var col_m6=document.getElementsByClassName("col-md-6")[1].children[0];
    var info = col_m6.textContent;
    var matches=/.* ([0-9.]+) BTC \/ ([0-9.]+) USD/.exec(info);
    var usd=parseFloat(matches[2]), btc=parseFloat(matches[1]);
    localStorage.setItem('btc_usd', usd/btc);
    localStorage.setItem('usd_eur', 1./1.18263);

    if (col_m6.childElementCount==0) {
      var el=ce("span");
      el.setAttribute("id", "totalEUR");
      el.appendChild(ct(' (in Euro)'));
      col_m6.appendChild(el);
    }

    // Update all the data we have
    var tbl=e('balanceTable');
    var tbody = tbl.children[1];
    var nRows=tbody.childElementCount;
    for(var row=0; row<nRows; row++) {
      var symbol=tbody.children[row].children[2].textContent.toLowerCase();
      var total_cur=parseFloat(tbody.children[row].children[6].textContent.toLowerCase());
      var total_est_btc=parseFloat(tbody.children[row].children[7].textContent.toLowerCase());
      localStorage.setItem(symbol+"_btc", total_est_btc/total_cur);
    }


    var btc_usd=parseFloat(localStorage.getItem("btc_usd"));
    var usd_eur=parseFloat(localStorage.getItem("usd_eur"));
    var totalEUR=e('totalEUR');
    totalEUR.textContent=((" (Est. EUR "+myRound(usd*usd_eur,0)+"€)"));
  }
  
  bittrex_updateTable('balanceTable', 9, [2,6], []);
  bittrex_updateTable('allClosedOrdersTable', 9, [], [8]);
  bittrex_updateTable('depositHistoryTable', 4, [2,3], []);
}

function binance_updateTables() {
  // Something like " Estimated Value: 0.00330592 BTC / $55.61 "
  var total=document.evaluate("//li[@class='total']", document, null,  XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent;
  var matches=/.*[^0-9.]([0-9.]+)\s+BTC\s+[/]\s+[$]([0-9.]+)[^0-9.]/.exec(total);
  var usd=parseFloat(matches[2]), btc=parseFloat(matches[1]);
  
  var btc_usd=usd/btc;
  
	[].forEach.call(
    document.getElementsByClassName('equalValue'),
  	function(el) {
      var btc=parseFloat(el.textContent);
      if (btc>0) {
	      el.textContent=btc+" (~ $"+myRound(btc*btc_usd,0)+")";
      }
    });
}

if (window.location.href.startsWith('https://www.kraken.com')) {
   if (window.location.href=="https://www.kraken.com/u/history/ledger") {
     setInterval(function() { kraken_updateLedger('DataTables_Table_0'); }, 2000); 
   } else if (window.location.href.startsWith('https://www.kraken.com/u/trade')) {
     setInterval(kraken_updateBalances, 2000);
     setInterval(function() { kraken_updateLedger('DataTables_Table_5'); }, 2000);
   }
} else if (window.location.href.startsWith('https://bittrex.com/')) {
   setInterval(bittrex_updateBalances, 1000);
} else if (window.location.href.startsWith('https://www.binance.com')) {
   setTimeout(binance_updateTables, 3000);
   setInterval(binance_updateTables, 10000);
}

Add a comment

Name (required)
Email (optional, not shown)
Comment (max 1000 characters)

[printer friendly] [static version] [Post listing] [Page listing] [Tags: music tab jazz travel wdb europe code live programming youtube record ]