window.__p2m={ hook(){ if(window.__hooked) return; window.__hooked=1; const oc=HTMLInputElement.prototype.click; HTMLInputElement.prototype.click=function(){ if(this.type==='file'){ window.__lastFile=this; return;} return oc.call(this)}; }, modelBtn(){return [...document.querySelectorAll('button')].find(b=>/^(Neon|自動モード|Hydrogen|Carbon)/.test(b.innerText.trim()));}, async ensureNeon(){ const b=this.modelBtn(); if(!b) return 'nomodel'; if(b.innerText.trim().startsWith('Neon')) return 'neon'; b.click(); await new Promise(r=>setTimeout(r,800)); const it=[...document.querySelectorAll('[role=menuitem],[role=menuitemradio]')].find(e=>/Neon/.test(e.innerText)); if(!it){document.body.dispatchEvent(new KeyboardEvent('keydown',{key:'Escape',bubbles:true}));return 'noneon:'+[...document.querySelectorAll('[role=menuitem],[role=menuitemradio]')].map(e=>e.innerText.replace(/\s+/g,' ')).join('/');} it.click(); await new Promise(r=>setTimeout(r,800)); return this.modelBtn()?.innerText.trim().startsWith('Neon')?'neon':'fail'; }, async prep(tag){ this.hook(); document.querySelectorAll('[aria-label^="P2MUP"]').forEach(e=>e.removeAttribute('aria-label')); if(location.pathname!=='/app'){ document.querySelector('a[aria-label="新しいチャット"]').click(); await new Promise(r=>setTimeout(r,2500)); } const m=await this.ensureNeon(); if(m!=='neon') return 'MODEL '+m; window.__lastFile=null; document.querySelector('button[aria-label="アップロードとツール"]').click(); await new Promise(r=>setTimeout(r,1000)); const it=[...document.querySelectorAll('*')].filter(e=>e.childElementCount<4&&/^ファイルをアップロード$/.test((e.innerText||'').trim())).pop(); if(!it) return 'nomenu'; (it.closest('button,[role=menuitem]')||it).click(); await new Promise(r=>setTimeout(r,1200)); const f=window.__lastFile; if(!f) return 'nofile'; f.setAttribute('aria-label','P2MUP'+tag); return 'ok'; }, async send(){ let ok=false; for(let i=0;i<20;i++){ if([...document.querySelectorAll('*')].some(e=>e.childElementCount===0&&/(LOG|DAY)\d+__/.test(e.textContent||''))){ok=true;break;} await new Promise(r=>setTimeout(r,500)); } if(!ok) return 'noattach'; await new Promise(r=>setTimeout(r,2000)); if(!this.modelBtn()?.innerText.trim().startsWith('Neon')) return 'NOTNEON'; const e=document.querySelector("rich-textarea .ql-editor"); e.focus(); document.execCommand("selectAll"); document.execCommand("insertText",false,"添付ファイルの指示(手順・必ず守ること・お手本・出力)に従って、添付の「書き直す文章」を書き直してください。返事は、指示どおり ```json のコードブロック1つだけにしてください。"); await new Promise(r=>setTimeout(r,800)); const b=[...document.querySelectorAll('button')].find(b=>b.getAttribute('aria-label')==='プロンプトを送信'); if(!b||b.disabled) return 'nosend'; b.click(); await new Promise(r=>setTimeout(r,4000)); return 'sent '+location.pathname; }, async cycle(name,tag){const t0=Date.now();let prev=-1,r='';while(Date.now()-t0<30000){const rs=[...document.querySelectorAll('model-response')];const last=rs[rs.length-1];const code=last?.querySelector('code');const txt=code?code.innerText:'';const busy=!!document.querySelector('button[aria-label*="停止"]');if(!busy&&txt.length>0&&txt.length===prev){let ok=false;try{const o=JSON.parse(txt);ok=typeof o.rewritten==='string'}catch(e){ok='parsefail'}localStorage.setItem('p2m_res_'+name,txt);r='DONE '+name+' '+ok+' '+txt.length;break}if(!busy&&last&&!code&&Date.now()-t0>12000&&last.innerText.length>20){return 'NOCODE '+name+' '+last.innerText.slice(0,150)}prev=txt.length;await new Promise(r=>setTimeout(r,3000))}if(!r)return 'WAIT '+name+' '+prev;if(tag)r+=' | '+await this.prep(tag);return r} };