xpmn / firefox-to-deepl

Firefox extension. Highlight text on a web page and send it to DeepL
https://addons.mozilla.org/en-US/firefox/addon/to-deepl/
MIT License
88 stars 8 forks source link

Selected text from textbox is not translated if using keyboard shortcuts #33

Closed zznidar closed 2 months ago

zznidar commented 7 months ago

Selecting some text within a text field and trying to translate it using keyboard shortcuts restults in an empty translation. The issue lies in Firefox bug (https://bugzilla.mozilla.org/show_bug.cgi?id=1527627) where getSelection() returns no text if inside a textbox.

Right-clicking it and sending it to DeepL from context menu works, however, because different code is used.

Repro steps

  1. Open a webpage with a text input
  2. Write some text
  3. Select text
  4. Press your defined keyboard shortcut to send to DeepL

Expected result

Selected text should be translated

Actual result

No text is translated

Fix

In the mentioned FF bug, the following workaround is suggested: document.activeElement?.value?.substring(document.activeElement.selectionStart,document.activeElement.selectionEnd) Although it works fine for single selection, it only returns the last selection if you select multiple parts of the text (while holding down Ctrl key). Nevertheless, I think this is still acceptable for most cases.

I'll sumbit a PR soon.