mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
feat(vom): vom can distinguish important canvas area
This commit is contained in:
1 parent
b9fd6e46af
commit
ed91f41291
23 files changed
+855
-10
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import type { Rect } from "@browser-skill/vom";
|
||||
import type { RenderedSurface, RenderedSurfaceGroup } from "./types";
|
||||
|
||||
const STACK_OVERLAP_RATIO = 0.9;
|
||||
|
||||
function area(rect: Rect): number {
|
||||
return Math.max(0, rect.w) * Math.max(0, rect.h);
|
||||
}
|
||||
|
||||
function intersectionArea(a: Rect, b: Rect): number {
|
||||
const width = Math.max(0, Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x));
|
||||
const height = Math.max(0, Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y));
|
||||
return width * height;
|
||||
}
|
||||
|
||||
function sameVisualStack(a: RenderedSurface, b: RenderedSurface): boolean {
|
||||
if (a.frameId !== b.frameId) return false;
|
||||
const smallerArea = Math.min(area(a.visibleRect), area(b.visibleRect));
|
||||
return (
|
||||
smallerArea > 0 &&
|
||||
intersectionArea(a.visibleRect, b.visibleRect) / smallerArea >= STACK_OVERLAP_RATIO
|
||||
);
|
||||
}
|
||||
|
||||
function unionRect(rects: Rect[]): Rect {
|
||||
const x = Math.min(...rects.map((rect) => rect.x));
|
||||
const y = Math.min(...rects.map((rect) => rect.y));
|
||||
const right = Math.max(...rects.map((rect) => rect.x + rect.w));
|
||||
const bottom = Math.max(...rects.map((rect) => rect.y + rect.h));
|
||||
return { x, y, w: right - x, h: bottom - y };
|
||||
}
|
||||
|
||||
function toGroup(members: RenderedSurface[]): RenderedSurfaceGroup {
|
||||
const representative = [...members].sort(
|
||||
(a, b) => area(b.visibleRect) - area(a.visibleRect) || b.paintOrder - a.paintOrder,
|
||||
)[0];
|
||||
return {
|
||||
frameId: representative.frameId,
|
||||
members,
|
||||
representative,
|
||||
rect: unionRect(members.map((surface) => surface.rect)),
|
||||
visibleRect: unionRect(members.map((surface) => surface.visibleRect)),
|
||||
label: members.map((surface) => surface.label).find((label) => label !== undefined),
|
||||
existenceConfidence: members.some((surface) => surface.existenceConfidence === "high")
|
||||
? "high"
|
||||
: "low",
|
||||
};
|
||||
}
|
||||
|
||||
export function clusterRenderedSurfaces(surfaces: RenderedSurface[]): RenderedSurfaceGroup[] {
|
||||
const groups: RenderedSurface[][] = [];
|
||||
for (const surface of surfaces) {
|
||||
const matching = groups.find((group) =>
|
||||
group.some((member) => sameVisualStack(member, surface)),
|
||||
);
|
||||
if (matching) matching.push(surface);
|
||||
else groups.push([surface]);
|
||||
}
|
||||
return groups
|
||||
.map(toGroup)
|
||||
.sort(
|
||||
(a, b) =>
|
||||
a.frameId.localeCompare(b.frameId) ||
|
||||
a.visibleRect.y - b.visibleRect.y ||
|
||||
a.visibleRect.x - b.visibleRect.x,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import type { Rect, Viewport } from "@browser-skill/vom";
|
||||
import type { CapturedNode } from "../capture";
|
||||
import type { CapturedFrameDocument } from "../frame-capture";
|
||||
import type { SemanticAxNode } from "../semantic-graph";
|
||||
import type { RenderedSurface } from "./types";
|
||||
|
||||
const MIN_PROMINENT_EDGE = 32;
|
||||
const MIN_PROMINENT_AREA = 4_096;
|
||||
const NON_CONTENT_ROLES = new Set(["", "generic", "none", "presentation"]);
|
||||
|
||||
function clean(value: string | undefined): string | undefined {
|
||||
const normalized = value?.replace(/\s+/g, " ").trim();
|
||||
return normalized || undefined;
|
||||
}
|
||||
|
||||
function intersectViewport(rect: Rect, viewport: Viewport): Rect | null {
|
||||
const x = Math.max(0, rect.x);
|
||||
const y = Math.max(0, rect.y);
|
||||
const right = Math.min(viewport.width, rect.x + rect.w);
|
||||
const bottom = Math.min(viewport.height, rect.y + rect.h);
|
||||
if (right <= x || bottom <= y) return null;
|
||||
return { x, y, w: right - x, h: bottom - y };
|
||||
}
|
||||
|
||||
function hiddenByDomPolicy(node: CapturedNode, nodesByBackend: Map<number, CapturedNode>): boolean {
|
||||
let current: CapturedNode | undefined = node;
|
||||
let guard = 0;
|
||||
while (current && guard <= nodesByBackend.size) {
|
||||
const attrs = current.attrs;
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(attrs, "hidden") ||
|
||||
Object.prototype.hasOwnProperty.call(attrs, "inert") ||
|
||||
(attrs["aria-hidden"] ?? "").toLowerCase() === "true"
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
current =
|
||||
current.parentBackendNodeId === null
|
||||
? undefined
|
||||
: nodesByBackend.get(current.parentBackendNodeId);
|
||||
guard += 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function hasVerifiedNativeMirror(canvas: CapturedNode, axNodes: SemanticAxNode[]): boolean {
|
||||
const owner = axNodes.find(
|
||||
(node) => node.backendDOMNodeId === canvas.backendNodeId && node.ignored !== true,
|
||||
);
|
||||
if (!owner?.childIds?.length) return false;
|
||||
const byId = new Map(axNodes.map((node) => [node.nodeId, node]));
|
||||
const queue = [...owner.childIds];
|
||||
const seen = new Set<string>();
|
||||
while (queue.length > 0) {
|
||||
const id = queue.shift() as string;
|
||||
if (seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
const node = byId.get(id);
|
||||
if (!node || node.ignored === true) continue;
|
||||
const role = String(node.role?.value ?? "").toLowerCase();
|
||||
if (!NON_CONTENT_ROLES.has(role)) return true;
|
||||
queue.push(...(node.childIds ?? []));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isProminent(node: CapturedNode, visibleRect: Rect, label: string | undefined): boolean {
|
||||
const tabindex = Number.parseInt(node.attrs.tabindex ?? "", 10);
|
||||
const interactionSignal =
|
||||
node.cursor === "pointer" || (Number.isFinite(tabindex) && tabindex >= 0);
|
||||
return (
|
||||
label !== undefined ||
|
||||
interactionSignal ||
|
||||
(visibleRect.w >= MIN_PROMINENT_EDGE &&
|
||||
visibleRect.h >= MIN_PROMINENT_EDGE &&
|
||||
visibleRect.w * visibleRect.h >= MIN_PROMINENT_AREA)
|
||||
);
|
||||
}
|
||||
|
||||
export function discoverRenderedSurfaces(
|
||||
documents: CapturedFrameDocument<SemanticAxNode>[],
|
||||
viewport: Viewport,
|
||||
): RenderedSurface[] {
|
||||
const surfaces: RenderedSurface[] = [];
|
||||
for (const document of documents) {
|
||||
const nodesByBackend = new Map(document.domNodes.map((node) => [node.backendNodeId, node]));
|
||||
for (const node of document.domNodes) {
|
||||
if (node.tag.toLowerCase() !== "canvas" || node.rendered !== true || !node.rect) continue;
|
||||
if (hiddenByDomPolicy(node, nodesByBackend)) continue;
|
||||
if (hasVerifiedNativeMirror(node, document.axNodes)) continue;
|
||||
const visibleRect = intersectViewport(node.rect, viewport);
|
||||
if (!visibleRect) continue;
|
||||
const label = clean(node.attrs["aria-label"] ?? node.attrs.title);
|
||||
surfaces.push({
|
||||
renderingKind: "canvas",
|
||||
frameId: document.frameId,
|
||||
backendNodeId: node.backendNodeId,
|
||||
parentBackendNodeId: node.parentBackendNodeId,
|
||||
rect: node.rect,
|
||||
...(node.localRect ? { localRect: node.localRect } : {}),
|
||||
visibleRect,
|
||||
paintOrder: node.paintOrder,
|
||||
...(label ? { label } : {}),
|
||||
existenceConfidence: isProminent(node, visibleRect, label) ? "high" : "low",
|
||||
});
|
||||
}
|
||||
}
|
||||
return surfaces;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export { clusterRenderedSurfaces } from "./cluster";
|
||||
export { discoverRenderedSurfaces } from "./discovery";
|
||||
export { projectRenderedSurfaces } from "./project";
|
||||
export type { RenderedSurface, RenderedSurfaceGroup } from "./types";
|
||||
@@ -0,0 +1,87 @@
|
||||
import type { VomScene, VomVisualSurface, VomVisualSurfaceSummary } from "@browser-skill/vom";
|
||||
import type { CapturedFrameDocument } from "../frame-capture";
|
||||
import type { SemanticAxNode } from "../semantic-graph";
|
||||
import type { RenderedSurfaceGroup } from "./types";
|
||||
|
||||
function frameBackendKey(frameId: string, backendNodeId: number): string {
|
||||
return `${frameId}\u0000${backendNodeId}`;
|
||||
}
|
||||
|
||||
function nearestSceneParent(
|
||||
scene: VomScene,
|
||||
documents: CapturedFrameDocument<SemanticAxNode>[],
|
||||
group: RenderedSurfaceGroup,
|
||||
): number | null {
|
||||
const sceneByBackend = new Map(
|
||||
scene.nodes.flatMap((node) =>
|
||||
node.backendNodeId === undefined
|
||||
? []
|
||||
: [[frameBackendKey(node.frameId ?? "", node.backendNodeId), node.id] as const],
|
||||
),
|
||||
);
|
||||
const document = documents.find((candidate) => candidate.frameId === group.frameId);
|
||||
const domByBackend = new Map(document?.domNodes.map((node) => [node.backendNodeId, node]) ?? []);
|
||||
let parentBackendNodeId = group.representative.parentBackendNodeId;
|
||||
let guard = 0;
|
||||
while (parentBackendNodeId !== null && guard <= domByBackend.size) {
|
||||
const sceneId = sceneByBackend.get(frameBackendKey(group.frameId, parentBackendNodeId));
|
||||
if (sceneId !== undefined) return sceneId;
|
||||
parentBackendNodeId = domByBackend.get(parentBackendNodeId)?.parentBackendNodeId ?? null;
|
||||
guard += 1;
|
||||
}
|
||||
|
||||
if (document?.parentFrameId && document.ownerBackendNodeId !== undefined) {
|
||||
const owner = sceneByBackend.get(
|
||||
frameBackendKey(document.parentFrameId, document.ownerBackendNodeId),
|
||||
);
|
||||
if (owner !== undefined) return owner;
|
||||
}
|
||||
|
||||
return (
|
||||
scene.nodes.find(
|
||||
(node) =>
|
||||
node.frameId === group.frameId &&
|
||||
["rootwebarea", "webarea"].includes(node.role?.toLowerCase() ?? ""),
|
||||
)?.id ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function projectRenderedSurfaces(
|
||||
scene: VomScene,
|
||||
documents: CapturedFrameDocument<SemanticAxNode>[],
|
||||
groups: RenderedSurfaceGroup[],
|
||||
): VomScene {
|
||||
const visualSurfaces: VomVisualSurface[] = [];
|
||||
const summaryCounts = new Map<
|
||||
string,
|
||||
{ parentId: number | null; frameId: string; count: number }
|
||||
>();
|
||||
|
||||
for (const group of groups) {
|
||||
const parentId = nearestSceneParent(scene, documents, group);
|
||||
if (group.existenceConfidence === "low") {
|
||||
const key = `${group.frameId}\u0000${parentId ?? "root"}`;
|
||||
const current = summaryCounts.get(key) ?? { parentId, frameId: group.frameId, count: 0 };
|
||||
current.count += group.members.length;
|
||||
summaryCounts.set(key, current);
|
||||
continue;
|
||||
}
|
||||
visualSurfaces.push({
|
||||
parentId,
|
||||
backendNodeId: group.representative.backendNodeId,
|
||||
frameId: group.frameId,
|
||||
renderingKind: "canvas",
|
||||
rect: group.visibleRect,
|
||||
...(group.representative.localRect ? { localRect: group.representative.localRect } : {}),
|
||||
...(group.label ? { label: group.label } : {}),
|
||||
memberCount: group.members.length,
|
||||
});
|
||||
}
|
||||
|
||||
const visualSurfaceSummaries: VomVisualSurfaceSummary[] = [...summaryCounts.values()];
|
||||
return {
|
||||
...scene,
|
||||
...(visualSurfaces.length > 0 ? { visualSurfaces } : {}),
|
||||
...(visualSurfaceSummaries.length > 0 ? { visualSurfaceSummaries } : {}),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { Rect } from "@browser-skill/vom";
|
||||
|
||||
export type SurfaceExistenceConfidence = "high" | "low";
|
||||
|
||||
export interface RenderedSurface {
|
||||
renderingKind: "canvas";
|
||||
frameId: string;
|
||||
backendNodeId: number;
|
||||
parentBackendNodeId: number | null;
|
||||
rect: Rect;
|
||||
localRect?: Rect;
|
||||
visibleRect: Rect;
|
||||
paintOrder: number;
|
||||
label?: string;
|
||||
existenceConfidence: SurfaceExistenceConfidence;
|
||||
}
|
||||
|
||||
export interface RenderedSurfaceGroup {
|
||||
frameId: string;
|
||||
members: RenderedSurface[];
|
||||
representative: RenderedSurface;
|
||||
rect: Rect;
|
||||
visibleRect: Rect;
|
||||
label?: string;
|
||||
existenceConfidence: SurfaceExistenceConfidence;
|
||||
}
|
||||
Reference in new issue
Block a user