Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Page helper: text at no opacity is not what a page opens with

luishron123a966c15358d1
diff --git a/shared/app-frame/heading.test.ts b/shared/app-frame/heading.test.ts
index 8394ef83cb..27acc0a22f 100644
--- a/shared/app-frame/heading.test.ts
+++ b/shared/app-frame/heading.test.ts
@@ -36,6 +36,13 @@ describe("whether a page opens with a heading of its own", () => {
expect(opensWithHeading(open("<header hidden><h1>Hidden</h1></header><p>body</p>"))).toBe(false);
});
+ it("does not count text the page shows at no opacity", () => {
+ expect(opensWithHeading(open("<h1 style=\"opacity:0\">Hidden</h1><p>body</p>"))).toBe(false);
+ expect(opensWithHeading(open("<header class=\"clear\"><h1>Hidden</h1></header><p>body</p>", "<style>.clear{opacity:0}</style>"))).toBe(false);
+ expect(opensWithHeading(open("<p style=\"opacity:0\">Hidden</p><h1>Title</h1>"))).toBe(true);
+ expect(opensWithHeading(open("<h1 style=\"opacity:0.4\">Faint</h1><p>body</p>"))).toBe(true);
+ });
+
it("looks past hidden text that comes before a heading", () => {
expect(opensWithHeading(open("<p style=\"display:none\">Hidden</p><h1>Title</h1>"))).toBe(true);
expect(opensWithHeading(open("<p class=\"gone\">Hidden</p><h1>Title</h1>", "<style>.gone{display:none}</style>"))).toBe(true);
@@ -67,7 +74,7 @@ describe("the page helper's heading report", () => {
Object.defineProperty(page.document, "readyState", { configurable: true, get: () => "complete" });
new Function("window", "document", "MutationObserver", "ResizeObserver", "requestAnimationFrame", APP_HELPER_SOURCE)(
frame, page.document, page.MutationObserver, undefined, (callback: () => void) => setTimeout(callback, 0));
- return { page, reports };
+ return { page, frame, reports };
}
it("says once whether the page opens with a heading", async () => {
@@ -103,6 +110,23 @@ describe("the page helper's heading report", () => {
await expect.poll(() => reports).toEqual([false, true, false]);
});
+ it("looks again when a fade the page runs has ended", async () => {
+ const { page, frame, reports } = boot("<h1>Fading in</h1><p>body</p>");
+ // an animation moves no attribute, so nothing but its end tells the helper
+ const real = frame.getComputedStyle;
+ let clear = true;
+ frame.getComputedStyle = (element) => {
+ const style = real(element);
+ return clear && (element as { tagName?: string }).tagName === "H1" ? { display: "block", visibility: "visible", opacity: "0" } as never : style;
+ };
+ await expect.poll(() => reports).toEqual([false]);
+ clear = false;
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ expect(reports).toEqual([false]);
+ page.document.querySelector("h1")!.dispatchEvent(new page.Event("animationend", { bubbles: true }) as never);
+ await expect.poll(() => reports).toEqual([false, true]);
+ });
+
it("settles on no heading for a page that never shows any text", async () => {
const { reports } = boot("<canvas></canvas>");
await expect.poll(() => reports, { timeout: 3_000 }).toEqual([false]);
diff --git a/shared/app-frame/helper.ts b/shared/app-frame/helper.ts
index 4d758d09fe..ed0159bbb7 100644
--- a/shared/app-frame/helper.ts
+++ b/shared/app-frame/helper.ts
@@ -45,7 +45,7 @@ const HEADING_WAIT_MS = 600;
const HEADING_DRAWING_MS = 8_000;
/** `(window, document) => true | false | null`: whether the first text the page shows sits in a heading near its top;
- * null while it shows no text at all. Text counts when no ancestor is `display: none` or `hidden`, it is not
+ * null while it shows no text at all. Text counts when no ancestor is `display: none`, `hidden` or at no opacity, it is not
* `visibility: hidden`, and, where there is a layout to ask, its element is more than a pixel in size. */
export const APP_OPENS_WITH_HEADING_SOURCE = `function (window, document) {
var body = document.body;
@@ -56,7 +56,7 @@ export const APP_OPENS_WITH_HEADING_SOURCE = `function (window, document) {
var first = function (element) {
if (skipped[element.tagName] || element.hasAttribute("hidden")) return null;
var style = window.getComputedStyle(element);
- if (style.display === "none") return null;
+ if (style.display === "none" || parseFloat(style.opacity) === 0) return null;
var shown = style.visibility !== "hidden" && style.visibility !== "collapse";
for (var child = element.firstChild; child; child = child.nextSibling) {
if ((budget -= 1) < 0) return null;
@@ -233,6 +233,9 @@ const helperSource = (room: boolean) => `(function () {
}
window.addEventListener("load", look);
window.addEventListener("resize", look);
+ // A fade moves no attribute: only its end says the text is there now.
+ document.addEventListener("animationend", look, true);
+ document.addEventListener("transitionend", look, true);
look();
};
// What the person selects, and where it ends, so the host can offer to cite it as chat does. Not in a room.