I created a simple script that auto pauses any video that autoplays. The code is simple:
document.getElementsByClassName("ytp-play-button ytp-button")[0].click();
And if I run it in the Chrome dev console after the entire page loads, everything works.
However, if I run this in tampermonkey, I get the error of cannot do .click()
of undefined
, which basically means the script ran before that element loaded.
I have tried:
// @run-at document-idle
window.onload
$(window).on('load', function(){})
window.addEventListener('load', function () {})
document.addEventListener("DOMContentLoaded", function(event){})
But it just won't work. To test this, I put things in a main()
function and did a setTimeout(main,5000)
. If everything works as expected, then the video should be paused when it has played for 5 seconds. However, the video gets paused when it's only been 2 or 3 seconds depending on how fast the page loaded, meaning the script ran before everything had loaded. If I delete the setTimeout
, then things don't work at all. Any solutions?