bibimbap/.atp/work-session/20261014-091658/artifacts/input-supplemental/cdp.mjs

16 lines
4.8 KiB
JavaScript

import fs from 'node:fs';
const tabs=await(await fetch('http://127.0.0.1:18087/json')).json();
const ws=new WebSocket(tabs.find(x=>x.type==='page').webSocketDebuggerUrl);await new Promise(r=>ws.onopen=r);let id=0;const pending=new Map();ws.onmessage=e=>{const m=JSON.parse(e.data);if(m.id){const p=pending.get(m.id);pending.delete(m.id);m.error?p.reject(m.error):p.resolve(m.result)}};
const send=(method,params={})=>new Promise((resolve,reject)=>{pending.set(++id,{resolve,reject});ws.send(JSON.stringify({id,method,params}))});
const ev=async(expression)=>{const r=await send('Runtime.evaluate',{expression,returnByValue:true,awaitPromise:true});if(r.exceptionDetails)throw r.exceptionDetails;return r.result.value};
const action=process.argv[2];
if(action==='init'){await send('Emulation.setDeviceMetricsOverride',{width:390,height:844,deviceScaleFactor:1,mobile:true});await send('Emulation.setTouchEmulationEnabled',{enabled:true,maxTouchPoints:1});await send('Page.navigate',{url:'http://127.0.0.1:18086/'});}
if(action==='eval') console.log(JSON.stringify(await ev(process.argv[3]),null,2));
if(action==='shot'){const r=await send('Page.captureScreenshot',{format:'png'});fs.writeFileSync(process.argv[3],Buffer.from(r.data,'base64'));}
if(action==='touch'){const x=+process.argv[3],y=+process.argv[4];await send('Input.dispatchTouchEvent',{type:'touchStart',touchPoints:[{x,y,id:1,radiusX:1,radiusY:1,force:1}]});await new Promise(r=>setTimeout(r,100));await send('Input.dispatchTouchEvent',{type:'touchEnd',touchPoints:[]});}
if(action==='resize'){await send('Emulation.setDeviceMetricsOverride',{width:+process.argv[3],height:+process.argv[4],deviceScaleFactor:1,mobile:true});}
if(action==='expanded'){await ev('document.querySelector("[data-player-expand]").click()');await send('Emulation.setDeviceMetricsOverride',{width:844,height:390,deviceScaleFactor:1,mobile:false});await new Promise(r=>setTimeout(r,1000));console.log(JSON.stringify(await ev('(()=>{let f=document.querySelector("iframe"),c=f.contentDocument.querySelector("canvas");return {view:[innerWidth,innerHeight],frame:f.getBoundingClientRect().toJSON(),canvas:c.getBoundingClientRect().toJSON(),buffer:[c.width,c.height],sameFrame:f===window.__originalFrame,sameUnity:f.contentWindow.unityInstance===window.__originalUnity}})()'),null,2));let r=await send('Page.captureScreenshot',{format:'png'});fs.writeFileSync(process.argv[3],Buffer.from(r.data,'base64'));}
if(action==='expandedtap'){await send('Emulation.setDeviceMetricsOverride',{width:844,height:390,deviceScaleFactor:1,mobile:false});await new Promise(r=>setTimeout(r,400));await ev('document.querySelector("iframe").contentWindow.__events=[]');await send('Input.dispatchTouchEvent',{type:'touchStart',touchPoints:[{x:466,y:345,id:2,radiusX:1,radiusY:1,force:1}]});await new Promise(r=>setTimeout(r,100));await send('Input.dispatchTouchEvent',{type:'touchEnd',touchPoints:[]});await new Promise(r=>setTimeout(r,400));console.log(JSON.stringify(await ev('document.querySelector("iframe").contentWindow.__events'),null,2));let r=await send('Page.captureScreenshot',{format:'png'});fs.writeFileSync(process.argv[3],Buffer.from(r.data,'base64'));}
if(action==='portrait'){await ev('document.querySelector("[data-player-expand]").click()');await send('Emulation.setDeviceMetricsOverride',{width:390,height:844,deviceScaleFactor:1,mobile:false});await new Promise(r=>setTimeout(r,500));let geo=await ev('(()=>{let f=document.querySelector("iframe"),c=f.contentDocument.querySelector("canvas");return {view:[innerWidth,innerHeight],frame:f.getBoundingClientRect().toJSON(),canvas:c.getBoundingClientRect().toJSON(),buffer:[c.width,c.height],sameFrame:f===window.__originalFrame,sameUnity:f.contentWindow.unityInstance===window.__originalUnity}})()');let coords=(portion)=>({x:geo.frame.x+geo.canvas.x+geo.canvas.width*portion,y:geo.frame.y+geo.canvas.y+geo.canvas.height*.96});let tap=async(p)=>{await send('Input.dispatchTouchEvent',{type:'touchStart',touchPoints:[{...p,id:3,radiusX:1,radiusY:1,force:1}]});await new Promise(r=>setTimeout(r,100));await send('Input.dispatchTouchEvent',{type:'touchEnd',touchPoints:[]});await new Promise(r=>setTimeout(r,300));};await tap(coords(.1));let r=await send('Page.captureScreenshot',{format:'png'});fs.writeFileSync(process.argv[3]+'/portrait-home-before.png',Buffer.from(r.data,'base64'));await ev('document.querySelector("iframe").contentWindow.__events=[]');await tap(coords(.3));r=await send('Page.captureScreenshot',{format:'png'});fs.writeFileSync(process.argv[3]+'/portrait-shop-after.png',Buffer.from(r.data,'base64'));let events=await ev('document.querySelector("iframe").contentWindow.__events');let result={geo,coordinates:coords(.3),events};fs.writeFileSync(process.argv[3]+'/portrait-input.json',JSON.stringify(result,null,2));console.log(JSON.stringify(result,null,2));}
ws.close();