Last active
April 4, 2022 18:04
-
-
Save saitergun/6a3627232c2c6072abc0501715d569e9 to your computer and use it in GitHub Desktop.
catch url changes with javascript credit https://stackoverflow.com/a/52809105/5765524
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
// history.pushState = ( f => function pushState(){ | |
// var ret = f.apply(this, arguments); | |
// window.dispatchEvent(new Event('pushstate')); | |
// window.dispatchEvent(new Event('locationchange')); | |
// return ret; | |
// })(history.pushState); | |
// history.replaceState = ( f => function replaceState(){ | |
// var ret = f.apply(this, arguments); | |
// window.dispatchEvent(new Event('replacestate')); | |
// window.dispatchEvent(new Event('locationchange')); | |
// return ret; | |
// })(history.replaceState); | |
// window.addEventListener('popstate',()=>{ | |
// window.dispatchEvent(new Event('locationchange')) | |
// }); | |
// window.addEventListener('locationchange', function(){ | |
// console.log('location changed!'); | |
// }) | |
let previousUrl = '' | |
const observer = new MutationObserver(function(mutations) { | |
if (location.href !== previousUrl) { | |
previousUrl = location.href | |
console.log(`Navigated to ${location.href}`) | |
} | |
}) | |
const config = { subtree: true, childList: true } | |
observer.observe(document, config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment