import WebSocket from 'ws' const C = JSON.parse((await (await fetch('http://127.0.0.1:9223/json/version')).text())) const ws = new WebSocket(C.webSocketDebuggerUrl, { perMessageDeflate: false }) let id = 0; const pending = new Map() const send = (m, p = {}, s) => new Promise((r) => { const i = ++id; pending.set(i, r); ws.send(JSON.stringify({ id: i, method: m, params: p, sessionId: s })) }) ws.on('message', (d) => { const m = JSON.parse(d); if (m.id && pending.has(m.id)) { pending.get(m.id)(m.result); pending.delete(m.id) } }) await new Promise((r) => ws.on('open', r)) const { targetId } = await send('Target.createTarget', { url: 'about:blank' }) const { sessionId } = await send('Target.attachToTarget', { targetId, flatten: true }) const S = (m, p) => send(m, p, sessionId) await S('Runtime.enable'); await S('Page.enable') await S('Emulation.setDeviceMetricsOverride', { width: 400, height: 800, deviceScaleFactor: 2, mobile: true }) await S('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 5 }) await S('Page.navigate', { url: 'http://127.0.0.1:5199/dragtest.html' }) const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) const ev = async (e) => (await S('Runtime.evaluate', { expression: e, returnByValue: true })).result.value for (let i = 0; i < 30; i++) { await sleep(1000); if (await ev(`!!document.querySelector('[data-task-id="3"]')`)) break } await sleep(1000) // 等延迟数据加载完 const touch = (type, x, y) => S('Input.dispatchTouchEvent', { type, touchPoints: type === 'touchEnd' ? [] : [{ x, y, id: 1 }] }) // 用例: 按住任务3的把手 → 水平拖 +200px (带 +30 纵向漂移, 复现真机抖动) → 松手 const r = JSON.parse(await ev(`JSON.stringify(document.querySelector('[data-task-id="3"] .grip').getBoundingClientRect())`)) let x = r.left + r.width / 2, y = r.top + r.height / 2 console.log('grip rect', JSON.stringify(r)) await touch('touchStart', x, y) await sleep(60) for (const [dx, dy] of [[25, 4], [60, 10], [110, 18], [160, 26], [200, 30]]) { await touch('touchMove', x + dx, y + dy) await sleep(50) } console.log('row class after moves:', await ev(`document.querySelector('[data-task-id="3"]').className`)) console.log('meta:', await ev(`document.querySelector('[data-task-id="3"] .task-meta').textContent`)) await touch('touchEnd') await sleep(300) console.log('events:', await ev('window.__ev.join(" | ")')) ws.close()