|
38 | 38 | true
|
39 | 39 | );
|
40 | 40 | })();
|
| 41 | + |
| 42 | +// Navigation polyfill for Firefox and Safari, as of 2024-01-04 |
| 43 | +// NOTE: this is a very basic polyfill, it only supports firing a `navigate` |
| 44 | +// event on location change and even that without interception support, etc. |
| 45 | +!(function () { |
| 46 | + if (window.navigation == undefined) { |
| 47 | + |
| 48 | + class NavigateEvent extends CustomEvent { |
| 49 | + constructor() { |
| 50 | + super("navigate"); |
| 51 | + this.destination = { url: undefined }; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Create a navigation object on the window |
| 56 | + // We create a DOM element for the navigation object so that we can |
| 57 | + // attach events on it. |
| 58 | + window.navigation = document.createElement("div"); |
| 59 | + |
| 60 | + const create_event = (args) => { |
| 61 | + const event = new NavigateEvent(); |
| 62 | + event.destination.url = args[2]; |
| 63 | + return event; |
| 64 | + }; |
| 65 | + |
| 66 | + // Patch pushState to trigger an `navigate` event on the navigation |
| 67 | + // object when the URL changes. |
| 68 | + const pushState = window.history.pushState; |
| 69 | + window.history.pushState = function () { |
| 70 | + pushState.apply(window.history, arguments); |
| 71 | + window.navigation.dispatchEvent(create_event(arguments)); |
| 72 | + }; |
| 73 | + |
| 74 | + // Same with replaceState |
| 75 | + const replaceState = window.history.replaceState; |
| 76 | + window.history.replaceState = function () { |
| 77 | + replaceState.apply(window.history, arguments); |
| 78 | + window.navigation.dispatchEvent(create_event(arguments)); |
| 79 | + }; |
| 80 | + } |
| 81 | +})(); |
0 commit comments