32 lines
3.3 KiB
JavaScript
32 lines
3.3 KiB
JavaScript
// Native history, with a conservative same-tab fallback when Navigation API is unavailable.
|
|
(() => {
|
|
const button=document.querySelector('[data-app-back]');if(!button)return;
|
|
const key='plotdirector.history.v1',field='plotdirectorHistory';
|
|
let state={entries:{},current:null};try{state=JSON.parse(sessionStorage.getItem(key))||state;}catch{}
|
|
if(!state.entries||typeof state.entries!=='object')state={entries:{},current:null};
|
|
const nativeReplace=history.replaceState.bind(history),nativePush=history.pushState.bind(history);
|
|
const internal=url=>{try{return typeof url==='string'&&url.length>0&&new URL(url,location.href).origin===location.origin;}catch{return false;}};
|
|
const save=()=>{try{sessionStorage.setItem(key,JSON.stringify(state));}catch{}};
|
|
function register(previous){
|
|
if(history.state!=null&&(typeof history.state!=='object'||Array.isArray(history.state)))return;
|
|
const id=globalThis.crypto?.randomUUID?.()||`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;state.entries[id]={url:location.href,previous,length:history.length};state.current=id;
|
|
nativeReplace({...history.state,[field]:id},'',location.href);state.entries[id].navigationKey=window.navigation?.currentEntry?.key;save();
|
|
}
|
|
const known=history.state?.[field];
|
|
if(known&&state.entries[known]){state.current=known;state.entries[known].url=location.href;state.entries[known].navigationKey=window.navigation?.currentEntry?.key;save();}
|
|
else {const prior=state.entries[state.current];let same=false;try{same=!!document.referrer&&internal(document.referrer)&&new URL(document.referrer).pathname+new URL(document.referrer).search===new URL(prior?.url).pathname+new URL(prior?.url).search;}catch{}register(same&&history.length>prior.length?state.current:null);}
|
|
function previous(){
|
|
if(window.navigation?.entries){const rows=navigation.entries(),i=rows.findIndex(e=>e.key===navigation.currentEntry?.key);return i>0&&internal(rows[i-1].url)&&Object.values(state.entries).some(e=>e.navigationKey===rows[i-1].key&&e.url===rows[i-1].url);}
|
|
const entry=state.entries[history.state?.[field]];return !!entry?.previous&&!!state.entries[entry.previous]&&internal(state.entries[entry.previous].url);
|
|
}
|
|
const render=()=>button.disabled=!previous();
|
|
history.pushState=function(...args){const prior=state.current;nativePush(...args);register(prior);render();};
|
|
history.replaceState=function(value,title,url){const id=history.state?.[field];nativeReplace(value&&typeof value==='object'&&!Array.isArray(value)?{...value,...(id?{[field]:id}:{})}:value,title,url);if(id&&state.entries[id]){state.entries[id].url=location.href;state.entries[id].navigationKey=window.navigation?.currentEntry?.key;save();}render();};
|
|
window.addEventListener('popstate',()=>{try{state=JSON.parse(sessionStorage.getItem(key))||state;}catch{}state.current=history.state?.[field]||null;save();render();});
|
|
window.addEventListener('pageshow',()=>{try{state=JSON.parse(sessionStorage.getItem(key))||state;}catch{}state.current=history.state?.[field]||null;save();render();});
|
|
window.addEventListener('hashchange',()=>{const id=history.state?.[field];if(!id||state.entries[id]?.url!==location.href)register(id||state.current);render();});
|
|
window.navigation?.addEventListener('currententrychange',render);
|
|
button.addEventListener('click',()=>{if(previous())history.back();else render();});
|
|
render();
|
|
})();
|