// Attachments in WebKit, as on an iPhone (390x844, touch), in a page that // carries production's Content-Security-Policy: a picked photo's chip // preview loads, paste attaches, the sent thumbnail loads, and the pill's // focus edge is measured at rest and focused. Prints one JSON object. import { createRequire } from "node:module"; const require = createRequire(import.meta.url); const { webkit } = require(process.env.EQUIPO_PLAYWRIGHT_CORE); const cfg = JSON.parse(process.env.EQUIPO_ATTACH); const loaded = sel => { const i = document.querySelector(sel); return !!i && i.complete && i.naturalWidth > 0; }; const out = {}; const browser = await webkit.launch(); try { for (const scheme of ["dark", "light"]) { const ctx = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, hasTouch: true, isMobile: true, colorScheme: scheme }); await ctx.addCookies([{ name: cfg.cookie.name, value: cfg.cookie.value, url: cfg.base }]); const page = await ctx.newPage(); const blocked = []; page.on("console", m => { if (/Refused to load|img-src/i.test(m.text())) blocked.push(m.text()); }); await page.goto(cfg.base + "/conversation"); await page.waitForFunction(() => !!document.querySelector("#composer[data-enhanced]") && !!window.EquipoAttach); const edge = () => getComputedStyle(document.querySelector("[equipo-pill]")).borderTopColor; // Past the first paint's transitions (the edge and the arrow's fade). await page.waitForTimeout(500); const res = { rest: await page.evaluate(edge) }; if (cfg.art) await page.screenshot({ path: `${cfg.art}/composer-focus-rest-${scheme}-390.png` }); await page.focus("#chat-body"); await page.waitForTimeout(500); res.focused = await page.evaluate(edge); res.outline = await page.evaluate(() => getComputedStyle(document.querySelector("#chat-body")).outlineStyle); if (cfg.art) await page.screenshot({ path: `${cfg.art}/composer-focus-focused-${scheme}-390.png` }); await page.setInputFiles('[data-picker="image"]', cfg.photo); await page.waitForFunction(() => document.querySelector('[equipo-chip][data-state="ready"]'), null, { timeout: 15000 }); res.chip = await page.evaluate(() => { const i = document.querySelector('[equipo-chip][data-state="ready"] [equipo-chip-thumb] img'); return { src: i ? i.src.slice(0, 15) : "", width: i ? i.naturalWidth : 0, complete: !!i && i.complete }; }); if (cfg.art) await page.screenshot({ path: `${cfg.art}/composer-after-chip-webkit-${scheme}-390.png` }); // Paste, as a long-press paste delivers it: an image item. res.paste = await page.evaluate(async (b64) => { const bytes = Uint8Array.from(atob(b64), c => c.charCodeAt(0)); const d = new DataTransfer(); d.items.add(new File([bytes], "pasted.png", { type: "image/png" })); const e = new ClipboardEvent("paste", { clipboardData: d, bubbles: true, cancelable: true }); document.querySelector("#chat-body").dispatchEvent(e); return { supported: !!e.clipboardData && e.clipboardData.items.length > 0, taken: e.defaultPrevented }; }, cfg.small); if (res.paste.taken) await page.waitForFunction(() => document.querySelectorAll('[equipo-chip][data-state="ready"]').length === 2, null, { timeout: 15000 }); await page.tap("[equipo-send]"); await page.waitForFunction(() => { const own = [...document.querySelectorAll('[equipo-message="own"]')].pop(); const i = own && own.querySelector("[equipo-file-thumb] img"); return !!i && i.complete && i.naturalWidth > 0 && own.dataset.message; }, null, { timeout: 15000 }); res.sent = await page.evaluate(() => { const s = [...document.querySelectorAll('[equipo-message="own"]')].pop().querySelector("[equipo-file-thumb] img").src; return s.startsWith("data:") ? s.slice(0, 15) : new URL(s).pathname; }); await page.reload(); await page.waitForFunction(() => { const own = [...document.querySelectorAll('[equipo-message="own"]')].pop(); const i = own && own.querySelector("[equipo-file-thumb] img"); return !!i && i.complete && i.naturalWidth > 0; }, null, { timeout: 15000 }); res.server = await page.evaluate(() => new URL([...document.querySelectorAll('[equipo-message="own"]')].pop().querySelector("[equipo-file-thumb] img").src).pathname); if (cfg.art) await page.screenshot({ path: `${cfg.art}/composer-after-feed-webkit-${scheme}-390.png` }); res.blocked = blocked; out[scheme] = res; await ctx.close(); } } finally { await browser.close(); } process.stdout.write(JSON.stringify(out));