Created
July 21, 2024 19:59
-
-
Save coo11/c537f37c760968bcc4c42bcfb5ac83ea to your computer and use it in GitHub Desktop.
Tampermonkey urlchange event polyfill
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
| (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