1

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');
TechWisdom
  • 3,960
  • 4
  • 33
  • 40
Jmuse
  • 17
  • 3
  • You will need to store `current` somewhere before leaving. `localStorage` might be what you're looking for. And for the refresh, see https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript for how you can detect a refresh. –  Mar 12 '19 at 17:44

1 Answers1

1

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);
}
  • Ideally you should only set the localStorage `lastCount` on page `unload`. –  Mar 12 '19 at 17:57
  • This code does not allow the counter to go up and also the line "obj.innerHTML = next;" is not defined – Jmuse Mar 12 '19 at 17:58
  • I have fixed the errors and tried the code and it works, of course it could be optimized to only work when page refresh [code here](https://codepen.io/yamidospina/pen/drZNzz?editors=1010) – Yamid Ospina Mar 12 '19 at 18:04
  • Works Great! Thanks. – Jmuse Mar 12 '19 at 18:09