From d949460d39b57489fbb79f1a578c34c86535a6e9 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:29:32 -0700 Subject: [PATCH 01/12] fix(extension): avoid overlapping page controls --- src/content.ts | 47 ++++++++++++++++++++++++- src/uiPosition.ts | 54 +++++++++++++++++++++++++---- test/content.test.ts | 76 +++++++++++++++++++++++++++++++++++++++++ test/uiPosition.test.ts | 28 +++++++++++++++ 4 files changed, 197 insertions(+), 8 deletions(-) diff --git a/src/content.ts b/src/content.ts index b78f42e..d8d087b 100644 --- a/src/content.ts +++ b/src/content.ts @@ -27,6 +27,18 @@ const MAX_CONTEXT_CHARS = 30_000 const BUTTON_SIZE = 24 const SMALL_BUTTON_SIZE = 20 const SMALL_EDITOR_HEIGHT = 34 +const CLICKABLE_AVOID_SELECTOR = [ + 'button', + 'a[href]', + '[role="button"]', + '[role="menuitem"]', + '[role="option"]', + 'input[type="button"]', + 'input[type="submit"]', + 'input[type="reset"]', + 'summary' +].join(',') +const CLICKABLE_AVOID_PROXIMITY = 80 const BRAND_GLYPH_SVG = `` @@ -546,12 +558,45 @@ function positionButton(editor: HTMLElement): void { const position = buttonPositionNearEditor({ editorRect: rect, buttonSize: { width: size, height: size }, - viewportSize: { width: window.innerWidth, height: window.innerHeight } + viewportSize: { width: window.innerWidth, height: window.innerHeight }, + avoidRects: clickableAvoidRectsNearEditor(editor, rect) }) button.style.top = `${position.top}px` button.style.left = `${position.left}px` } +function clickableAvoidRectsNearEditor( + editor: HTMLElement, + editorRect: DOMRect +): DOMRect[] { + const proximityRect = expandedRect(editorRect, CLICKABLE_AVOID_PROXIMITY) + + return Array.from( + document.querySelectorAll(CLICKABLE_AVOID_SELECTOR) + ) + .filter((element) => element !== editor) + .filter((element) => !element.contains(editor)) + .filter((element) => !isInjectedUITarget(element)) + .map((element) => element.getBoundingClientRect()) + .filter((rect) => rect.width > 0 && rect.height > 0) + .filter((rect) => rectsOverlap(rect, proximityRect)) +} + +function expandedRect(rect: DOMRect, amount: number): DOMRect { + return new DOMRect( + rect.left - amount, + rect.top - amount, + rect.width + amount * 2, + rect.height + amount * 2 + ) +} + +function rectsOverlap(a: DOMRect, b: DOMRect): boolean { + return ( + a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top + ) +} + function buttonSizeForEditor(rect: DOMRect): number { return rect.height <= SMALL_EDITOR_HEIGHT ? SMALL_BUTTON_SIZE : BUTTON_SIZE } diff --git a/src/uiPosition.ts b/src/uiPosition.ts index ff480f8..ccee3bd 100644 --- a/src/uiPosition.ts +++ b/src/uiPosition.ts @@ -39,19 +39,59 @@ export function buttonPositionNearEditor(options: { editorRect: RectLike buttonSize: Size viewportSize: Size + avoidRects?: RectLike[] }): { top: number; left: number } { - const { editorRect, buttonSize, viewportSize } = options + const { editorRect, buttonSize, viewportSize, avoidRects = [] } = options const fitsInsideEditor = editorRect.height >= buttonSize.height + BUTTON_GAP * 2 - const unclampedTop = fitsInsideEditor + const lowerRightTop = fitsInsideEditor ? editorRect.bottom - buttonSize.height - BUTTON_GAP : editorRect.top + editorRect.height / 2 - buttonSize.height / 2 - const unclampedLeft = editorRect.right - buttonSize.width - BUTTON_GAP + const rightAlignedLeft = editorRect.right - buttonSize.width - BUTTON_GAP const maxTop = viewportSize.height - buttonSize.height - VIEWPORT_GUTTER const maxLeft = viewportSize.width - buttonSize.width - VIEWPORT_GUTTER + const candidates = [ + { top: lowerRightTop, left: rightAlignedLeft }, + { top: editorRect.top + BUTTON_GAP, left: rightAlignedLeft }, + { top: editorRect.bottom + BUTTON_GAP, left: rightAlignedLeft }, + { + top: editorRect.top - buttonSize.height - BUTTON_GAP, + left: rightAlignedLeft + }, + { top: lowerRightTop, left: editorRect.right + BUTTON_GAP }, + { + top: lowerRightTop, + left: editorRect.left - buttonSize.width - BUTTON_GAP + } + ].map((candidate) => ({ + top: Math.max(VIEWPORT_GUTTER, Math.min(maxTop, candidate.top)), + left: Math.max(VIEWPORT_GUTTER, Math.min(maxLeft, candidate.left)) + })) - return { - top: Math.max(VIEWPORT_GUTTER, Math.min(maxTop, unclampedTop)), - left: Math.max(VIEWPORT_GUTTER, Math.min(maxLeft, unclampedLeft)) - } + const nonOverlappingCandidate = candidates.find( + (candidate) => + !rectOverlapsAny( + { + top: candidate.top, + left: candidate.left, + right: candidate.left + buttonSize.width, + bottom: candidate.top + buttonSize.height, + width: buttonSize.width, + height: buttonSize.height + }, + avoidRects + ) + ) + + return nonOverlappingCandidate ?? candidates[0] +} + +function rectOverlapsAny(rect: RectLike, avoidRects: RectLike[]): boolean { + return avoidRects.some((avoidRect) => rectsOverlap(rect, avoidRect)) +} + +function rectsOverlap(a: RectLike, b: RectLike): boolean { + return ( + a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top + ) } diff --git a/test/content.test.ts b/test/content.test.ts index 215c2fc..043a33c 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -85,6 +85,82 @@ describe('content script button visibility', () => { expect(button.innerHTML).toContain('font-size="48"') }) + test('moves the Simple Words button away from overlapping page buttons', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue({ + top: 160, + left: 260, + right: 310, + bottom: 210, + width: 50, + height: 50, + x: 260, + y: 160, + toJSON: () => null + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + + test('does not avoid broad tabindex containers near the editor', () => { + document.body.innerHTML = + '
Focusable wrapper
' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const focusableWrapper = document.querySelector('div') as HTMLDivElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + vi.spyOn(focusableWrapper, 'getBoundingClientRect').mockReturnValue({ + top: 160, + left: 260, + right: 310, + bottom: 210, + width: 50, + height: 50, + x: 260, + y: 160, + toJSON: () => null + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('168px') + expect(button.style.left).toBe('268px') + }) + test('uses the editable root when input events come from a contenteditable child', () => { document.body.innerHTML = '
rough reply
' diff --git a/test/uiPosition.test.ts b/test/uiPosition.test.ts index 2167690..a7f952a 100644 --- a/test/uiPosition.test.ts +++ b/test/uiPosition.test.ts @@ -78,4 +78,32 @@ describe('buttonPositionNearEditor', () => { expect(position.top).toBe(40) expect(position.left).toBe(192) }) + + test('uses the upper-right of the editor when the lower-right overlaps an avoided control', () => { + const position = buttonPositionNearEditor({ + editorRect: { + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100 + }, + buttonSize: { width: 24, height: 24 }, + viewportSize: { width: 500, height: 500 }, + avoidRects: [ + { + top: 160, + left: 260, + right: 310, + bottom: 210, + width: 50, + height: 50 + } + ] + }) + + expect(position.top).toBe(108) + expect(position.left).toBe(268) + }) }) From f039fb46b1ba65d6037d946f8debcd4e3eda7e87 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:32:45 -0700 Subject: [PATCH 02/12] no-mistakes(review): Ignore invisible overlap controls --- src/content.ts | 4 ++++ test/content.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/content.ts b/src/content.ts index d8d087b..a123f93 100644 --- a/src/content.ts +++ b/src/content.ts @@ -577,6 +577,10 @@ function clickableAvoidRectsNearEditor( .filter((element) => element !== editor) .filter((element) => !element.contains(editor)) .filter((element) => !isInjectedUITarget(element)) + .filter((element) => { + const visibility = getComputedStyle(element).visibility + return visibility !== 'hidden' && visibility !== 'collapse' + }) .map((element) => element.getBoundingClientRect()) .filter((rect) => rect.width > 0 && rect.height > 0) .filter((rect) => rectsOverlap(rect, proximityRect)) diff --git a/test/content.test.ts b/test/content.test.ts index 043a33c..b893260 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -123,6 +123,44 @@ describe('content script button visibility', () => { expect(button.style.left).toBe('268px') }) + test('does not avoid invisible page buttons near the editor', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue({ + top: 160, + left: 260, + right: 310, + bottom: 210, + width: 50, + height: 50, + x: 260, + y: 160, + toJSON: () => null + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('168px') + expect(button.style.left).toBe('268px') + }) + test('does not avoid broad tabindex containers near the editor', () => { document.body.innerHTML = '
Focusable wrapper
' From 9eb3dd2d82ded45dcc124e214cb0ce66556cc2a3 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:37:01 -0700 Subject: [PATCH 03/12] no-mistakes(review): Constrain button collision checks --- src/content.ts | 62 +++++++++++++++++++++++++++++++++++++++++++- src/uiPosition.ts | 52 +++++++++++++++++++++---------------- test/content.test.ts | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 24 deletions(-) diff --git a/src/content.ts b/src/content.ts index a123f93..fc84c35 100644 --- a/src/content.ts +++ b/src/content.ts @@ -16,9 +16,11 @@ import { type SimpleWordsSettings } from './settings' import { + buttonCandidateRectsNearEditor, buttonPositionNearEditor, panelPositionAboveButton } from './uiPosition' +import type { RectLike } from './uiPosition' const BUTTON_ID = 'simplewords-button' const PANEL_ID = 'simplewords-panel' @@ -569,6 +571,49 @@ function clickableAvoidRectsNearEditor( editor: HTMLElement, editorRect: DOMRect ): DOMRect[] { + const size = buttonSizeForEditor(editorRect) + const buttonSize = { width: size, height: size } + const candidateRects = buttonCandidateRectsNearEditor({ + editorRect, + buttonSize, + viewportSize: { width: window.innerWidth, height: window.innerHeight } + }) + const elementsFromPoint = document.elementsFromPoint?.bind(document) + + if (elementsFromPoint) { + const elements = new Set() + + for (const candidateRect of candidateRects) { + for (const element of elementsFromCandidateRect( + candidateRect, + elementsFromPoint + )) { + const clickableElement = element.closest( + CLICKABLE_AVOID_SELECTOR + ) + if (clickableElement) { + elements.add(clickableElement) + } + } + } + + return Array.from(elements) + .filter((element) => element !== editor) + .filter((element) => !element.contains(editor)) + .filter((element) => !isInjectedUITarget(element)) + .filter((element) => { + const visibility = getComputedStyle(element).visibility + return visibility !== 'hidden' && visibility !== 'collapse' + }) + .map((element) => element.getBoundingClientRect()) + .filter((rect) => rect.width > 0 && rect.height > 0) + .filter((rect) => + candidateRects.some((candidateRect) => + rectsOverlap(rect, candidateRect) + ) + ) + } + const proximityRect = expandedRect(editorRect, CLICKABLE_AVOID_PROXIMITY) return Array.from( @@ -586,6 +631,21 @@ function clickableAvoidRectsNearEditor( .filter((rect) => rectsOverlap(rect, proximityRect)) } +function elementsFromCandidateRect( + rect: RectLike, + elementsFromPoint: (x: number, y: number) => Element[] +): Element[] { + const right = rect.right - 1 + const bottom = rect.bottom - 1 + return [ + ...elementsFromPoint(rect.left, rect.top), + ...elementsFromPoint(right, rect.top), + ...elementsFromPoint(rect.left, bottom), + ...elementsFromPoint(right, bottom), + ...elementsFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2) + ] +} + function expandedRect(rect: DOMRect, amount: number): DOMRect { return new DOMRect( rect.left - amount, @@ -595,7 +655,7 @@ function expandedRect(rect: DOMRect, amount: number): DOMRect { ) } -function rectsOverlap(a: DOMRect, b: DOMRect): boolean { +function rectsOverlap(a: RectLike, b: RectLike): boolean { return ( a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top ) diff --git a/src/uiPosition.ts b/src/uiPosition.ts index ccee3bd..f2496e1 100644 --- a/src/uiPosition.ts +++ b/src/uiPosition.ts @@ -41,7 +41,22 @@ export function buttonPositionNearEditor(options: { viewportSize: Size avoidRects?: RectLike[] }): { top: number; left: number } { - const { editorRect, buttonSize, viewportSize, avoidRects = [] } = options + const { avoidRects = [] } = options + const candidates = buttonCandidateRectsNearEditor(options) + + const nonOverlappingCandidate = candidates.find( + (candidate) => !rectOverlapsAny(candidate, avoidRects) + ) + + return nonOverlappingCandidate ?? candidates[0] +} + +export function buttonCandidateRectsNearEditor(options: { + editorRect: RectLike + buttonSize: Size + viewportSize: Size +}): RectLike[] { + const { editorRect, buttonSize, viewportSize } = options const fitsInsideEditor = editorRect.height >= buttonSize.height + BUTTON_GAP * 2 const lowerRightTop = fitsInsideEditor @@ -50,7 +65,7 @@ export function buttonPositionNearEditor(options: { const rightAlignedLeft = editorRect.right - buttonSize.width - BUTTON_GAP const maxTop = viewportSize.height - buttonSize.height - VIEWPORT_GUTTER const maxLeft = viewportSize.width - buttonSize.width - VIEWPORT_GUTTER - const candidates = [ + return [ { top: lowerRightTop, left: rightAlignedLeft }, { top: editorRect.top + BUTTON_GAP, left: rightAlignedLeft }, { top: editorRect.bottom + BUTTON_GAP, left: rightAlignedLeft }, @@ -63,27 +78,18 @@ export function buttonPositionNearEditor(options: { top: lowerRightTop, left: editorRect.left - buttonSize.width - BUTTON_GAP } - ].map((candidate) => ({ - top: Math.max(VIEWPORT_GUTTER, Math.min(maxTop, candidate.top)), - left: Math.max(VIEWPORT_GUTTER, Math.min(maxLeft, candidate.left)) - })) - - const nonOverlappingCandidate = candidates.find( - (candidate) => - !rectOverlapsAny( - { - top: candidate.top, - left: candidate.left, - right: candidate.left + buttonSize.width, - bottom: candidate.top + buttonSize.height, - width: buttonSize.width, - height: buttonSize.height - }, - avoidRects - ) - ) - - return nonOverlappingCandidate ?? candidates[0] + ].map((candidate) => { + const top = Math.max(VIEWPORT_GUTTER, Math.min(maxTop, candidate.top)) + const left = Math.max(VIEWPORT_GUTTER, Math.min(maxLeft, candidate.left)) + return { + top, + left, + right: left + buttonSize.width, + bottom: top + buttonSize.height, + width: buttonSize.width, + height: buttonSize.height + } + }) } function rectOverlapsAny(rect: RectLike, avoidRects: RectLike[]): boolean { diff --git a/test/content.test.ts b/test/content.test.ts index b893260..cc4bd9a 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -8,6 +8,7 @@ describe('content script button visibility', () => { document.getElementById('simplewords-button')?.remove() document.getElementById('simplewords-panel')?.remove() vi.restoreAllMocks() + Reflect.deleteProperty(document, 'elementsFromPoint') Reflect.deleteProperty(globalThis, 'chrome') resetExtensionContextForTests() }) @@ -161,6 +162,55 @@ describe('content script button visibility', () => { expect(button.style.left).toBe('268px') }) + test('does not measure distant page buttons while positioning', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const nearButton = document.querySelector( + '[data-near]' + ) as HTMLButtonElement + const farButton = document.querySelector('[data-far]') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + const nearRect = { + top: 160, + left: 260, + right: 310, + bottom: 210, + width: 50, + height: 50, + x: 260, + y: 160, + toJSON: () => null + } + vi.spyOn(nearButton, 'getBoundingClientRect').mockReturnValue(nearRect) + const farRectSpy = vi.spyOn(farButton, 'getBoundingClientRect') + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + value: () => [] + }) + vi.spyOn(document, 'elementsFromPoint').mockReturnValue([nearButton]) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + expect(farRectSpy).not.toHaveBeenCalled() + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + test('does not avoid broad tabindex containers near the editor', () => { document.body.innerHTML = '
Focusable wrapper
' From 35a1810fe9be459ab110589d1ff678297bdd5ebb Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:39:50 -0700 Subject: [PATCH 04/12] no-mistakes(review): Improve candidate overlap sampling --- src/content.ts | 23 ++++++++++++++------- test/content.test.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/content.ts b/src/content.ts index fc84c35..422d3b9 100644 --- a/src/content.ts +++ b/src/content.ts @@ -635,15 +635,24 @@ function elementsFromCandidateRect( rect: RectLike, elementsFromPoint: (x: number, y: number) => Element[] ): Element[] { + const elements: Element[] = [] + const sampleInterval = 1 const right = rect.right - 1 const bottom = rect.bottom - 1 - return [ - ...elementsFromPoint(rect.left, rect.top), - ...elementsFromPoint(right, rect.top), - ...elementsFromPoint(rect.left, bottom), - ...elementsFromPoint(right, bottom), - ...elementsFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2) - ] + + for (let y = rect.top; y < rect.bottom; y += sampleInterval) { + for (let x = rect.left; x < rect.right; x += sampleInterval) { + elements.push(...elementsFromPoint(x, y)) + } + elements.push(...elementsFromPoint(right, y)) + } + + for (let x = rect.left; x < rect.right; x += sampleInterval) { + elements.push(...elementsFromPoint(x, bottom)) + } + elements.push(...elementsFromPoint(right, bottom)) + + return elements } function expandedRect(rect: DOMRect, amount: number): DOMRect { diff --git a/test/content.test.ts b/test/content.test.ts index cc4bd9a..892d441 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -211,6 +211,55 @@ describe('content script button visibility', () => { expect(button.style.left).toBe('268px') }) + test('avoids page buttons that partially overlap a candidate between sampled points', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + const sendRect = { + top: 170, + left: 282, + right: 302, + bottom: 178, + width: 20, + height: 8, + x: 282, + y: 170, + toJSON: () => null + } + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect) + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + value: (x: number, y: number) => + x >= sendRect.left && + x < sendRect.right && + y >= sendRect.top && + y < sendRect.bottom + ? [sendButton] + : [] + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + test('does not avoid broad tabindex containers near the editor', () => { document.body.innerHTML = '
Focusable wrapper
' From 9aeb6b5e018dd9b43c602e296b40d2b761a34c00 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:43:56 -0700 Subject: [PATCH 05/12] no-mistakes(review): Bound button overlap hit testing --- src/content.ts | 2 +- test/content.test.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/content.ts b/src/content.ts index 422d3b9..922feb2 100644 --- a/src/content.ts +++ b/src/content.ts @@ -636,7 +636,7 @@ function elementsFromCandidateRect( elementsFromPoint: (x: number, y: number) => Element[] ): Element[] { const elements: Element[] = [] - const sampleInterval = 1 + const sampleInterval = 8 const right = rect.right - 1 const bottom = rect.bottom - 1 diff --git a/test/content.test.ts b/test/content.test.ts index 892d441..5bc74f9 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -260,6 +260,58 @@ describe('content script button visibility', () => { expect(button.style.left).toBe('268px') }) + test('bounds overlap hit testing while positioning the button', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + const sendRect = { + top: 170, + left: 282, + right: 302, + bottom: 178, + width: 20, + height: 8, + x: 282, + y: 170, + toJSON: () => null + } + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect) + const elementsFromPoint = vi.fn((x: number, y: number) => + x >= sendRect.left && + x < sendRect.right && + y >= sendRect.top && + y < sendRect.bottom + ? [sendButton] + : [] + ) + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + value: elementsFromPoint + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + expect(elementsFromPoint).toHaveBeenCalledTimes(96) + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + test('does not avoid broad tabindex containers near the editor', () => { document.body.innerHTML = '
Focusable wrapper
' From dc00d2f0b940d85acbe956d42e6370e6394e764a Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:48:26 -0700 Subject: [PATCH 06/12] no-mistakes(review): Tighten button overlap sampling --- src/content.ts | 2 +- test/content.test.ts | 51 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/content.ts b/src/content.ts index 922feb2..b709395 100644 --- a/src/content.ts +++ b/src/content.ts @@ -636,7 +636,7 @@ function elementsFromCandidateRect( elementsFromPoint: (x: number, y: number) => Element[] ): Element[] { const elements: Element[] = [] - const sampleInterval = 8 + const sampleInterval = 2 const right = rect.right - 1 const bottom = rect.bottom - 1 diff --git a/test/content.test.ts b/test/content.test.ts index 5bc74f9..205002a 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -304,7 +304,56 @@ describe('content script button visibility', () => { editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) - expect(elementsFromPoint).toHaveBeenCalledTimes(96) + expect(elementsFromPoint).toHaveBeenCalledTimes(1014) + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + + test('avoids small page buttons that overlap between sampled rows and columns', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + const sendRect = { + top: 170, + left: 278, + right: 282, + bottom: 174, + width: 4, + height: 4, + x: 278, + y: 170, + toJSON: () => null + } + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect) + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + value: (x: number, y: number) => + x >= sendRect.left && + x < sendRect.right && + y >= sendRect.top && + y < sendRect.bottom + ? [sendButton] + : [] + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + const button = document.getElementById( 'simplewords-button' ) as HTMLButtonElement From 11b9e0b968c606d153c3f350745bd3989631cd99 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:51:46 -0700 Subject: [PATCH 07/12] no-mistakes(review): Bound button overlap hit testing --- src/content.ts | 17 ++++++++--------- test/content.test.ts | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/content.ts b/src/content.ts index b709395..67f9d88 100644 --- a/src/content.ts +++ b/src/content.ts @@ -636,21 +636,20 @@ function elementsFromCandidateRect( elementsFromPoint: (x: number, y: number) => Element[] ): Element[] { const elements: Element[] = [] - const sampleInterval = 2 const right = rect.right - 1 const bottom = rect.bottom - 1 + const centerX = Math.floor(rect.left + rect.width / 2) - for (let y = rect.top; y < rect.bottom; y += sampleInterval) { - for (let x = rect.left; x < rect.right; x += sampleInterval) { - elements.push(...elementsFromPoint(x, y)) - } - elements.push(...elementsFromPoint(right, y)) + for (let x = rect.left; x < rect.right; x += 1) { + elements.push(...elementsFromPoint(x, rect.top)) + elements.push(...elementsFromPoint(x, bottom)) } - for (let x = rect.left; x < rect.right; x += sampleInterval) { - elements.push(...elementsFromPoint(x, bottom)) + for (let y = rect.top + 1; y < bottom; y += 1) { + elements.push(...elementsFromPoint(rect.left, y)) + elements.push(...elementsFromPoint(right, y)) + elements.push(...elementsFromPoint(centerX, y)) } - elements.push(...elementsFromPoint(right, bottom)) return elements } diff --git a/test/content.test.ts b/test/content.test.ts index 205002a..3270fd1 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -304,7 +304,7 @@ describe('content script button visibility', () => { editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) - expect(elementsFromPoint).toHaveBeenCalledTimes(1014) + expect(elementsFromPoint).toHaveBeenCalledTimes(684) const button = document.getElementById( 'simplewords-button' ) as HTMLButtonElement From afd7224bc8b850e7972735ea7d5f7e672f674f49 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:54:47 -0700 Subject: [PATCH 08/12] no-mistakes(review): Cover interior button collisions --- src/content.ts | 19 +++++++++++++++++ test/content.test.ts | 51 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/content.ts b/src/content.ts index 67f9d88..9e13985 100644 --- a/src/content.ts +++ b/src/content.ts @@ -41,6 +41,7 @@ const CLICKABLE_AVOID_SELECTOR = [ 'summary' ].join(',') const CLICKABLE_AVOID_PROXIMITY = 80 +const CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP = 4 const BRAND_GLYPH_SVG = `` @@ -651,6 +652,24 @@ function elementsFromCandidateRect( elements.push(...elementsFromPoint(centerX, y)) } + for ( + let x = rect.left + CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP; + x < right; + x += CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP + ) { + if (x === centerX) { + continue + } + + for ( + let y = rect.top + CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP; + y < bottom; + y += CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP + ) { + elements.push(...elementsFromPoint(x, y)) + } + } + return elements } diff --git a/test/content.test.ts b/test/content.test.ts index 3270fd1..e484453 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -304,7 +304,7 @@ describe('content script button visibility', () => { editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) - expect(elementsFromPoint).toHaveBeenCalledTimes(684) + expect(elementsFromPoint).toHaveBeenCalledTimes(804) const button = document.getElementById( 'simplewords-button' ) as HTMLButtonElement @@ -361,6 +361,55 @@ describe('content script button visibility', () => { expect(button.style.left).toBe('268px') }) + test('avoids small page buttons fully inside a candidate away from sampled lines', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + const sendRect = { + top: 176, + left: 276, + right: 280, + bottom: 180, + width: 4, + height: 4, + x: 276, + y: 176, + toJSON: () => null + } + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect) + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + value: (x: number, y: number) => + x >= sendRect.left && + x < sendRect.right && + y >= sendRect.top && + y < sendRect.bottom + ? [sendButton] + : [] + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + test('does not avoid broad tabindex containers near the editor', () => { document.body.innerHTML = '
Focusable wrapper
' From 40c8aa31aa49101596936ded7c12ce7f28dba441 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 12:59:46 -0700 Subject: [PATCH 09/12] no-mistakes(review): Bound collision probes per candidate --- src/content.ts | 65 ++++++++++++++++++++++++-------------------- test/content.test.ts | 4 +-- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/content.ts b/src/content.ts index 9e13985..57d6aef 100644 --- a/src/content.ts +++ b/src/content.ts @@ -17,7 +17,6 @@ import { } from './settings' import { buttonCandidateRectsNearEditor, - buttonPositionNearEditor, panelPositionAboveButton } from './uiPosition' import type { RectLike } from './uiPosition' @@ -558,43 +557,53 @@ function positionButton(editor: HTMLElement): void { const rect = editor.getBoundingClientRect() const size = buttonSizeForEditor(rect) button.dataset.size = size === SMALL_BUTTON_SIZE ? 'small' : 'normal' - const position = buttonPositionNearEditor({ + const position = collisionAwareButtonPositionNearEditor({ editorRect: rect, buttonSize: { width: size, height: size }, viewportSize: { width: window.innerWidth, height: window.innerHeight }, - avoidRects: clickableAvoidRectsNearEditor(editor, rect) + editor }) button.style.top = `${position.top}px` button.style.left = `${position.left}px` } -function clickableAvoidRectsNearEditor( - editor: HTMLElement, +function collisionAwareButtonPositionNearEditor(options: { + editor: HTMLElement editorRect: DOMRect + buttonSize: { width: number; height: number } + viewportSize: { width: number; height: number } +}): RectLike { + const { editor, ...positionOptions } = options + const candidateRects = buttonCandidateRectsNearEditor(positionOptions) + + for (const candidateRect of candidateRects) { + const avoidRects = clickableAvoidRectsForCandidate(editor, candidateRect) + if (!avoidRects.some((rect) => rectsOverlap(rect, candidateRect))) { + return candidateRect + } + } + + return candidateRects[0] +} + +function clickableAvoidRectsForCandidate( + editor: HTMLElement, + candidateRect: RectLike ): DOMRect[] { - const size = buttonSizeForEditor(editorRect) - const buttonSize = { width: size, height: size } - const candidateRects = buttonCandidateRectsNearEditor({ - editorRect, - buttonSize, - viewportSize: { width: window.innerWidth, height: window.innerHeight } - }) const elementsFromPoint = document.elementsFromPoint?.bind(document) if (elementsFromPoint) { const elements = new Set() - for (const candidateRect of candidateRects) { - for (const element of elementsFromCandidateRect( - candidateRect, - elementsFromPoint - )) { - const clickableElement = element.closest( - CLICKABLE_AVOID_SELECTOR - ) - if (clickableElement) { - elements.add(clickableElement) - } + for (const element of elementsFromCandidateRect( + candidateRect, + elementsFromPoint + )) { + const clickableElement = element.closest( + CLICKABLE_AVOID_SELECTOR + ) + if (clickableElement) { + elements.add(clickableElement) } } @@ -608,14 +617,10 @@ function clickableAvoidRectsNearEditor( }) .map((element) => element.getBoundingClientRect()) .filter((rect) => rect.width > 0 && rect.height > 0) - .filter((rect) => - candidateRects.some((candidateRect) => - rectsOverlap(rect, candidateRect) - ) - ) + .filter((rect) => rectsOverlap(rect, candidateRect)) } - const proximityRect = expandedRect(editorRect, CLICKABLE_AVOID_PROXIMITY) + const proximityRect = expandedRect(candidateRect, CLICKABLE_AVOID_PROXIMITY) return Array.from( document.querySelectorAll(CLICKABLE_AVOID_SELECTOR) @@ -673,7 +678,7 @@ function elementsFromCandidateRect( return elements } -function expandedRect(rect: DOMRect, amount: number): DOMRect { +function expandedRect(rect: RectLike, amount: number): DOMRect { return new DOMRect( rect.left - amount, rect.top - amount, diff --git a/test/content.test.ts b/test/content.test.ts index e484453..2e9f44c 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -260,7 +260,7 @@ describe('content script button visibility', () => { expect(button.style.left).toBe('268px') }) - test('bounds overlap hit testing while positioning the button', () => { + test('stops overlap hit testing after finding a clear button position', () => { document.body.innerHTML = '' @@ -304,7 +304,7 @@ describe('content script button visibility', () => { editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) - expect(elementsFromPoint).toHaveBeenCalledTimes(804) + expect(elementsFromPoint).toHaveBeenCalledTimes(268) const button = document.getElementById( 'simplewords-button' ) as HTMLButtonElement From ca82254979acd2dc09205b5d2fff2934854db968 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 13:05:23 -0700 Subject: [PATCH 10/12] no-mistakes(review): Short-circuit button collision probes --- src/content.ts | 66 +++++++++++++++++++++++++++----------------- test/content.test.ts | 54 +++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/src/content.ts b/src/content.ts index 57d6aef..6ecd13a 100644 --- a/src/content.ts +++ b/src/content.ts @@ -593,7 +593,7 @@ function clickableAvoidRectsForCandidate( const elementsFromPoint = document.elementsFromPoint?.bind(document) if (elementsFromPoint) { - const elements = new Set() + const seenElements = new Set() for (const element of elementsFromCandidateRect( candidateRect, @@ -602,22 +602,41 @@ function clickableAvoidRectsForCandidate( const clickableElement = element.closest( CLICKABLE_AVOID_SELECTOR ) - if (clickableElement) { - elements.add(clickableElement) + + if (!clickableElement || seenElements.has(clickableElement)) { + continue + } + + seenElements.add(clickableElement) + + if (clickableElement === editor) { + continue + } + + if (clickableElement.contains(editor)) { + continue + } + + if (isInjectedUITarget(clickableElement)) { + continue + } + + const visibility = getComputedStyle(clickableElement).visibility + if (visibility === 'hidden' || visibility === 'collapse') { + continue + } + + const rect = clickableElement.getBoundingClientRect() + if ( + rect.width > 0 && + rect.height > 0 && + rectsOverlap(rect, candidateRect) + ) { + return [rect] } } - return Array.from(elements) - .filter((element) => element !== editor) - .filter((element) => !element.contains(editor)) - .filter((element) => !isInjectedUITarget(element)) - .filter((element) => { - const visibility = getComputedStyle(element).visibility - return visibility !== 'hidden' && visibility !== 'collapse' - }) - .map((element) => element.getBoundingClientRect()) - .filter((rect) => rect.width > 0 && rect.height > 0) - .filter((rect) => rectsOverlap(rect, candidateRect)) + return [] } const proximityRect = expandedRect(candidateRect, CLICKABLE_AVOID_PROXIMITY) @@ -637,24 +656,23 @@ function clickableAvoidRectsForCandidate( .filter((rect) => rectsOverlap(rect, proximityRect)) } -function elementsFromCandidateRect( +function* elementsFromCandidateRect( rect: RectLike, elementsFromPoint: (x: number, y: number) => Element[] -): Element[] { - const elements: Element[] = [] +): Iterable { const right = rect.right - 1 const bottom = rect.bottom - 1 const centerX = Math.floor(rect.left + rect.width / 2) for (let x = rect.left; x < rect.right; x += 1) { - elements.push(...elementsFromPoint(x, rect.top)) - elements.push(...elementsFromPoint(x, bottom)) + yield* elementsFromPoint(x, rect.top) + yield* elementsFromPoint(x, bottom) } for (let y = rect.top + 1; y < bottom; y += 1) { - elements.push(...elementsFromPoint(rect.left, y)) - elements.push(...elementsFromPoint(right, y)) - elements.push(...elementsFromPoint(centerX, y)) + yield* elementsFromPoint(rect.left, y) + yield* elementsFromPoint(right, y) + yield* elementsFromPoint(centerX, y) } for ( @@ -671,11 +689,9 @@ function elementsFromCandidateRect( y < bottom; y += CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP ) { - elements.push(...elementsFromPoint(x, y)) + yield* elementsFromPoint(x, y) } } - - return elements } function expandedRect(rect: RectLike, amount: number): DOMRect { diff --git a/test/content.test.ts b/test/content.test.ts index 2e9f44c..30897d3 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -304,7 +304,59 @@ describe('content script button visibility', () => { editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) - expect(elementsFromPoint).toHaveBeenCalledTimes(268) + expect(elementsFromPoint).toHaveBeenCalledTimes(187) + const button = document.getElementById( + 'simplewords-button' + ) as HTMLButtonElement + expect(button.style.top).toBe('108px') + expect(button.style.left).toBe('268px') + }) + + test('short-circuits overlap hit testing for blocked button positions', () => { + document.body.innerHTML = + '' + + const editor = document.querySelector('textarea') as HTMLTextAreaElement + const sendButton = document.querySelector('button') as HTMLButtonElement + vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({ + top: 100, + left: 100, + right: 300, + bottom: 200, + width: 200, + height: 100, + x: 100, + y: 100, + toJSON: () => null + }) + const sendRect = { + top: 168, + left: 268, + right: 292, + bottom: 192, + width: 24, + height: 24, + x: 268, + y: 168, + toJSON: () => null + } + vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect) + const elementsFromPoint = vi.fn((x: number, y: number) => + x >= sendRect.left && + x < sendRect.right && + y >= sendRect.top && + y < sendRect.bottom + ? [sendButton] + : [] + ) + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + value: elementsFromPoint + }) + + editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true })) + + expect(elementsFromPoint).toHaveBeenCalledTimes(135) const button = document.getElementById( 'simplewords-button' ) as HTMLButtonElement From 226b48768f53c934c41d2da17368a8405c6c112d Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 13:10:51 -0700 Subject: [PATCH 11/12] no-mistakes(document): document collision-aware button placement --- .agents/skills/simple-words-design/README.md | 2 +- .../ui_kits/extension/README.md | 4 +- e2e/button-overlap.spec.ts | 221 ++++++++++++++++++ 3 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 e2e/button-overlap.spec.ts diff --git a/.agents/skills/simple-words-design/README.md b/.agents/skills/simple-words-design/README.md index 9bcd88a..aa95e1e 100644 --- a/.agents/skills/simple-words-design/README.md +++ b/.agents/skills/simple-words-design/README.md @@ -180,7 +180,7 @@ The options page is fully opaque. ### Layout rules -- **Floating button** is `position: fixed`, `z-index: 2147483647` (max), and positioned near the active editable field's lower-right edge. +- **Floating button** is `position: fixed`, `z-index: 2147483647` (max), starts near the active editable field's lower-right edge, and can move to avoid button-like page controls. This is non-negotiable — it must clear every host page, including ones with their own floating UI. Use a compact 24px `sw` glyph button for normal editors and a 20px version for short inputs. The injected button and panel are visible only while the current domain is enabled and the active editor is connected, focused, and visible, including empty editors. diff --git a/.agents/skills/simple-words-design/ui_kits/extension/README.md b/.agents/skills/simple-words-design/ui_kits/extension/README.md index 66abc24..5dde15e 100644 --- a/.agents/skills/simple-words-design/ui_kits/extension/README.md +++ b/.agents/skills/simple-words-design/ui_kits/extension/README.md @@ -6,7 +6,7 @@ Check the production extension files before treating this kit as pixel-faithful. ## Surfaces 1. **Floating button** (`FloatingButton.jsx`) - a prototype pill button with the older sparkles icon and label. - Production now uses a compact `sw` glyph button near the lower-right edge of focused editable fields, so check the extension files before treating this component as pixel-faithful. + Production now uses a compact `sw` glyph button that starts near the lower-right edge of focused editable fields and can move to avoid button-like page controls, so check the extension files before treating this component as pixel-faithful. 2. **Refine panel** (`RefinePanel.jsx`) - the floating panel that shows loading, setup or provider error guidance, rewritten draft states, and Open settings/Replace/Dismiss actions. 3. **Options page** (`OptionsPage.jsx`) - the settings page prototype with provider config, enabled domains, name field, and advanced system prompt. 4. **Host page** (`HostPage.jsx`) - a faked Gmail-like compose window, just so the floating button has something to attach to in `index.html`. @@ -33,7 +33,7 @@ Preserve that visibility contract in production-facing work. ## Demo `index.html` boots a fake Gmail compose window with a draft typed in. -The prototype still positions its legacy pill below the textarea; production positions the compact `sw` button near the editor's lower-right edge. +The prototype still positions its legacy pill below the textarea; production starts the compact `sw` button near the editor's lower-right edge and can move it to avoid button-like page controls. Clicking the button opens the refine panel with a 1-second simulated "Refining..." state, then a rewritten draft. Click "Replace draft" to swap the textarea content; click "Dismiss" to close. There's also a tab switch at the top to jump to the **Options page** view. diff --git a/e2e/button-overlap.spec.ts b/e2e/button-overlap.spec.ts new file mode 100644 index 0000000..5bf97ff --- /dev/null +++ b/e2e/button-overlap.spec.ts @@ -0,0 +1,221 @@ +import { test, expect, chromium, type BrowserContext } from '@playwright/test' +import { createServer, type Server } from 'node:http' +import { mkdir, writeFile } from 'node:fs/promises' +import { mkdtemp } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import path from 'node:path' + +const evidenceDir = path.join(process.cwd(), 'test-results', 'button-overlap') + +test('places the Simple Words button away from page buttons but not broad tabindex containers', async () => { + const app = await startFixtureServer() + const extensionPath = path.join(process.cwd(), 'extension') + const userDataDir = await mkdtemp(path.join(tmpdir(), 'simplewords-overlap-e2e-')) + let context: BrowserContext | undefined + + try { + context = await chromium.launchPersistentContext(userDataDir, { + headless: false, + args: [ + `--disable-extensions-except=${extensionPath}`, + `--load-extension=${extensionPath}` + ] + }) + + await configureSiteEnablementOnly(context) + await mkdir(evidenceDir, { recursive: true }) + + const page = await context.newPage() + await page.setViewportSize({ width: 640, height: 480 }) + + await page.goto(`${app.baseURL}/button-overlap`) + await page.locator('#editor').click() + await expect(page.locator('#simplewords-button')).toBeVisible() + const buttonControlEvidence = await page.evaluate(measurePlacement) + expect(buttonControlEvidence.overlapsAvoidTarget).toBe(false) + expect(buttonControlEvidence.simpleWordsButton.top).toBeLessThan( + buttonControlEvidence.editor.bottom - 30 + ) + await page.screenshot({ + path: path.join(evidenceDir, 'button-control-avoided.png'), + fullPage: true + }) + + await page.goto(`${app.baseURL}/tabindex-nearby`) + await page.locator('#editor').click() + await expect(page.locator('#simplewords-button')).toBeVisible() + const tabindexEvidence = await page.evaluate(measurePlacement) + expect(tabindexEvidence.overlapsAvoidTarget).toBe(true) + expect(tabindexEvidence.simpleWordsButton.top).toBeGreaterThan( + tabindexEvidence.editor.bottom - 40 + ) + await page.screenshot({ + path: path.join(evidenceDir, 'tabindex-container-not-avoided.png'), + fullPage: true + }) + + await writeFile( + path.join(evidenceDir, 'placement-evidence.json'), + `${JSON.stringify({ buttonControlEvidence, tabindexEvidence }, null, 2)}\n` + ) + } finally { + await context?.close() + await app.close() + } +}) + +async function configureSiteEnablementOnly( + context: BrowserContext +): Promise { + const serviceWorker = + context.serviceWorkers()[0] ?? (await context.waitForEvent('serviceworker')) + + await serviceWorker.evaluate(() => { + return chrome.storage.local.set({ + enabledDomains: ['127.0.0.1'] + }) + }) +} + +async function startFixtureServer(): Promise<{ + baseURL: string + close: () => Promise +}> { + const server: Server = createServer((request, response) => { + if (request.url === '/button-overlap') { + response.writeHead(200, { 'content-type': 'text/html' }) + response.end(fixturePage('')) + return + } + + if (request.url === '/tabindex-nearby') { + response.writeHead(200, { 'content-type': 'text/html' }) + response.end(fixturePage('
Focusable wrapper
')) + return + } + + response.writeHead(404, { 'content-type': 'text/plain' }) + response.end('not found') + }) + + await new Promise((resolve) => { + server.listen(0, '127.0.0.1', resolve) + }) + + const address = server.address() + if (!address || typeof address === 'string') { + throw new Error('Unable to start E2E fixture server') + } + + return { + baseURL: `http://127.0.0.1:${address.port}`, + close: () => + new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error) + return + } + + resolve() + }) + }) + } +} + +function fixturePage(avoidTarget: string): string { + return ` + + + Simple Words button overlap fixture + + + +
+

Reply composer

+
+ + ${avoidTarget} +
+
+ +` +} + +function measurePlacement(): { + editor: PlainRect + avoidTarget: PlainRect + simpleWordsButton: PlainRect + overlapsAvoidTarget: boolean +} { + const plainRect = (element: HTMLElement | null): PlainRect => { + if (!element) { + throw new Error('Expected element to exist') + } + + const rect = element.getBoundingClientRect() + return { + top: Math.round(rect.top), + left: Math.round(rect.left), + right: Math.round(rect.right), + bottom: Math.round(rect.bottom), + width: Math.round(rect.width), + height: Math.round(rect.height) + } + } + const rectsOverlap = (a: PlainRect, b: PlainRect): boolean => { + return ( + a.left < b.right && + a.right > b.left && + a.top < b.bottom && + a.bottom > b.top + ) + } + + const editor = plainRect(document.getElementById('editor')) + const avoidTarget = plainRect(document.getElementById('avoid-target')) + const simpleWordsButton = plainRect(document.getElementById('simplewords-button')) + + return { + editor, + avoidTarget, + simpleWordsButton, + overlapsAvoidTarget: rectsOverlap(simpleWordsButton, avoidTarget) + } +} + +interface PlainRect { + top: number + left: number + right: number + bottom: number + width: number + height: number +} From da2b8779d386934460873752df9b30ff8eaf9267 Mon Sep 17 00:00:00 2001 From: kunchenguid Date: Fri, 15 May 2026 13:12:24 -0700 Subject: [PATCH 12/12] no-mistakes(lint): Format changed e2e spec --- e2e/button-overlap.spec.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/e2e/button-overlap.spec.ts b/e2e/button-overlap.spec.ts index 5bf97ff..fbdd582 100644 --- a/e2e/button-overlap.spec.ts +++ b/e2e/button-overlap.spec.ts @@ -10,7 +10,9 @@ const evidenceDir = path.join(process.cwd(), 'test-results', 'button-overlap') test('places the Simple Words button away from page buttons but not broad tabindex containers', async () => { const app = await startFixtureServer() const extensionPath = path.join(process.cwd(), 'extension') - const userDataDir = await mkdtemp(path.join(tmpdir(), 'simplewords-overlap-e2e-')) + const userDataDir = await mkdtemp( + path.join(tmpdir(), 'simplewords-overlap-e2e-') + ) let context: BrowserContext | undefined try { @@ -84,13 +86,19 @@ async function startFixtureServer(): Promise<{ const server: Server = createServer((request, response) => { if (request.url === '/button-overlap') { response.writeHead(200, { 'content-type': 'text/html' }) - response.end(fixturePage('')) + response.end( + fixturePage('') + ) return } if (request.url === '/tabindex-nearby') { response.writeHead(200, { 'content-type': 'text/html' }) - response.end(fixturePage('
Focusable wrapper
')) + response.end( + fixturePage( + '
Focusable wrapper
' + ) + ) return } @@ -201,7 +209,9 @@ function measurePlacement(): { const editor = plainRect(document.getElementById('editor')) const avoidTarget = plainRect(document.getElementById('avoid-target')) - const simpleWordsButton = plainRect(document.getElementById('simplewords-button')) + const simpleWordsButton = plainRect( + document.getElementById('simplewords-button') + ) return { editor,