Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix(selection): require target for long translations
Emre Sokullu8f9aa4264fa0
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.jsindex fbc02c36d..4cb18d511 100644--- a/src/chrome/src/content/selection-shortcut.js+++ b/src/chrome/src/content/selection-shortcut.js@@ -629,6 +629,10 @@cardParts.target.addEventListener('change', () => {if (!cardState || !isSupportedTranslationLanguage(cardParts.target.value)) return;cardState.target = cardParts.target.value;+ if (snapshot?.text.length > INLINE_MAX_CHARS) {+ submitSelection('translate', '', cardState.target);+ return;+ }runInline();});cardParts.retry.addEventListener('click', (event) => {@@ -954,7 +958,7 @@async function openCard(kind, opener) {if (!snapshot || submitting || !INLINE_ACTIONS.includes(kind)) return;- if (snapshot.text.length > INLINE_MAX_CHARS) {+ if (kind !== 'translate' && snapshot.text.length > INLINE_MAX_CHARS) {// Too long for a card: the side panel is the better place to read it. This is// decided before anything is awaited, because the panel can only be opened// inside the click's user gesture, which the localization wait would outlive.@@ -993,6 +997,15 @@// does not dismiss it, and Escape and Enter reach the card's handlers.card.focus({ preventScroll: true });if (kind === 'translate') {+ // Long selections go straight to the side panel, where an automatic target+ // could be a no-op. Require an explicit target rather than guessing.+ if (snapshot?.text.length > INLINE_MAX_CHARS) {+ cardState.status = 'idle';+ cardState.target = '';+ renderCard();+ cardParts.target.focus({ preventScroll: true });+ return;+ }const detected = await detectSourceLanguage(cardState?.quote || '');if (requestId !== cardRequestId || !cardState) return;cardState.sourceLabel = detected ? languageName(detected) : '';diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.jsindex 117359b46..9c7c9e6d5 100644--- a/src/firefox/src/content/selection-shortcut.js+++ b/src/firefox/src/content/selection-shortcut.js@@ -629,6 +629,10 @@cardParts.target.addEventListener('change', () => {if (!cardState || !isSupportedTranslationLanguage(cardParts.target.value)) return;cardState.target = cardParts.target.value;+ if (snapshot?.text.length > INLINE_MAX_CHARS) {+ submitSelection('translate', '', cardState.target);+ return;+ }runInline();});cardParts.retry.addEventListener('click', (event) => {@@ -954,7 +958,7 @@async function openCard(kind, opener) {if (!snapshot || submitting || !INLINE_ACTIONS.includes(kind)) return;- if (snapshot.text.length > INLINE_MAX_CHARS) {+ if (kind !== 'translate' && snapshot.text.length > INLINE_MAX_CHARS) {// Too long for a card: the side panel is the better place to read it. This is// decided before anything is awaited, because the panel can only be opened// inside the click's user gesture, which the localization wait would outlive.@@ -993,6 +997,15 @@// does not dismiss it, and Escape and Enter reach the card's handlers.card.focus({ preventScroll: true });if (kind === 'translate') {+ // Long selections go straight to the side panel, where an automatic target+ // could be a no-op. Require an explicit target rather than guessing.+ if (snapshot?.text.length > INLINE_MAX_CHARS) {+ cardState.status = 'idle';+ cardState.target = '';+ renderCard();+ cardParts.target.focus({ preventScroll: true });+ return;+ }const detected = await detectSourceLanguage(cardState?.quote || '');if (requestId !== cardRequestId || !cardState) return;cardState.sourceLabel = detected ? languageName(detected) : '';diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjsindex 704c09070..4b7e501de 100644--- a/test/fixtures/run.mjs+++ b/test/fixtures/run.mjs@@ -2677,6 +2677,32 @@ for (const [label, sourcePath, manualOpen] of [}});+ test(`${label}: a long translation waits for a target choice`, async (page) => {+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });+ await page.evaluate(() => {+ const article = document.createElement('article');+ article.id = 'long-english-selection';+ article.textContent = 'This is an English sentence. '.repeat(250);+ document.body.appendChild(article);+ });+ const selected = await selectFixtureText(page, '#long-english-selection');+ await clickRectCenter(page, selected.inlineRects.translate);+ await page.waitForFunction(() => window.__webbrainSelectionShortcut.getState().cardStatus === 'idle');+ let state = await page.evaluate(() => ({+ state: window.__webbrainSelectionShortcut.getState(),+ messages: window.__selectionMessages,+ }));+ if (state.messages.length || state.state.cardTarget !== '') {+ throw new Error(`a long translation should wait for a target: ${JSON.stringify(state)}`);+ }+ await page.evaluate(() => window.__webbrainSelectionShortcut.chooseTarget('de'));+ await page.waitForFunction(() => window.__selectionMessages.length === 1);+ state = await page.evaluate(() => ({ message: window.__selectionMessages[0], state: window.__webbrainSelectionShortcut.getState() }));+ if (state.message.language !== 'de' || state.state.cardVisible) {+ throw new Error(`the chosen target should be handed to the side panel: ${JSON.stringify(state)}`);+ }+ });+test(`${label}: a long selection is handed over at once, not after the language bundle loads`, async (page) => {// The side panel can only be opened inside the click's user gesture, so the hand-over// must not wait for localization.@@ -2692,13 +2718,13 @@ for (const [label, sourcePath, manualOpen] of [document.body.appendChild(article);});const selected = await selectFixtureText(page, '#long-selection');- await clickRectCenter(page, selected.inlineRects.translate);+ await clickRectCenter(page, selected.inlineRects.explain);await page.waitForFunction(() => window.__selectionMessages.length === 1, null, { timeout: 2000 });const result = await page.evaluate(() => ({message: window.__selectionMessages[0],inline: window.__selectionInlineMessages.length,}));- if (result.message.action !== 'translate' || result.inline !== 0) {+ if (result.message.action !== 'explain' || result.inline !== 0) {throw new Error(`a long selection should reach the side panel flow while localization is still loading: ${JSON.stringify(result)}`);}await page.evaluate(() => window.__releaseLocalization?.());