Last active
August 29, 2022 11:57
-
-
Save otherjohn/9d6867cb142674e140f8fcd3a8d6797b to your computer and use it in GitHub Desktop.
Watching For Page Load and Page Change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Listeners | |
!(async function () { | |
// listen to page change | |
let e = location.href; | |
new MutationObserver(() => { | |
const t = location.href; | |
t !== e && ((e = t), urlChanged()); | |
}).observe(document, { | |
subtree: !0, | |
childList: !0, | |
}); | |
// listen to app loadeed | |
new MutationObserver( (mutations) => { | |
mutations.forEach( (mutationRecord) => { | |
if (mutationRecord.target.id === "app") { | |
pageLoaded(); | |
} | |
}) | |
}).observe(targetApp, { | |
attributes: true, | |
attributeFilter: ["style", "loading"], | |
}); | |
})(); | |
const urlChanged = async function() { | |
// the url has changed, you can now fire off your code. | |
// insert your code here. | |
console.info('Url Changed!'); | |
}; | |
const pageLoaded = async function() { | |
// the page has loaded, you can now fire off your code. | |
// insert your code here | |
console.info('Page Loaded'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment