We are developing a Chrome extension in which we tried to get the selected text in a browser tab. To do so, we are using a context menu option but we got an empty string as selected text by using the following method in background.js
:
window.getSelection().toString()
Then referred this link and changed the method to get the selected text as:
chrome.contextMenus.create({
title: "Click",
contexts: ["selection"],
onclick: function(info, tab) {
selectedText = info.selectionText;
};
});
Using this method we are able to get the selected text as string (single line) but with no line break. Is it possible to get the selected text while preserving line breaks?
For example, if the selected content is:
Text first line
Text second line
We are getting the selected text as Text first line Text second line
(on a single line, with no line break).