Here is my working example:
(function () {
let lastUrl = location.href;
function runCustomScript() {
document.querySelectorAll("p").forEach(node => {
{ node.style.backgroundColor = "Yellow"; }
});
}
function checkUrlChange() {
const currentUrl = location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
runCustomScript();
}
}
const pushState = history.pushState;
const replaceState = history.replaceState;
history.pushState = function () {
const result = pushState.apply(this, arguments);
checkUrlChange();
return result;
};
history.replaceState = function () {
const result = replaceState.apply(this, arguments);
checkUrlChange();
return result;
};
window.addEventListener("popstate", checkUrlChange);
setInterval(checkUrlChange, 1000);
runCustomScript();
})();