pmx-vmd-sync-player / v121-runtime.js
mg696's picture
Add v121 runtime for normal URL copy and VPD animation detach
833f26d verified
Raw History Blame Contribute Delete
4.88 kB
// v121-runtime.js: single normal-URL patch. Copy button clone + VPD removes VMD animation from helper.
(function(){
const VER='v121';
let lastCopyAt=0;
function log(m,l){try{(window.__dbg||console.log)(m,l||'info')}catch(e){console.log(m)}}
function setVer(){try{document.title='MMD pmx Player '+VER;const v=document.getElementById('version-label');if(v)v.textContent=VER}catch(e){}}
function logText(){const el=document.getElementById('debug-log');return el?(el.innerText||el.textContent||''):''}
function flash(btn,ok){try{btn.textContent=ok?'✅コピー完了':'⚠コピー失敗';btn.style.background=ok?'#064':'#700';btn.style.color='#fff';setTimeout(()=>{btn.textContent='📋 Copy All';btn.style.background='';btn.style.color=''},1800)}catch(e){}}
function fallbackCopy(txt){try{const ta=document.createElement('textarea');ta.value=txt;ta.readOnly=true;ta.setAttribute('readonly','');ta.style.position='fixed';ta.style.left='8px';ta.style.top='8px';ta.style.width='2px';ta.style.height='2px';ta.style.opacity='0.01';ta.style.zIndex='999999';document.body.appendChild(ta);ta.focus({preventScroll:true});ta.select();ta.setSelectionRange(0,ta.value.length);const ok=document.execCommand('copy');document.body.removeChild(ta);return !!ok}catch(e){return false}}
function copyNow(e){if(e){try{e.preventDefault()}catch(_){}try{e.stopImmediatePropagation()}catch(_){}try{e.stopPropagation()}catch(_){}}const now=Date.now();if(now-lastCopyAt<1000)return;lastCopyAt=now;const btn=document.getElementById('debug-copy-btn');if(!btn)return;const txt=logText();if(fallbackCopy(txt)){flash(btn,true);log('[Copy:'+VER+'] copy ok len='+txt.length,'info');return}if(navigator.clipboard&&navigator.clipboard.writeText){navigator.clipboard.writeText(txt).then(()=>{flash(btn,true);log('[Copy:'+VER+'] async copy ok len='+txt.length,'info')}).catch(err=>{flash(btn,false);log('[Copy:'+VER+'] copy failed '+(err&&err.message?err.message:String(err)),'warn')})}else{flash(btn,false);log('[Copy:'+VER+'] copy failed no clipboard','warn')}}
function bindCopy(){const old=document.getElementById('debug-copy-btn');if(!old||old.__v121Copy)return;const btn=old.cloneNode(true);btn.__v121Copy=true;old.replaceWith(btn);btn.addEventListener('touchend',copyNow,{capture:true,passive:false});btn.addEventListener('click',copyNow,{capture:true,passive:false});log('[Copy:'+VER+'] single copy handler installed','info')}
function targetFromInput(el){return el&&el.id==='vmd-input2'?window.mesh2:window.mesh}
function clearMotionInput(id,why){const el=document.getElementById(id);if(el){el.value='';log('[Motion:'+VER+'] '+id+' cleared '+why,'info')}}
function removeAnimationKeepPhysics(mesh){
if(!mesh||!window.helper||!window.helper.objects)return false;
try{
const obj=window.helper.objects.get(mesh); if(!obj)return false;
const usePhys=!!obj.physics;
window.helper.remove(mesh);
window.helper.add(mesh,{physics:usePhys});
log('[VPD:'+VER+'] removed VMD animation and re-added physics='+usePhys,'info');
return true;
}catch(e){log('[VPD:'+VER+'] remove animation failed '+e.message,'warn');return false}
}
function stopMixer(mesh){try{const obj=window.helper&&window.helper.objects&&window.helper.objects.get(mesh);if(obj&&obj.mixer){obj.mixer.stopAllAction();obj.mixer.time=0;obj.mixer.timeScale=0;log('[VPD:'+VER+'] mixer stopped','info')}}catch(e){}}
function bindMotionInputs(){
[['file-input','vmd-input'],['file-input2','vmd-input2']].forEach(([modelId,motionId])=>{const m=document.getElementById(modelId);if(m&&!m.__v121ModelClear){m.__v121ModelClear=true;m.addEventListener('change',()=>clearMotionInput(motionId,'model slot changed'),true)}});
['vmd-input','vmd-input2','cam-vmd-input'].forEach(id=>{const el=document.getElementById(id);if(!el||el.__v121SameFile)return;el.__v121SameFile=true;['click','touchstart','pointerdown'].forEach(ev=>el.addEventListener(ev,()=>{try{el.value=''}catch(e){};log('[Motion:'+VER+'] '+id+' cleared before picker','info')},{capture:true,passive:true}));el.addEventListener('change',()=>setTimeout(()=>{try{el.value=''}catch(e){}},0),false)});
['vmd-input','vmd-input2'].forEach(id=>{const el=document.getElementById(id);if(!el||el.__v121Vpd)return;el.__v121Vpd=true;el.addEventListener('change',()=>{const files=Array.from(el.files||[]);if(files.some(f=>/\.vpd$/i.test(f.name||''))){const mesh=targetFromInput(el);log('[VPD:'+VER+'] VPD selected: will detach prior VMD for '+id,'info');setTimeout(()=>{removeAnimationKeepPhysics(mesh);stopMixer(mesh)},450);setTimeout(()=>stopMixer(mesh),1000)}},false)})
}
function bind(){setVer();setTimeout(setVer,500);setTimeout(setVer,1500);bindCopy();bindMotionInputs();log('[Runtime:'+VER+'] normal URL patch enabled','info')}
function wait(){if(window.__appReady)bind();else setTimeout(wait,100)}
wait();
})();