-1

Im doing a google chrome extension and i was wondering why

window.onload = loadPage;
function loadPage() {
    document.getElementById('nav-robux-amount').innerHTML = '0';
    console.log("Robux are now in hide.");
}

is so slow and if it had any other alternative working faster.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Skully
  • 55
  • 1
  • 7

1 Answers1

5

That is true, since you have used window.onload, it will get called only after entire page is loaded completely. If you don't need your function to wait for all the loadings, you can try giving a shot to DOMContentLoaded.

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
});
Nish
  • 922
  • 13
  • 31
  • Its kinda working but its going way to fast i see it a milisecond and then the normal amount show back. Is there a way to do like a "wait" or "sleep" ? – Skully Jun 18 '21 at 13:36
  • Do you have Jquery library in your code? or it is plain vanilla JS – Nish Jun 18 '21 at 13:37
  • Its Javascript so .js – Skully Jun 18 '21 at 13:39
  • Use Promises to make your JS version of sleep. https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep – Tushar Shahi Jun 18 '21 at 13:43
  • It still just show for a milisecond then go back to the normal value – Skully Jun 18 '21 at 13:45
  • Sounds like there's some other JS running on the page which is replacing your replaced text. – phuzi Jun 18 '21 at 13:47
  • Settings done by you ```document.getElementById('nav-robux-amount').innerHTML = '0';``` is getting modified it seems. The value persists when you use window.onload right? but the problem is it is slower – Nish Jun 18 '21 at 13:48
  • window.onload work but its slow and i want it directly. But now it look like something else is editing my robux value – Skully Jun 18 '21 at 13:49
  • This is the problem with Chrome extensions. You're trying to run code on a page that is likely using a fair amount of JS. You'll need to debug the page and figure out what is causing the value to be updated and somehow update it afterwards. – phuzi Jun 18 '21 at 13:50
  • Ok i'll try to do that – Skully Jun 18 '21 at 13:51
  • Correct @Skully. You need to find out what is changing this value. if you control it, then above DOMContentLoaded will work – Nish Jun 18 '21 at 13:51
  • Ill try to do something like once ('nav-robux-amount') is loaded it edit – Skully Jun 18 '21 at 13:52