I'm currently working on a Chrome Extension. I did two ways to monitor URL changes:
- Use a mutation Observer to observe all sub-elements changing;
- Check if the
window.location.href
is equal to the old href.
The first one is working, but it may cost too many calls and not be easy to extend in the future. The second one is simple, the code is:
var currentPage = window.location.href;
setInterval(() => {
if (currentPage != window.location.href) {
currentPage = window.location.href;
$(function() {
myFunction(); //append some buttons inside the HTML
});
}
}, 500);
But this doesn't work, since when it finds that the currentPage
has changed, the myFunction
will append buttons in the old HTML pages immediately since the new page is not reloaded, but the old one is.
I'm wondering if there are any ways to just change a little bit in this code and make it work, so that the buttons will append to the current page correctly.