window.addEventListener('load',async()=>{ const wait=async check=>{for(let i=0;i<200;i++){if(check())return;const error=document.querySelector('[data-city-organize-error]')?.textContent;if(error&&!error.includes('保存中'))throw new Error(error);await new Promise(r=>setTimeout(r,25));}throw new Error('City organizer timed out');}; const click=(el,ctrl=false)=>{if(!el)throw new Error('Missing click target');el.dispatchEvent(new MouseEvent('click',{bubbles:true,ctrlKey:ctrl,cancelable:true}));}; const map=async()=>await(await fetch('/_/api/v1/city/map')).json(); const spot=(data,title)=>data.quartiers.flatMap(q=>q.streets.flatMap(s=>s.spots)).find(p=>p.title===title); const enterStreet=async id=>{click(document.querySelector(`[data-city-type="Street"][data-city-uuid="${id}"]`));await wait(()=>document.querySelector('.city-spot-tile'));}; const submit=()=>document.querySelector('.city-organize-dialog form').requestSubmit(); try{ let data=await map();const home=data.quartiers[0].streets.find(s=>s.name==='home'),work=data.quartiers[0].streets.find(s=>s.name==='work'); await wait(()=>document.querySelector('.city-quartier-tile'));click(document.querySelector('.city-quartier-tile')); await wait(()=>document.querySelectorAll('.city-generated-street-label').length===2);await enterStreet(home.uuid); await wait(()=>document.querySelectorAll('.city-undefined-page-link').length===2); const url=location.href,points=[...document.querySelectorAll('.city-undefined-page-link')]; click(points[0],true);click(points[1],true); if(document.querySelectorAll('.city-undefined-page-link.city-is-selected').length!==2||location.href!==url)throw new Error('Ctrl selection navigated or failed'); click(points[0],true);if(document.querySelectorAll('.city-undefined-page-link.city-is-selected').length!==1)throw new Error('Ctrl toggle failed');click(points[0],true); click(document.querySelector('.city-selection-bar [data-city-create-selection]')); const form=document.querySelector('.city-organize-dialog form');form.elements.title.value='まとめたSpot';form.elements.name.value='bundle';form.elements.name.dispatchEvent(new Event('input'));submit(); await wait(()=>!document.querySelector('.city-organize-dialog')); data=await map();const bundle=spot(data,'まとめたSpot');if(!bundle||bundle.pages.length!==2||!bundle.pages.every(p=>p.path.startsWith('/life/home/')))throw new Error('Grouping changed URLs or lost Pages'); await wait(()=>document.querySelector(`[data-city-uuid="${bundle.uuid}"]`)); // Right-click title edit, preserving identifier and Page URLs. document.querySelector(`[data-city-uuid="${bundle.uuid}"]`).dispatchEvent(new MouseEvent('contextmenu',{bubbles:true,cancelable:true})); click(document.querySelector('.city-context-menu [data-city-rename]')); document.querySelector('.city-organize-dialog input[name="title"]').value='新しい表示名';submit();await wait(()=>!document.querySelector('.city-organize-dialog')); data=await map();if(spot(data,'新しい表示名')?.uuid!==bundle.uuid||spot(data,'新しい表示名')?.name!=='bundle')throw new Error('Rename changed identity'); // Whole-Spot merge, using the floating toolbar rather than portal clicks. for(const title of ['alpha','beta']){const p=spot(data,title);await wait(()=>document.querySelector(`[data-city-uuid="${p.uuid}"]`));click(document.querySelector(`[data-city-uuid="${p.uuid}"]`),true);} click(document.querySelector('.city-selection-bar [data-city-merge]')); document.querySelector('.city-organize-dialog select[name="target"]').value=bundle.uuid;submit();await wait(()=>!document.querySelector('.city-organize-dialog')); data=await map();if(spot(data,'新しい表示名').pages.length!==4||spot(data,'alpha')||spot(data,'beta'))throw new Error('Whole Spot merge failed'); await wait(()=>document.querySelector(`[data-city-uuid="${bundle.uuid}"]`));click(document.querySelector(`[data-city-uuid="${bundle.uuid}"]`),true); click(document.querySelector('.city-selection-bar [data-city-move-selection]')); const move=document.querySelector('.city-organize-dialog form');move.elements.street.value=work.uuid;move.elements.street.dispatchEvent(new Event('change'));submit();await wait(()=>!document.querySelector('.city-organize-dialog')); data=await map();const moved=data.quartiers[0].streets.find(s=>s.uuid===work.uuid).spots.find(p=>p.uuid===bundle.uuid); if(!moved||moved.path_prefix!=='/life/work/bundle'||moved.pages.length!==4||!moved.pages.every(p=>p.path.startsWith('/life/home/')))throw new Error('Move lost identity or moved URLs'); const paths=await(await fetch('/_/api/v1/city/autoplan')).json(); if(paths.items.some(p=>p.path.startsWith('/life/home/')))throw new Error('Autoplan will undo manual organization'); click(document.querySelector('[data-city-level="quartier"]'));await wait(()=>document.querySelectorAll('.city-generated-street-label').length===2);await enterStreet(work.uuid); document.body.dataset.vimReady='true'; }catch(error){const el=document.createElement('div');el.id='error';el.textContent=error.stack||error.message;document.body.append(el);} });