Help With JS Error
-
I am working on a mod that requires js because of some of the limitations of css (no effect on parent). I am getting this error:
Uncaught TypeError: Cannot read property 'addEventListener' of null at custom.js:14
Normally this could be solved by putting the code in a window.addEventListener("load") function but not in Vivaldi, I believe because the browser elements are rendered dynamically. My code does everything I want when I paste it into the devtools console.
-
So, I figured it out using @lonm's code:
function initMod(){ if(document.querySelector("#browser")){ //what you want to execute } else { setTimeout(initMod, 500); } }
-
@code3 Edit: Looks like you figured it out
I don't know if you have taken a look around at some of the other mods posted here, but the solution most people use is timers.
Like this:
// Loop waiting for the browser to load the UI let intervalID = setInterval(function () { const browser = document.getElementById("browser"); if (browser) { clearInterval(intervalID); callYourFunctionHere(); // Insert relevant function call here } }, 300); // Don't forget to thank @luetage for the standard 300 ms
You can check to see if the
#browser
element is loaded to cover most standard mods, but some mods require checking more specialized elements.It would help to know what element you are trying to add the listener to. A short segment of the relevant code is useful for helping with code issues.