I'm trying to write a primitive google chrome extension that gets the name of the current youtube channel.
So I use MutationObserver To do this. MutationObserver waits for the desired node to appear on the page.
This code works:
var observer = new MutationObserver(function (mutations) {
if (document.querySelector("#text a")) {
console.log(document.querySelector("#text a").innerText);
observer.disconnect();
}
});
const target = document.querySelector("body");
const config = { childList: true, characterData: true };
observer.observe(target, config);
But since Youtube changes the content of the page without reloading. I am trying to restart MutationObserver to get the new channel name again. But MutationObserver doesn't get called from addEventListener.
This example code doesn't work:
window.addEventListener("popstate", function() {
var observer = new MutationObserver(function (mutations) {
if (document.querySelector("#text a")) {
console.log(document.querySelector("#text a").innerText);
observer.disconnect();
}
});
const target = document.querySelector("body");
const config = { childList: true, characterData: true };
observer.observe(target, config);
});
How would I implement this task to receive the desired node with a fresh channel name every time the url address changes without page reloading?
window.addEventListener("popstate", function() {
var observer = new MutationObserver(function (mutations) {
if (document.querySelector("#text a")) {
console.log(document.querySelector("#text a").innerText);
observer.disconnect();
}
});
const target = document.querySelector("body");
const config = { childList: true, characterData: true };
observer.observe(target, config);
});
If I go forward or go back on the history of links any code works except MutationObserver call.