Skip to content

Instantly share code, notes, and snippets.

@otherjohn
Last active August 29, 2022 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otherjohn/9d6867cb142674e140f8fcd3a8d6797b to your computer and use it in GitHub Desktop.
Save otherjohn/9d6867cb142674e140f8fcd3a8d6797b to your computer and use it in GitHub Desktop.
Watching For Page Load and Page Change
// 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