import {readFileSync} from 'node:fs'; import {MAIN_MENU_GROUPS} from './main-menu.mjs'; const root='https://www.dunerealms.com'; export function homepageHeaderTemplate(html){ const start=html.indexOf('
'),end=html.indexOf('')||!template.includes('id="portal-navigation"'))throw Error('Original homepage header invalid'); return template.replace(/(href|src)="\/(?!\/)/g,'$1="'+root+'/'); } export function mountSharedHomepageHeader(template,groups,root){ if(window.__duneHomepageHeaderMounted||window.top!==window.self)return; const old=document.querySelector('header.dune-site-header'); const original=document.querySelector('header.masthead'); const utility=(original||old)?.previousElementSibling,bar=(original||old)?.nextElementSibling; if(!old&&!(original&&utility?.classList.contains('utility')&&bar?.classList.contains('nav-bar')))return; window.__duneHomepageHeaderMounted=true; const home=!!document.querySelector('.arrival-grid')&&!!document.querySelector('#play-dialog'); const host=document.createElement('dune-shared-header');host.hidden=true; const shadow=host.attachShadow({mode:'open'}); const from=location.hostname==='wiki.dunerealms.com'?'':root; const load=(path)=>{const link=document.createElement('link');link.rel='stylesheet';link.href=from+path;shadow.append(link);return new Promise(resolve=>{link.onload=()=>resolve(true);link.onerror=()=>resolve(false);});}; const loaded=[load('/preview/portal.css'),load('/preview/sections.css'),load('/realm-design.css?v=shared-homepage-header-1')]; const holder=document.createElement('template');holder.innerHTML=template;shadow.append(holder.content); const brand=shadow.querySelector('.brand'),releaseBadge=document.createElement('span');releaseBadge.className='shared-alpha-badge';releaseBadge.textContent='Alpha';brand.append(releaseBadge); const releaseApi=location.hostname==='cp.dunerealms.com'?'/api/menu-badges':'https://cp.dunerealms.com/api/menu-badges'; fetch(releaseApi,{cache:'no-store'}).then(r=>{if(!r.ok)throw Error();return r.json();}).then(data=>{if(!/^\d+(?:\.\d+){3}$/.test(data.releaseVersion))throw Error();return data.releaseVersion;}).catch(()=>fetch('/release.json',{cache:'no-store'}).then(r=>r.ok?r.json():null).then(data=>data?.version)).then(version=>{if(/^\d+(?:\.\d+){3}$/.test(version||''))releaseBadge.textContent='Alpha '+version;}).catch(()=>{}); const nav=shadow.querySelector('#portal-navigation'),toggle=shadow.querySelector('.menu-toggle'),actions=shadow.querySelector('.nav-actions'); // Reuse the original homepage structure, maintaining all PR602 destinations in the matching menu groups. const addLink=(box,label,href)=>{const a=document.createElement('a');a.href=href;a.textContent=label;box.append(a);}; for(const group of nav.querySelectorAll('details')){ const label=group.querySelector('summary').childNodes[0].textContent.trim(); const box=group.querySelector('.dropdown'); for(const [text,href] of groups[label]||[])if(![...box.querySelectorAll('a')].some(a=>a.href===href))addLink(box,text,href); } // Keep the actual account control in its homepage position; its menu preserves the existing shared account routes. const account=actions.querySelector('.account-link'),accountGroup=document.createElement('details');accountGroup.className='shared-account-group'; const summary=document.createElement('summary');summary.className=account.className;summary.append(...account.childNodes); const accountBox=document.createElement('div');accountBox.className='dropdown';for(const [label,href] of groups['My Account']||[])addLink(accountBox,label,href); accountGroup.append(summary,accountBox);account.replaceWith(accountGroup); const staff=actions.querySelector('[data-staff-access]'); const oldStaff=old?.querySelector('[data-staff-access]')||bar?.querySelector('[data-staff-access]'); if(oldStaff){staff.hidden=oldStaff.hidden;const mirror=new MutationObserver(()=>{staff.hidden=oldStaff.hidden;});mirror.observe(oldStaff,{attributes:true,attributeFilter:['hidden']});setTimeout(()=>mirror.disconnect(),10000);} const welcome=location.hostname==='cp.dunerealms.com'?'/api/welcome':'https://cp.dunerealms.com/api/welcome'; fetch(welcome,{credentials:'include',cache:'no-store',signal:AbortSignal.timeout(6000)}).then(r=>r.ok?r.json():null).then(context=>{if(context)staff.hidden=context.staffAccess!==true;}).catch(()=>{}); const play=actions.querySelector('[data-play]');play.addEventListener('click',()=>{close();const dialog=document.querySelector('#play-dialog');if(home&&dialog?.showModal)dialog.showModal();else location.href=root+'/install.html';}); const menus=[...shadow.querySelectorAll('details')]; function close(){for(const menu of menus)menu.open=false;nav.classList.remove('open');toggle.setAttribute('aria-expanded','false');} toggle.addEventListener('click',()=>{const open=toggle.getAttribute('aria-expanded')!=='true';nav.classList.toggle('open',open);toggle.setAttribute('aria-expanded',String(open));}); for(const menu of menus)menu.addEventListener('toggle',()=>{if(menu.open)for(const other of menus)if(other!==menu)other.open=false;}); document.addEventListener('click',e=>{if(!e.composedPath().includes(host))close();}); document.addEventListener('keydown',e=>{if(e.key==='Escape'){const active=menus.find(m=>m.open);close();active?.querySelector('summary').focus();}}); for(const a of shadow.querySelectorAll('a'))a.addEventListener('click',close); const here=new URL(location.href); const active=home?'Home':here.hostname==='forums.dunerealms.com'||/\/(discord|staff|community-records|news)/.test(here.pathname)?'Community':here.hostname==='support.dunerealms.com'||here.hostname==='wiki.dunerealms.com'||/\/manual|\/install/.test(here.pathname)?'Discover':here.hostname==='shop.dunerealms.com'||/\/exchange|\/kits/.test(here.pathname)?'Trade':/\/realm-hub/.test(here.pathname)?'The realm':null; const homeLink=nav.querySelector('.nav-home');if(!home)homeLink.removeAttribute('aria-current'); for(const item of nav.children){if(item.textContent.trim().startsWith(active||'\0')){const current=item.querySelector('summary')||item;current.setAttribute('aria-current','page');current.classList.add('shared-current');}} // Managed badges remain in shared header links only; the same endpoint remains authoritative. for(const badge of old?.querySelectorAll('.dune-new-overlay')||[]){const source=badge.closest('a');const target=[...shadow.querySelectorAll('.dropdown a')].find(a=>a.href===source?.href);if(target){const mark=document.createElement('span');mark.className='shared-menu-badge';mark.textContent=badge.textContent;target.append(mark);}} const badges=location.hostname==='cp.dunerealms.com'?'/api/menu-badges':'https://cp.dunerealms.com/api/menu-badges'; fetch(badges,{cache:'no-store'}).then(r=>r.ok?r.json():null).then(data=>{for(const a of shadow.querySelectorAll('.dropdown a')){const item=data?.items?.find(x=>x.href===a.href);if(['NEW','UPDATE'].includes(item?.badge)){const badge=a.querySelector('.shared-menu-badge')||document.createElement('span');badge.className='shared-menu-badge';badge.textContent=item.badge;if(!badge.parentNode)a.append(badge);}}}).catch(()=>{}); (old||utility).before(host); Promise.all(loaded).then(results=>{ if(results.some(x=>!x)){host.remove();window.__duneHomepageHeaderMounted=false;return;} if(old){old.remove();if(utility?.classList.contains('utility'))utility.remove();if(bar?.classList.contains('nav-bar'))bar.remove();} else{utility.remove();original.remove();bar.remove();} host.hidden=false; }); } export function sharedHomepageHeaderFallback(html=readFileSync(new URL('../web/index.html',import.meta.url),'utf8')){ const template=homepageHeaderTemplate(html).replace('
','
').replace('
','
'); return '
'+template+'
'; } export function sharedHomepageHeaderScript(html=readFileSync(new URL('../web/index.html',import.meta.url),'utf8')){ return '\n;('+mountSharedHomepageHeader.toString()+')('+JSON.stringify(homepageHeaderTemplate(html))+','+JSON.stringify(MAIN_MENU_GROUPS)+','+JSON.stringify(root)+');\n'; }