git.fiddlerwoaroof.com
src/history-events.js
30db14b1
 function polyfillCustomEvent() {
84348047
   if (typeof window.CustomEvent === "function") return false;
30db14b1
 
84348047
   function CustomEvent(event, params) {
30db14b1
     params = params || { bubbles: false, cancelable: false, detail: undefined };
84348047
     var evt = document.createEvent("CustomEvent");
     evt.initCustomEvent(
       event,
       params.bubbles,
       params.cancelable,
       params.detail
     );
30db14b1
     return evt;
   }
 
   CustomEvent.prototype = window.Event.prototype;
 
   window.CustomEvent = CustomEvent;
84348047
 }
30db14b1
 
f62307c9
 function runOnceFor(object, flag, cb) {
30db14b1
   if (!object[flag]) {
     object[flag] = true;
     cb();
   }
 }
 
 let MISSING_HISTORY = Symbol("missing_history");
 export default function addMissingHistoryEvents(window, history) {
f62307c9
   runOnceFor(history, MISSING_HISTORY, () => {
30db14b1
     const pushState = history.pushState.bind(history);
     const replaceState = history.replaceState.bind(history);
 
     polyfillCustomEvent();
 
84348047
     history.pushState = function(state, title, url) {
30db14b1
       let result = pushState(...arguments);
 
84348047
       var pushstate = new CustomEvent("pushstate", {
         detail: { state, title, url }
       });
30db14b1
       window.dispatchEvent(pushstate);
       return result;
     };
 
84348047
     history.replaceState = function(state, title, url) {
30db14b1
       const result = replaceState(...arguments);
 
84348047
       var replacestate = new CustomEvent("replacestate", {
         detail: { state, title, url }
       });
30db14b1
       window.dispatchEvent(replacestate);
       return result;
     };
   });
7b1d07fc
 }