-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvhistory.min.js
1 lines (1 loc) · 1.84 KB
/
vhistory.min.js
1
(function(){var h;var f=0;var i;var e=window.history;(function(){var j=function(k){var l=e[k];return function(){var n=l.apply(this,arguments);var m=new Event(k.toLowerCase());m.arguments=arguments||{};m.action=k;window.dispatchEvent(m);return n}};e.pushState=j("pushState");e.replaceState=j("replaceState");e.go=j("go");e.back=j("back");e.forward=j("forward")}());function a(){return e.state?(e.state.state?e.state.state:e.state):{}}function c(j){var k=a();this.pathName=j;this.state=JSON.stringify(k)}function g(j){return(j.pathName===window.location.pathname&&j.state===JSON.stringify(a()))}function b(){return new c(window.location.pathname)}function d(){h.push(b())}window.onload=function(){h=[new c(window.location.pathname)];window.addEventListener("go",function(j){i=true;f=f+j.arguments[0]});window.addEventListener("back",function(j){i=true;f--});window.addEventListener("forward",function(j){i=true;f++});window.addEventListener("pushstate",function(j){if(f===h.length-1){d();f++}else{h.splice(f+1);d();f++}});window.addEventListener("popstate",function(j){setTimeout(function(){if(i){i=false;return}if(g(h[f-1])){f--}else{if(g(h[f+1])){f++}}})});window.addEventListener("replacestate",function(k){var j=window.location.pathname;if(h&&h.length){h.splice(h-1,1,j)}else{h=[new c(j)]}})};e.getStack=function(){var j=[];h.forEach(function(k){j.push(k.pathName)});return j};e.backTo=function(m,l){l=l==null?true:l;var j=e.getStack();var k=l?j.lastIndexOf(m):j.indexOf(m);if(k>-1&&k<f){e.go(-(f-k))}else{console.error("Path does not exist")}};e.forwardTo=function(m,l){l=l==null?true:l;var j=e.getStack();var k=l?j.indexOf(m):j.lastIndexOf(m);if(k>-1&&k>f){e.go(k-f)}else{console.error("Path does not exist")}};e.getDistance=function(m,l){l=l==null?true:l;var j=e.getStack();var k=l?j.lastIndexOf(m):j.indexOf(m);if(k>-1){return(-(f-k))}else{console.error("Path does not exist")}}}());