selectionText in chrome.contextMenus API doesn't include new lines

1,250 views
Skip to first unread message

Eduardo Vela Nava

unread,
Dec 19, 2011, 10:52:21 PM12/19/11
to Chromium-extensions
1. chrome.contextMenus.create({
"title": "Test",
"contexts": ["selection"],
"onclick": function(info){
console.log(info.selectionText);
}
});
2. Select something that covers multiple lines, Right click -> Text
3. See the console

I would expect the console to log the new lines, but the new lines are
not saved.

This is very valuable information, and it gets lost, could it be
possible to get it somehow?

Łukasz Łoboda

unread,
Dec 21, 2011, 7:47:39 AM12/21/11
to Eduardo Vela Nava, Chromium-extensions
Are sure that there was <br> elements in text? 

2011/12/20 Eduardo Vela Nava <e...@google.com>

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Eduardo' Vela

unread,
Dec 21, 2011, 1:50:57 PM12/21/11
to Łukasz Łoboda, Chromium-extensions
I was testing with <textarea>xxxx
xxxxx
xxx</textarea>. I'll test again with <br/>

Thanks!
Eduardo Vela | Security Inge | e...@google.com | +1 (601) 675-8352

Eduardo' Vela

unread,
Dec 21, 2011, 1:55:05 PM12/21/11
to Łukasz Łoboda, Chromium-extensions
Confirmed, <br>, <p>, <li>, <div>, <span (display:block)>, etc.. all don't generate new lines when you read selectionText.

Greetings

Eduardo Vela | Security Inge | e...@google.com | +1 (601) 675-8352



Ziyuan Yao

unread,
Jan 19, 2013, 7:07:01 PM1/19/13
to chromium-...@chromium.org, Łukasz Łoboda
I confirm this, now that I add such a feature in my extension https://chrome.google.com/webstore/detail/pie-international/jafbohhbdpejlcfpkbbpkegglokegjid .
To unsubscribe from this group, send email to chromium-extensions+unsub...@chromium.org.

Ziyuan Yao

unread,
Jan 20, 2013, 10:18:50 AM1/20/13
to chromium-...@chromium.org, Łukasz Łoboda
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.

Related bug: http://code.google.com/p/chromium/issues/detail?id=116429

Mike West

unread,
Jan 29, 2013, 6:02:31 AM1/29/13
to chromium-...@chromium.org, Łukasz Łoboda
The textarea should, I think, give you line breaks in the selection text. I'm less sure about paragraphs, lists, etc, as those generally collapse whitespace when rendered. Regardless, I'll CC some folks on the bug for visibility. Let's continue the conversation there.

-mike

taki...@gmail.com

unread,
Nov 3, 2017, 6:12:02 AM11/3/17
to Chromium-Extensions-Announce
This bug is still present.

Is there an easy way to grab selected text with line breaks from background page?
Reply all
Reply to author
Forward
0 new messages