PrzZiomek2 6a0f11f7e1 add time elapsed between clicking emulator
Summary: Ref T2747

Reviewers: kuba-orlik

Reviewed By: kuba-orlik

Subscribers: kuba-orlik

Maniphest Tasks: T2747

Differential Revision: https://hub.sealcode.org/D1392
2024-03-14 18:23:20 +01:00

113 lines
2.5 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Rentgen android</title>
<style>
main{
display: flex;
}
.log-section{
height: 600px;
width: 300px;
overflow-y: auto;
display: flex;
flex-direction: column;
margin-left: 20px;
}
.screen{
display: inline-block
}
</style>
</head>
<body>
<main>
<img id="screen" src="" class="screen"/>
<p
id="clicks-log"
class="log-section"
></p>
<p
id="traffic-log"
class="log-section"
></p>
</main>
<script>
var screen = document.getElementById("screen");
var clicksLog = document.getElementById("clicks-log");
let lastTouch = new Date().getTime();
const calculateElapsedTime = (last) => {
const currentTouch = new Date().getTime();
const elapsedTime = currentTouch - lastTouch;
const elapsedSec = Math.round(elapsedTime / 1000);
lastTouch = currentTouch;
return elapsedSec;
};
const waitToLog = (clickInfoText) => {
const clickInfo = document.createElement("span");
const waitInfo = document.createElement("span");
waitInfo.textContent = `await wait(${calculateElapsedTime(lastTouch)});`
clicksLog.appendChild(waitInfo);
clickInfo.textContent = clickInfoText;
clicksLog.appendChild(clickInfo);
}
async function displayImage() {
try {
const response = await fetch("screen");
const blob = await response.blob();
screen.src = URL.createObjectURL(blob);
} catch (error) {
console.error("Error fetching image: ", error);
}
}
async function handleTouchEvent(event) {
var phoneX = event.offsetX;
var phoneY = event.offsetY;
if (
phoneX <= 320 &&
phoneX >= 0 &&
phoneY <= 640 &&
phoneY >= 0
) {
waitToLog(`await click(${phoneX}, ${phoneY});`);
await fetch("touch", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `x=${phoneX}&y=${phoneY}`,
});
}
}
async function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function screenshot_loop() {
var before;
while (true) {
before = performance.now();
await displayImage();
while (performance.now() - before < ___screenshotDelayMs___)
await sleep(50);
}
}
screen.addEventListener("click", handleTouchEvent);
screenshot_loop();
</script>
<script src="/trafficLog.js"></script>
</body>
</html>