0% found this document useful (0 votes)
186 views1 page

Balance Script

This JavaScript code converts a cryptocurrency balance from one unit to another by: 1) Defining a unit mapping object to convert between units like BTC, satoshis, and microBTC. 2) Getting the balance element and removing its event listener to prevent looping. 3) Multiplying the existing balance by the conversion factor. 4) Recreating the balance element and text node with the new value and unit. 5) Readding the event listener to the balance element to handle future changes.

Uploaded by

LZGAMERHQ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views1 page

Balance Script

This JavaScript code converts a cryptocurrency balance from one unit to another by: 1) Defining a unit mapping object to convert between units like BTC, satoshis, and microBTC. 2) Getting the balance element and removing its event listener to prevent looping. 3) Multiplying the existing balance by the conversion factor. 4) Recreating the balance element and text node with the new value and unit. 5) Readding the event listener to the balance element to handle future changes.

Uploaded by

LZGAMERHQ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

(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