0

I am trying to get some information from innerHTML from Youtube source page.

However, when I do this

var myStr = document.getElementById('player').innerHTML;
alert(myStr);

this only procs the alert if I visit the same Youtube page twice. If I navigate to a different video, no alert shows up, unless I refresh the page again.

I am also doing this:

if(document.readyState === 'loading') {
    document.addEventListener("load", searchSomething, false);
} else {
    searchSomething();
}

at the beginning of the Javscript just to make sure that I am getting the innerHTML after the whole page loads. What is the problem here? How do I fix this so that I do not have to refresh the page to proc the alert!

UPDATE ----------------------

I started to log every event that gets fired on Youtube. However, Youtube does not fire any events including "load", "unload", "hashchange". It only fires Javascript events if you refresh the page!!! How can this be possible?

Matt Choi
  • 169
  • 1
  • 13
  • document ready handler – Pranav C Balan Apr 11 '19 at 06:18
  • When changing the video, your script doesn't get executed at all. It only executes when the page loads. The URL is changed by the history API, so you should be able to listen to those events. See [this post](https://stackoverflow.com/questions/3522090/event-when-window-location-href-changes) for more details. – 31piy Apr 11 '19 at 06:19
  • Thank you for the prompt response! However, onhashstate, onpopstate, onload, none of them works in this case. I makes sense to me that my script does not get executed at all because changing video does not actually load the page... Any other suggestions? – Matt Choi Apr 11 '19 at 06:35

1 Answers1

0

If you use jquery it will be like this

$(document).ready(function() {
    var myStr = document.getElementById('player').innerHTML;
    alert(myStr);
}
KLeoS
  • 71
  • 1
  • 11
  • This is NOT the problem I think. This does not resolve the problem – Matt Choi Apr 11 '19 at 06:29
  • hmm what about you add a timeout? ``` setTimeout(function() { var myStr = document.getElementById('player').innerHTML; alert(myStr); }, 3000); ``` – KLeoS Apr 11 '19 at 06:36
  • I think when I click on a different video on Youtube, it does not actually load the page so the onload event does not get fired? I did add the timeout too but still no luck :( – Matt Choi Apr 11 '19 at 06:39
  • try this if (document.getElementById("player").innerHTML == "Play") { alert here } – KLeoS Apr 11 '19 at 06:43
  • player is actually div with a bunch of html elements,, nothing to do with the actual player – Matt Choi Apr 11 '19 at 07:25