Skip to content

Commit bddb3a2

Browse files
authored
Merge pull request #53 from softpudding/feat/optimize-extension-perf
Feat/optimize extension perf
2 parents a1b5e19 + a74d949 commit bddb3a2

File tree

11 files changed

+1270
-354
lines changed

11 files changed

+1270
-354
lines changed

eval/evaluate_browser_agent.py

Lines changed: 407 additions & 8 deletions
Large diffs are not rendered by default.

eval/evaluation_report.json

Lines changed: 370 additions & 225 deletions
Large diffs are not rendered by default.

extension/src/__tests__/background-cleanup-regression.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ describe('Background cleanup regressions', () => {
4848
);
4949
});
5050

51+
test('final highlight attempt full-scans even when readiness is still not_ready', () => {
52+
expect(backgroundSource).toContain(
53+
'fullPageScanOnNotReady: attempt === maxHighlightAttempts',
54+
);
55+
expect(backgroundSource).toContain('const maxHighlightAttempts = 3;');
56+
});
57+
5158
test('navigation defaults prime the page with a raw screenshot before highlight', () => {
5259
expect(backgroundSource).toContain('async function runRawScreenshotPrime(');
5360
expect(backgroundSource).toContain('primeWithRawScreenshot: true');

extension/src/__tests__/highlight-detection.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,26 @@ describe('highlight-detection helpers', () => {
116116
expect(script).toContain('evaluateLayoutReadiness');
117117
});
118118

119+
test('buildHighlightDetectionScript can force full scans on not-ready pages', () => {
120+
const script = buildHighlightDetectionScript({
121+
elementType: 'any',
122+
fullPageScanOnNotReady: true,
123+
});
124+
125+
expect(script).toContain('"fullPageScanOnNotReady":true');
126+
expect(script).toContain('config.fullPageScanOnNotReady === true');
127+
expect(script).toContain('override=full_scan');
128+
});
129+
119130
test('buildHighlightDetectionScript uses readiness snapshot instead of wait loop', () => {
120131
const script = buildHighlightDetectionScript({ elementType: 'any' });
121132

122133
expect(script).toContain('function evaluateReadinessSnapshot');
134+
expect(script).toContain('function getCandidateElementsForScan');
135+
expect(script).toContain("layoutStability.state !== 'not_ready'");
136+
expect(script).toContain('config.fullPageScanOnNotReady === true');
137+
expect(script).toContain('const NOT_READY_SCAN_LIMIT = 500;');
138+
expect(script).toContain("trace('scan:capped'");
123139
expect(script).toContain('readiness:snapshot');
124140
expect(script).not.toContain('function waitForLayoutStability');
125141
});
@@ -143,6 +159,51 @@ describe('highlight-detection helpers', () => {
143159
);
144160
});
145161

162+
test('buildHighlightDetectionScript excludes visually hidden screen-reader nodes', () => {
163+
const script = buildHighlightDetectionScript({ elementType: 'any' });
164+
const start = script.indexOf('function isElementVisibleForDetection');
165+
const end = script.indexOf(
166+
'function isElementInViewportForDetection',
167+
start,
168+
);
169+
const visibilitySource = script.slice(start, end);
170+
171+
expect(script).toContain('const VISUALLY_HIDDEN_TOKEN_REGEX');
172+
expect(script).toContain('sr-only');
173+
expect(script).toContain('screen-reader');
174+
expect(script).toContain('visually-hidden');
175+
expect(script).toContain('function isVisuallyHiddenForDetection');
176+
expect(visibilitySource).toContain('isVisuallyHiddenForDetection(el)');
177+
});
178+
179+
test('buildHighlightDetectionScript excludes truncation-only scrollable false positives', () => {
180+
const script = buildHighlightDetectionScript({ elementType: 'scrollable' });
181+
const truncationStart = script.indexOf(
182+
'function isLikelyTextTruncationContainer',
183+
);
184+
const truncationEnd = script.indexOf(
185+
'function isScrollableCandidate',
186+
truncationStart,
187+
);
188+
const truncationSource = script.slice(truncationStart, truncationEnd);
189+
const scrollableStart = script.indexOf('function isScrollableCandidate');
190+
const scrollableEnd = script.indexOf(
191+
'function isHoverableCandidate',
192+
scrollableStart,
193+
);
194+
const scrollableSource = script.slice(scrollableStart, scrollableEnd);
195+
196+
expect(truncationSource).toContain("style.textOverflow === 'ellipsis'");
197+
expect(truncationSource).toContain("style.whiteSpace === 'nowrap'");
198+
expect(truncationSource).toContain('countVisibleElementChildren(el, 2)');
199+
expect(scrollableSource).toContain('isLikelyTextTruncationContainer(el)');
200+
expect(scrollableSource).toContain('isSemanticControlElement(el)');
201+
expect(scrollableSource).toContain(
202+
"overflowX.includes('hidden') || overflowX.includes('clip')",
203+
);
204+
expect(scrollableSource).toContain('hasHorizontalSwipeLayout(el)');
205+
});
206+
146207
test("buildHighlightDetectionScript keeps 'any' candidate selection across all element types", () => {
147208
const script = buildHighlightDetectionScript({ elementType: 'any' });
148209
const start = script.indexOf('function resolveElementCandidate');

extension/src/background/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ async function captureHighlightedPageState(
296296
await tabManager.ensureTabManaged(tabId, conversationId);
297297
tabManager.updateTabActivity(tabId, conversationId);
298298

299-
const detectionScript = buildHighlightDetectionScript({
300-
elementType,
301-
});
302-
303299
await runHighlightPreconditionWarmup({
304300
tabId,
305301
conversationId,
@@ -316,6 +312,10 @@ async function captureHighlightedPageState(
316312

317313
for (let attempt = 1; attempt <= maxHighlightAttempts; attempt++) {
318314
console.log(`🔁 [${logLabel}] Attempt ${attempt}/${maxHighlightAttempts}`);
315+
const detectionScript = buildHighlightDetectionScript({
316+
elementType,
317+
fullPageScanOnNotReady: attempt === maxHighlightAttempts,
318+
});
319319

320320
const detectionResult = await javascript.executeJavaScript(
321321
tabId,

0 commit comments

Comments
 (0)