I'm doing an extension for chrome that replaces the selected text on a page with templates predefined by the user.
In general it goes well, and it replaces the text in any part of the page, including the body of the Gmail messages, which was the original purpose, but when the selection is inside a textarea it does not work, I imagine that because the inside of a textarea is not part of the DOM itself.
How could I detect if the selected text is inside a textarea and in that case replace it in another way? Here the relevant code:
chrome.runtime.onMessage.addListener(
function(request) {
console.log("recibo");
fill(request.texto);
}
);
function fill(plant) {
var mitext = window.getSelection()
var res = plant.replace(/#/g, mitext);
var sel, range;
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(res));
}
}