diff options
Diffstat (limited to 'src/background/menu-translation.js')
| -rw-r--r-- | src/background/menu-translation.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/background/menu-translation.js b/src/background/menu-translation.js new file mode 100644 index 0000000..1465c53 --- /dev/null +++ b/src/background/menu-translation.js @@ -0,0 +1,28 @@ +/** + * Context-menu translation entry (background). + * + * Receives the raw `menus.OnClickData.selectionText`, validates it through + * the shared pipeline and starts a translation without a sink. The popup + * observes results via the shared translation state (GET_POPUP_STATE). + */ + +import { validateSelectionText, runTranslation, messageForCode } from './translation-service.js'; +import { createRequestId } from '../shared/messages.js'; +import { ERROR_CODES } from '../shared/constants.js'; + +/** + * Start a translation from a context-menu selection. + * @param {*} selectionText raw value from menus.OnClickData + * @returns {{ok: boolean, requestId?: string, error?: string, message?: string}} + */ +export function startFromSelection(selectionText) { + const validated = validateSelectionText(selectionText); + if (!validated.ok) { + return { ok: false, error: validated.code, message: messageForCode(validated.code) }; + } + const requestId = createRequestId(); + // No sink: the popup polls GET_POPUP_STATE and receives streaming updates + // through the shared translation state. + void runTranslation({ text: validated.text, requestId, source: 'context-menu', sink: null }); + return { ok: true, requestId }; +}
\ No newline at end of file |
