-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimain.js
More file actions
1 lines (1 loc) · 1.94 KB
/
minimain.js
File metadata and controls
1 lines (1 loc) · 1.94 KB
1
function resetScroll(){window.innerWidth<=700?window.scrollTo(0,775):window.scrollTo(0,0)}class Page{path=location.pathname;constructor(){pageChanger.add(this),this.init()}init(){console.warn("init() needs to be a part of: "+this)}}class PageChanger{domainName=location.origin;loadedScripts=[];loadedPageScripts=[];constructor(){this.loadInitScripts()}add(e){this.loadedPageScripts.push(e)}init(){this.loadedPageScripts.forEach(e=>{location.pathname===e.path&&e.init()})}loadInitScripts(){for(let e=0;e<document.head.children.length;e++)"SCRIPT"===document.head.children[e].nodeName&&""!==document.head.children[e].src&&this.loadedScripts.push(document.head.children[e].src);this.setWindowOnPopState()}setWindowOnPopState(){window.onpopstate=()=>{this.changePage(location.pathname,()=>{history.replaceState(location.pathname,document.title,document.location),pageChanger.init()})}}changeURL(e){history.pushState(e,document.title,e),this.setWindowOnPopState(),this.init()}changeTo(e){location.pathname!==e&&this.changePage(e,()=>{this.changeURL(e)})}changeHead(e){document.head.innerHTML=null;for(let t=0;t<e.children.length;t++){let n=!1,a="SCRIPT"===e.children[t].nodeName&&e.children[t].src.length>0;if(a){for(let i=0;i<this.loadedScripts.length;i++)if(this.loadedScripts[i]===e.children[t].src){n=!0;break}n||this.loadedScripts.push(e.children[t].src)}if(!n){let h=document.createElement(e.children[t].nodeName);for(let r=0;r<e.children[t].getAttributeNames().length;r++){let d=e.children[t].getAttributeNames()[r],s=e.children[t].getAttribute(d);h.setAttribute(d,s)}h.innerHTML=e.children[t].innerHTML,document.head.append(h)}}}changeBody(e){document.body.innerHTML=e.innerHTML}changePage(e,t){let n=new Request(this.domainName+e);fetch(n).then(e=>e.text()).then(e=>{try{let n=new DOMParser;e=n.parseFromString(e,"text/html")}catch(a){console.error("Could not parse html: "+a)}return this.changeHead(e.head),this.changeBody(e.body),resetScroll(),t()})}}const pageChanger=new PageChanger;