Comment #5 on issue 116429 by
yaoziy...@gmail.com: Linebreaks/newlines
This bug is very serious. The reason why Chrome replaces all line-breaks in
selectionText with spaces is probably that it wants to put the string as
a %s parameter in a context menu item's title, e.g. "Search Google
for '%s'...".
But there are also many scenarios where preserving line-breaks in
selectionText would be desired, such as a Chrome extension that sends
selected text to Google Translate for translation, where the text could
have multiple paragraphs.
I have an extension that also happens to want multi-line selectionText:
https://chrome.google.com/webstore/detail/pie-international/jafbohhbdpejlcfpkbbpkegglokegjid .
The extension adds diacritics to English words to show pronunciation, but
also wants to provide a context menu item "Copy without Diacritics" which
strips selected text of diacritics and copies it to the clipboard.
Naturally it wants all line-breaks in selectionText to remain intact.
I think Chrome can provide two arguments: selectionText and
selectionTextPreview, where selectionText is the full selection text with
line-breaks preserved, while selectionTextPreview is a single-line, short
preview of the full selection text and can serve as %s in context menu item
titles.
There are workarounds that inject content scripts to every tab, frame and
iframe and listens to their mouseUp and keyPress events and, upon such
events, actively sends window.getSelection().toString() to the background
page, so that they can make sure when a context menu item is clicked, the
background page always knows what is selected in the currently focused
tab/frame/iframe. But this is certainly cumbersome.