I am using this script to convert words
on a text to links that open on Google Translate, but I'd like to convert sentences
, not word by word, sentences separated on pontuation like "?", ",", ".", ";", ";", ":" and so on. How could I do it?
//script
str = document.getElementById('texto-clicavel').innerHTML;
res = str.replace(/\"/g, "'");
document.getElementById('texto-clicavel').innerHTML = res;
// teste=document.getElementById('texto-clicavel').innerHTML = res;
// console.log(teste)
var criarLink = t => `<a target="_blank" onclick="window.open('https://translate.google.com/m/translate?ie=UTF8&sl=pt-BR&tl=en&q=#en/pt/${t}')">${t}</a>`
var criarArrayDeLinks = e => e.textContent.split(' ').filter(i => i !== '' && i !== '\n').map(i => criarLink(i));
function criarLinksEmElementos(pai) {
const tags = [];
if (pai.nodeType == 3 && pai.textContent.trim()) {
tags.push(...criarArrayDeLinks(e));
} else {
pai.childNodes
.forEach(
e => {
if (e.nodeType == 3 && e.textContent.trim()) {
tags.push(...criarArrayDeLinks(e));
} else {
tags.push(criarLinksEmElementos(e).outerHTML);
}
});
}
if (tags.length) {
pai.innerHTML = tags.join(' ');
}
return pai;
}
function criarTextoClicavel(seletor) {
const div = document.querySelector(seletor);
criarLinksEmElementos(div);
}
criarTextoClicavel('#texto-clicavel');