const COOKIE_NAME_SESSID = '_rb_sessionid';
const COOKIE_NAME_TEST = '_rb_test';
var sessionId = getCookie(COOKIE_NAME_SESSID);
if (sessionId === undefined) {
document.cookie = COOKIE_NAME_SESSID + '=' + getSessionUniqueId();
sessionId = getCookie(COOKIE_NAME_SESSID);
}
document.addEventListener('click', e => {
const rbContainer = document.getElementById('rb-container');
if (e.target.classList.contains('rb-bubble')) {
document.getElementById("rb-button").style.display = 'none';
document.getElementById("rb-chat").style.display = 'block';
rbContainer.style.width = 'auto';
rbContainer.style.height = 'auto';
}
if (e.target.classList.contains('rb-remove')) {
document.getElementById("rb-button").style.display = 'block';
document.getElementById("rb-chat").style.display = 'none';
rbContainer.style.width = '130px';
rbContainer.style.height = '45px';
}
});
var test = getCookie(COOKIE_NAME_TEST) === undefined ? 0 : 1,
host = test;
if (test) {
host = 'lk2.in-fo.ru';
} else {
host = window.location.host === 'admar-online.ru' ? 'chat.admar-online.ru' : 'lk.sales-management-center.com';
}
if (document.readyState !== 'loading') {
renderWidget();
} else {
document.addEventListener('DOMContentLoaded', renderWidget);
}
function renderWidget() {
let linkStyle = document.createElement('link');
linkStyle.setAttribute('rel', 'stylesheet');
linkStyle.setAttribute('href', `https://${host}/css/chat/widgetjs.css`);
document.head.appendChild(linkStyle);
let elemRbContainer = document.createElement('div');
elemRbContainer.setAttribute('id', 'rb-container');
let style = '--width-widget:350px;';
style += '--height-widget:500px;';
style += '--color-bg:#110ec4;';
elemRbContainer.style.cssText = style;
document.body.appendChild(elemRbContainer);
// Кнопка для вызова чата
let elemRbButton = document.createElement('div');
elemRbButton.setAttribute('id', 'rb-button');
elemRbButton.innerHTML = 'Напишите нам';
elemRbContainer.appendChild(elemRbButton);
// Чат с лентой и формой
let elemRemoveDiv = document.createElement('div');
elemRemoveDiv.setAttribute('class', 'rb-remove');
let elemIframe = document.createElement('iframe');
elemIframe.setAttribute('id', 'rb-iframe');
elemIframe.setAttribute('src', 'https://' + host + '/chat/widget/679fe280947fc'
+ '?session_id=' + sessionId
+ '&test=' + test
);
elemIframe.style.cssText = 'width:100%;height:100%;border:0;';
let elemRbChat = document.createElement('div');
elemRbChat.setAttribute('id', 'rb-chat');
elemRbChat.style.cssText = 'display:none;';
elemRbChat.appendChild(elemRemoveDiv);
elemRbChat.appendChild(elemIframe);
elemRbContainer.appendChild(elemRbChat);
}
function getSessionUniqueId() {
let idstr = String.fromCharCode(Math.floor((Math.random() * 25) + 65)).toLowerCase();
do {
let ascicode = Math.floor((Math.random() * 42) + 48);
if (ascicode < 58 || ascicode > 64) {
idstr += String.fromCharCode(ascicode).toLowerCase();
}
} while (idstr.length < 32);
return (idstr);
}
function getCookie(name) {
let matches = document.cookie.match(new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));
return matches ? decodeURIComponent(matches[1]) : undefined;
}