function appendHtml(el, str) {
var div = document.createElement('div');
div.innerHTML = str;
while (div.children.length > 0) {
el.appendChild(div.children[0]);
}
}
var paginaAtual = window.location.href;
var formData = new FormData();
formData.append('url_real', paginaAtual);
fetch('https://grupothx.com.br/atendimento-online/apiTHX/scriptAtendimentoOnlineTodosOsSitesJs.php', {
method: 'POST',
body: formData
}).catch(err => console.log('Erro Log:', err));
var textoMensagem = "Olá! Estou na página (" + paginaAtual + ") e preciso de atendimento.";
var textoCodificado = encodeURIComponent(textoMensagem);
var linkWhatsapp = "https://wa.me/551146550330?text=" + textoCodificado;
function openLigue (p)
{
window.location.href = linkWhatsapp;
}
function sendMsgChat (texto)
{
window.location.href = linkWhatsapp;
}
var html = `
`;
var paused = false;
window.addEventListener('load', function(){
appendHtml(document.body, html);
initBot();
});
function initBot()
{
var el = document.getElementById('botCall');
var msg;
if(el && el.value)
{
msg = trataMsgBot(el.value);
}
else
{
msg = getSaudacaoBom();
}
var mensagens = msg.split(/\n+/).filter(function(x){
return x.trim();
});
var img =
document.querySelector('meta[property="og:image"]')?.content ||
document.querySelector('meta[name="twitter:image"]')?.content ||
document.querySelector('meta[itemprop="image"]')?.content;
console.log('mensagens antes', mensagens);
if(img)
{
mensagens.splice(1, 0,
'
'
);
}
console.log('mensagens depois', mensagens);
preloadImages(mensagens);
preloadImages(mensagens).then(function(){
showMessages(mensagens);
});
}
function preloadImages(lista)
{
var promises = [];
lista.forEach(function(msg){
var m = msg.match(/
]+src=["']([^"']+)/i);
if(m)
{
promises.push(new Promise(function(resolve){
var img = new Image();
img.onload = resolve;
img.onerror = resolve;
img.src = m[1];
}));
}
});
return Promise.all(promises);
}
function typeWriter(el, texto, callback)
{
texto = texto.replace(/\n/g, '
');
let i = 0;
el.innerHTML = '';
function next()
{
if(i >= texto.length)
{
if(callback)
callback();
return;
}
// Não quebrar as tags
if(texto.substr(i,4) == '
')
{
el.innerHTML += '
';
i += 4;
}
else
{
el.innerHTML += texto.charAt(i);
i++;
}
setTimeout(next, 18 + Math.random()*22);
}
next();
}
function addBotMessage(texto)
{
var chat = document.getElementById('thxMessages');
chat.innerHTML +=
''+
texto+
'
';
chat.scrollTop = chat.scrollHeight;
}
function showMessages(lista)
{
var box = document.getElementById('thxBotText');
var i = 0;
var textoOriginal = box.innerHTML;
var resumeTimer;
box.onmouseenter = function(){
clearTimeout(resumeTimer);
paused = true;
};
box.onmouseleave = function(){
clearTimeout(resumeTimer);
resumeTimer = setTimeout(function(){
paused = false;
}, 2000); // 2 segundos
};
mostrarMensagem(box, lista, i, function(tempo){
i++;
timerNext = setTimeout(next, tempo);
});
function next()
{
if(paused)
{
timerNext = setTimeout(next,100);
return;
}
if(i >= lista.length)
{
setTimeout(function(){
typeWriter(box, textoOriginal);
},4000);
return;
}
img = box.querySelector('img');
if(img)
{
img.classList.remove('show');
img.classList.add('hide');
setTimeout(function(){
mostrarDigitando();
setTimeout(function(){
mostrarMensagem(box, lista, i, function(tempo){
i++;
timerNext = setTimeout(next, tempo);
});
}, 500 + Math.random() * 900);
}, 1300);
return;
}
mostrarDigitando();
setTimeout(function(){
mostrarMensagem(box, lista, i, function(tempo){
i++;
timerNext = setTimeout(next, tempo);
});
},500 + Math.random()*900);
}
}
function mostrarDigitando()
{
var box = document.getElementById('thxBotText');
box.innerHTML =
''+
''+
''+
''+
'
';
}
function mostrarMensagem(box, lista, indice, callback)
{
if(isHtml(lista[indice]))
{
box.innerHTML = lista[indice];
var img = box.querySelector('img');
if(img)
{
requestAnimationFrame(function(){
img.classList.add('show');
});
}
callback(5000);
return;
}
typeWriter(box, lista[indice], function(){
callback(2500);
});
}
function trataMsgBot(msg)
{
console.log('trataMsgBot', msg);
return msg
.replaceAll('{cpanel_suspended}', getCpanelSuspendedMessage())
.replaceAll('{cpanel_main}', getPageMessage())
.replaceAll('{site}', location.hostname.replace(/^www\./,''))
.replaceAll('{url}', location.href)
.replaceAll('{host}', location.hostname)
.replaceAll('{titulo}', document.title)
.replaceAll('{nl}', '\n')
.replaceAll('{nl1}', '\n\n')
.replaceAll('{bom}', getSaudacaoBom())
.replaceAll('{chamada}', chamadaInicial())
.replaceAll('{end}', '\n\n' + chamadaFinal())
.replaceAll('{emoji}', getEmojiRand())
// remove placeholders desconhecidos
.replace(/\{[a-z0-9_]+\}/gi, '');
}
function getCpanelSuspendedMessage()
{
return [
'{bom}',
'Encontramos uma indisponibilidade.',
'No site {site}.',
'Nossa equipe já foi notificada.',
'Se você é o administrador deste site, fale conosco.',
'{end}'
].join('{nl1}');
}
function getPageMessage()
{
//var img =
//document.querySelector('meta[property="og:image"]')?.content ||
//document.querySelector('meta[name="twitter:image"]')?.content ||
//document.querySelector('meta[itemprop="image"]')?.content ||
//'';
return [
'{bom}',
//img ? '
' : '',
document.title + '.',
document.querySelector('meta[name="description"]')?.content || '',
].filter(Boolean).join('{nl1}');
}
function isHtml(texto)
{
return /<[^>]+>/.test(texto);
}
function getSaudacaoBom()
{
var agora = new Date();
var periodo =
agora.getHours() < 12 ? 'manha' :
agora.getHours() < 18 ? 'tarde' :
'noite';
var hoje = agora.toISOString().slice(0,10);
var chave = hoje + '-' + periodo + '-2' ;
if(sessionStorage.getItem('thxBotSaudacao') == chave)
{
console.log('Saudação de ' + periodo + ' já exibida hoje.');
return '';
}
sessionStorage.setItem('thxBotSaudacao', chave);
if(periodo == 'manha')
return 'Ótimo Dia! 🌞';
if(periodo == 'tarde')
return 'Ótima Tarde 🌤️';
return 'Ótima Noite 🌛';
}
function getEmojiRand()
{
var items = ['👋','😇','😉','☝️','⭐','😋','🧐','😎','😏'];
return items[Math.floor(Math.random()*items.length)];
}
function chamadaInicial()
{
var itens = [
'Olá!',
'Você não vai acreditar 😱',
'Olha a promoção 👀',
'Hey! 🙃'
];
return itens[Math.floor(Math.random()*itens.length)];
}
function chamadaFinal()
{
var itens = [
'Pra saber mais é só falar comigo! 🔥',
'Estou à disposição 😊',
'Não perca tempo 😉',
'Fale comigo!'
];
return itens[Math.floor(Math.random()*itens.length)];
}