SHARE
TWEET

Untitled

a guest May 19th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import _ from 'lodash'
  2.  
  3. // eslint-disable-next-line no-undef
  4. class URLChangeEvent extends HashChangeEvent {
  5.   get [Symbol.toStringTag] () {
  6.     return 'URLChangeEvent'
  7.   }
  8. }
  9.  
  10. let urlCache = window.location.href
  11.  
  12. const onURLChange = () => {
  13.   if (window.location.href !== urlCache) {
  14.     const event = new URLChangeEvent('urlchange', {
  15.       oldURL: urlCache,
  16.       newURL: window.location.href
  17.     })
  18.     window.dispatchEvent(event)
  19.     typeof window.onurlchange === 'function' && window.onurlchange(event)
  20.     urlCache = window.location.href
  21.   }
  22. }
  23.  
  24. const registryOnChangeURL = _.once(() => {
  25.   const pushStateNative = window.history.pushState
  26.   window.history.pushState = (...args) => {
  27.     pushStateNative.apply(window.history, args)
  28.     onURLChange()
  29.   }
  30.   window.addEventListener('hashchange', onURLChange)
  31. })
  32.  
  33. export { registryOnChangeURL }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top