Search code examples
javascripttampermonkey

How to start number on refresh


This code is for Tamper-monkey and it works amazon. It just changes the look of how much money you have and it counts up, I wanted to know if it is possible to make it so whatever number it is on when you refresh the counter starts at that number.

var oof = document.getElementById("gc-ui-balance-gc-balance-value");
oof.innerHTML = "0";
function animateValue(id){
var obj = document.getElementById(id);
var current = parseInt(obj.innerHTML);

setInterval(function(){
    obj.innerHTML =current++;
},0.1);
}

animateValue('gc-ui-balance-gc-balance-value');

Solution

  • You need to save the number in browser's memory, you have two options:

    And the initial value would be the stored value or the default value if nothing's stored.

    Example with localStorage:

    var oof = document.getElementById("gc-ui-balance-gc-balance-value");
    
    var lastCount = localStorage.getItem("lastCount");
    
    oof.innerHTML = lastCount || "0";
    
    function animateValue(id) {
        var obj = document.getElementById(id);
        var current = parseInt(obj.innerHTML);
    
        setInterval(function () {
            var nextCount = current++;
            localStorage.setItem("lastCount", nextCount);
            obj.innerHTML = nextCount;
        }, 0.1);
    }