Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

(function() {

'use strict';
function changeDOM() {

var unitMap = {BTC : { unit : 'BTC', factor : 1e3 },


satoshi : { unit : 'satoshi', factor : 1e8 },
microBTC : { unit : 'BTC', factor : 1e6 }
};

var unitChoice = unitMap.microBTC;

var balanceLi = document.getElementById("balance").parentNode;

//remove eventListener otherwise we run into a loop


balanceLi.removeEventListener('DOMNodeInserted', changeDOM);

var balance = document.getElementById("balance").innerHTML;


balance *= unitChoice.factor;

//remove balance and textnode


while (balanceLi.hasChildNodes()) {
balanceLi.removeChild(balanceLi.firstChild);
}

//recreate balance span


var balanceSpan = document.createElement("span");
balanceSpan.setAttribute('id', 'balance');

var balanceSpanText = document.createTextNode(balance);


balanceSpan.appendChild(balanceSpanText);

//recreate textnode
var balanceLiUnitText = document.createTextNode(' ' + unitChoice.unit);

//insert balance and textnode into existing and empty blanaceli


balanceLi.appendChild(balanceSpan);
balanceLi.appendChild(balanceLiUnitText);

//readd removed eventListener


balanceLi.addEventListener('DOMNodeInserted', changeDOM, false);

console.log('changed to ' + balance + ' ' + unitChoice.unit);


}

changeDOM();

var balanceLi = document.getElementById("balance").parentNode;


balanceLi.addEventListener('DOMNodeInserted', changeDOM, false);

})();

You might also like