shithub: scrax

ref: 86a1bffad7063d1f51dfe44c2126bbdf8b7fe5c8
dir: /nonsense/test.js/

View raw version
let timeScale = 1

await drag(query(".library .block", /move steps/), query(".program"), {x0: 8, x1: 64, y1: 64})
await drag(query(".library .block", /forever/), query1(".program .block"), {x0: 8, x1: 0})
await drag(query(".library .block", /when clicked/), query(".program .block", /forever/), {x0: 8, x1: 0, y1: 32})
await drag(query(".library .block", /when clicked/), query(".program"), {x0: 8, x1: 64, y1: 64})
await drag(query(".library .block", /set x to/), query(".program .block .line", /move steps/), {x0: 8, x1: 0, y1: 64})
await drag(query(".library .block", /if/), query(".program .block", /move steps/), {x0: 8, x1: 0, y1: 64})
await drag(query(".library .block", /stop/), query(".program .block", /when clicked/), {x0: 8, x1: 0, y1: 64})
await write(query(".program .block .line", /stop/).querySelector("select"), "all")
await write(query(".library .block", /wait seconds/).querySelector("input"), "5")
await drag(query(".library .block", /wait seconds/), query(".program .block", /when clicked/), {x0: 8, x1: 0, y1: 64})
await write(query(".program .block .line", /set x to/).querySelector("input"), "-400")
await drag(query(".library .block", />/), query(".program .block .line", /if/).querySelector(".slot"))
await write(query(".program .block .line", />/).querySelectorAll("input")[1], "400")
await drag(query(".library .block", /x position/), query(".program .block .line", />/).querySelector("input"))
document.querySelector(".start-button").click()

function makePointerEvent(type, clientX, clientY, options = {})
{
	return new PointerEvent(type, {bubbles: true, clientX, clientY, pointerType: "mouse", buttons: 1, ...options})
}

function dispatchPointerEvent(type, clientX, clientY)
{
	let element = document.elementFromPoint(clientX, clientY) ?? document.body
	element.dispatchEvent(makePointerEvent(type, clientX, clientY))
}

async function drag(element0, element1, {x0, y0, x1, y1, t = 1000, d = 500} = {})
{
	t /= timeScale
	d /= timeScale
	
	element1.scrollIntoView({block: "center", inline: "center", behavior: "instant"})
	element0.scrollIntoView({block: "center", inline: "center", behavior: "instant"})
	
	let rect0 = element0.getBoundingClientRect()
	let rect1 = element1.getBoundingClientRect()
	
	x0 = rect0.x + (x0 ?? rect0.width / 2)
	y0 = rect0.y + (y0 ?? rect0.height / 2)
	x1 = rect1.x + (x1 ?? rect1.width / 2)
	y1 = rect1.y + (y1 ?? rect1.height / 2)
	
	dispatchPointerEvent("pointerdown", x0, y0)
	
	let t0 = performance.now()
	for (;;) {
		await new Promise(requestAnimationFrame)
		let t1 = performance.now() - t0
		if (t1 >= t) break
		let s0 = t1 / t
		let s = (1 - Math.cos(s0 * Math.PI)) / 2
		let x = x0 + (x1 - x0) * s
		let y = y0 + (y1 - y0) * s
		dispatchPointerEvent("pointermove", x, y)
	}
	
	dispatchPointerEvent("pointermove", x1, y1)
	await new Promise(requestAnimationFrame)
	dispatchPointerEvent("pointerup", x1, y1)
	await new Promise(resolve => setTimeout(resolve, d))
}

function query(selector, regex = /.?/, n = 0)
{
	let elements = []
	for (let element of document.querySelectorAll(selector)) {
		if (regex.test(element.innerText.normalize().replace(/\s+/u, " ").trim())) elements.push(element)
	}
	return elements.at(n)
}

function query1(selector, n = -1)
{
	return query(selector, undefined, n)
}

async function write(input, value, {d = 500} = {})
{
	d /= timeScale
	input.scrollIntoView({block: "center", inline: "center", behavior: "instant"})
	input.value = value
	input.dispatchEvent(new InputEvent("input", {bubbles: true}))
	input.dispatchEvent(new InputEvent("change", {bubbles: true}))
	input.dispatchEvent(new InputEvent("blur"))
	await new Promise(resolve => setTimeout(resolve, d))
}