Skip to content

Instantly share code, notes, and snippets.

@coo11
Created July 21, 2024 19:59
Show Gist options
  • Select an option

  • Save coo11/c537f37c760968bcc4c42bcfb5ac83ea to your computer and use it in GitHub Desktop.

Select an option

Save coo11/c537f37c760968bcc4c42bcfb5ac83ea to your computer and use it in GitHub Desktop.
Tampermonkey urlchange event polyfill
(function() {
if (typeof window.onurlchange === 'function') {
return;
}
const oldPushState = history.pushState;
const oldReplaceState = history.replaceState;
function fireUrlChange() {
const event = new Event('urlchange');
window.dispatchEvent(event);
if (typeof window.onurlchange === 'function') {
window.onurlchange(event);
}
}
history.pushState = function(...args) {
oldPushState.apply(this, args);
fireUrlChange();
};
history.replaceState = function(...args) {
oldReplaceState.apply(this, args);
fireUrlChange();
};
window.addEventListener('popstate', fireUrlChange);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment