Advertisement
Guest User

Untitled

a guest
May 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  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 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement