// Ver 1.01 / 2026-11-11 14:21 / 김경현 (function(){ if(window.top!==window.self)return; const style=document.createElement('style'); style.textContent='.wexcel-login-dialog{box-sizing:border-box;width:360px;max-width:calc(100vw - 28px);max-height:calc(100dvh - 28px);padding:0;border:1px solid #dae3df;border-radius:18px;background:#fff;overflow:auto;box-shadow:0 18px 70px #163c342b}.wexcel-login-dialog::backdrop{background:rgba(225,237,231,.62)}.wexcel-login-dialog iframe{display:block;width:100%;height:380px;border:0;background:#fff}.wexcel-login-status{padding:28px;text-align:center;color:#415d51;font:14px sans-serif}.wexcel-login-status button{display:block;margin:16px auto 0;padding:8px 22px;border:1px solid #cdded5;border-radius:7px;background:#edf6f1;color:#176344;cursor:pointer}'; document.head.append(style); const dialog=document.createElement('dialog');dialog.className='wexcel-login-dialog';dialog.setAttribute('aria-label','로그인');document.body.append(dialog); let frame=null,checking=false,openerRefreshed=false; function refreshLoginOpener(){ if(openerRefreshed)return;openerRefreshed=true; try{const opener=window.opener;if(opener&&!opener.closed&&opener.location.origin===location.origin)opener.location.reload();}catch(error){} } function closeLogin(){dialog.close();dialog.replaceChildren();frame=null;document.querySelector('[data-wexcel-login]')?.focus();} function showError(){dialog.replaceChildren();const status=document.createElement('div'),button=document.createElement('button');status.className='wexcel-login-status';status.textContent='로그인 화면을 불러오지 못했습니다.';button.type='button';button.textContent='둘러보기';button.onclick=closeLogin;status.append(button);dialog.append(status);frame=null;} function openLogin(event){ event?.preventDefault();if(dialog.open)return; dialog.style.width='360px';frame=document.createElement('iframe');frame.title='문자나라 공통 로그인';frame.src='/WEXCEL/login_layer.php';dialog.replaceChildren(frame); frame.addEventListener('load',async()=>{if(!frame)return;const loadedFrame=frame;try{const url=new URL(frame.contentWindow.location.href);if(url.origin!==location.origin){showError();return;}if(url.pathname==='/WEXCEL/login_layer.php'){if(!frame.contentDocument.querySelector('form[name="loginFm"]'))showError();return;}try{const response=await fetch('/WEXCEL/tour.html?auth=1',{credentials:'same-origin',cache:'no-store'});if(response.ok&&(await response.json()).loggedIn&&frame===loadedFrame)refreshLoginOpener();}catch(error){}if(frame===loadedFrame)window.location.replace(url.href);}catch(error){if(frame===loadedFrame)showError();}}); frame.addEventListener('error',showError);dialog.showModal(); } async function checkLogin(){if(checking||document.hidden)return;checking=true;const loginFrame=frame;try{const response=await fetch('/WEXCEL/tour.html?auth=1',{credentials:'same-origin',cache:'no-store'});if(response.ok&&(await response.json()).loggedIn){if(frame&&frame===loginFrame)refreshLoginOpener();location.replace('/WEXCEL/');}}catch(error){}finally{checking=false;}} document.querySelectorAll('[data-wexcel-login]').forEach(button=>button.addEventListener('click',openLogin)); dialog.addEventListener('cancel',event=>{event.preventDefault();closeLogin();}); window.addEventListener('message',event=>{ if(!frame||event.source!==frame.contentWindow||event.origin!==location.origin||event.data?.channel!=='wexcel-login')return; const data=event.data; if(data.type==='authenticated'){refreshLoginOpener();return;} if(data.type==='navigate'&&typeof data.url==='string'){try{const url=new URL(data.url,location.origin);if(url.origin===location.origin&&!url.username&&!url.password)location.replace(url.href);}catch(error){}return;} if(data.type==='close'||data.type==='tour'){closeLogin();checkLogin();} else if(data.type==='resize'&&Number.isFinite(data.height)){dialog.style.width=data.expanded?'680px':'360px';frame.style.height=Math.max(300,Math.min(data.height,window.innerHeight-32))+'px';} }); window.addEventListener('pageshow',checkLogin);window.addEventListener('focus',checkLogin);document.addEventListener('visibilitychange',checkLogin); if(document.body.hasAttribute('data-login-open'))openLogin(); })();