content_iframe.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. console.log('content_iframe', window.location)
  2. const _historyWrap = function (type) {
  3. const orig = history[type];
  4. const e = new Event(type);
  5. return function () {
  6. const rv = orig.apply(this, arguments);
  7. e.arguments = arguments
  8. window.dispatchEvent(e)
  9. return rv
  10. }
  11. }
  12. history.pushState = _historyWrap('pushState')
  13. history.replaceState = _historyWrap('replaceState')
  14. window.addEventListener('hashchange', function () {
  15. getRouterType()
  16. })
  17. window.addEventListener('popstate', function () {
  18. getRouterType()
  19. })
  20. window.addEventListener('pushState', function () {
  21. getRouterType()
  22. })
  23. window.addEventListener('replaceState', function () {
  24. getRouterType()
  25. })
  26. let router_data = []
  27. const getRouterType = () => {
  28. let key = history?.state?.key
  29. if (!key) {
  30. return
  31. }
  32. let index = router_data.indexOf(key)
  33. if (index < 0) {
  34. // 没有此页面
  35. router_data.push(key)
  36. index = router_data.indexOf(key)
  37. }
  38. let type = ''
  39. if (router_data.length == 1) {
  40. type = 'first&last'
  41. } else if (index + 1 == router_data.length) {
  42. type = 'last'
  43. } else if (index == 0) {
  44. type = 'first'
  45. }
  46. return type
  47. }