I\'m writing a Chrome extension so I need to be able to listen for changes in the YouTube URL (i.e., see that you switched videos). YouTube makes this hard because with its
I stumbled across this issue, so maybe this will benefit someone.
What did the trick for me is to listen for the yt-page-data-updated
event, which detects a video switch.
I was able to inspect it by using the monitorEvents(document.body)
and getEventListeners(document.body)
Chrome DevTools Commands upon page update. But keep in mind that it won't work on full page reload (by directly accessing the URL), so I had to coordinate with a load
event, something like this:
window.addEventListener('load', function () {
console.log('load');
});
window.addEventListener('yt-page-data-updated', function () {
console.log('url change');
});