import {spawnSync} from 'node:child_process'; import {connectWebView} from '../android-webview-cdp.mjs'; import {readRealReview} from './real-account-review-ledger.mjs'; export async function connectRealAccountWebView(){ const state=readRealReview(),session=state.reviewSession; if(!session||session.sourceMode!=='production-real'||session.packageId!=='cn.fmode.app')throw Error('没有可用的真实包调试会话'); const adb=args=>{const r=spawnSync('adb',['-s',session.serial,...args],{encoding:'utf8',timeout:30000,windowsHide:true});if(r.status!==0)throw Error('设备只读核对失败');return r.stdout.trim()}; if(adb(['shell','pidof',session.packageId])!==session.pid)throw Error('App进程已变化,先重新核对连接,不复用旧端口'); const file=adb(['shell','pm','path',session.packageId]).split(/\r?\n/).find(x=>x.startsWith('package:'))?.slice(8); const receipt=state.actions['build_'+session.revision]?.receipt; if(!file||!receipt||adb(['shell','sha256sum',file]).split(/\s+/)[0]!==receipt.sha256)throw Error('当前真实包摘要未匹配本轮构建'); const verifyForward=()=>{ const rows=adb(['forward','--list']).split(/\r?\n/).map(line=>line.trim().split(/\s+/)); const expected='localabstract:webview_devtools_remote_'+session.pid; if(!rows.some(row=>row[0]===session.serial&&row[1]==='tcp:'+session.port&&row[2]===expected))throw Error('调试端口未映射到已核对包的当前进程,停止输入'); }; verifyForward(); const version=await(await fetch('http://127.0.0.1:'+session.port+'/json/version',{signal:AbortSignal.timeout(10000)})).json(); if(version['Android-Package']!==session.packageId)throw Error('DevTools实际包身份与真实验收包不一致,停止输入'); const cdp=await connectWebView(session.port); try{verifyForward()}catch(error){cdp.close();throw error} return {cdp,session,adb}; } export async function tapVisibleText(cdp,text,scope='body'){ const selector=await cdp.evaluate(({text,scope})=>{ document.querySelectorAll('[data-real-review-target]').forEach(x=>x.removeAttribute('data-real-review-target')); const matches=[...document.querySelectorAll(scope+' button')].filter(x=>{const r=x.getBoundingClientRect();return x.textContent.trim()===text&&r.width>0&&r.height>0&&r.right>0&&r.bottom>0}); if(matches.length!==1)throw Error('按钮不唯一或不可见:'+text); const el=matches[0];el.scrollIntoView({block:'nearest'});const r=el.getBoundingClientRect(),hit=document.elementFromPoint(r.x+r.width/2,r.y+r.height/2); if(hit!==el&&!el.contains(hit))throw Error('目标被其他界面遮挡:'+text); el.setAttribute('data-real-review-target','current');return '[data-real-review-target="current"]'; },{text,scope}); await cdp.tap(selector); }