import WebSocket from 'ws' import fs from 'node:fs' 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 targets = await send('Target.getTargets') const t = targets.targetInfos.find((x) => x.url.includes('dragtest')) const { sessionId } = await send('Target.attachToTarget', { targetId: t.targetId, flatten: true }) const S = (m, p) => send(m, p, sessionId) const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) const ev = async (e) => (await S('Runtime.evaluate', { expression: e, returnByValue: true })).result.value await S('Page.reload') await sleep(2500) const touch = (type, x, y) => S('Input.dispatchTouchEvent', { type, touchPoints: type === 'touchEnd' ? [] : [{ x, y, id: 1 }] }) const r = JSON.parse(await ev(`JSON.stringify(document.querySelector('[data-habit-id="2"] .grip').getBoundingClientRect())`)) const x = r.left + r.width / 2, y = r.top + r.height / 2 await touch('touchStart', x, y) await sleep(60) const t4 = JSON.parse(await ev(`JSON.stringify(document.querySelector('[data-habit-id="4"]').getBoundingClientRect())`)) await touch('touchMove', t4.left + t4.width / 2, t4.top + t4.height / 2) await sleep(150) console.log('ghost bg:', await ev(`getComputedStyle(document.querySelector('.drag-ghost')).backgroundColor`)) console.log('ghost rect:', await ev(`JSON.stringify(document.querySelector('.drag-ghost').getBoundingClientRect())`)) const shot = await S('Page.captureScreenshot', { format: 'png' }) fs.writeFileSync(process.env.USERPROFILE + '\\AppData\\Local\\Temp\\drag-fixed.png', Buffer.from(shot.data, 'base64')) await touch('touchEnd') ws.close()