localStorage.CHATXS_openpages = Date.now();
var onLocalStorageEvent = function(e){
if(e.key == "CHATXS_openpages"){
localStorage.CHATXS_page_available = Date.now();
}
if(e.key == "CHATXS_page_available"){
setTimeout(()=>{
console.log("Pagina extra abierta", getContador());
},150)
}
if(e.key == "CHATXS_page_closes"){
setTimeout(()=>{
SendOnCloseTabs();
},150)
}
};
window.addEventListener('storage', onLocalStorageEvent, false);
//window.addEventListener("beforeunload", function (e) {
// localStorage.CHATXS_page_closes = Date.now();
//});
var getContador = function(){
return tabCount.tabsCount();
}
var PIC = "";
initchat();
var positions = {
"po1": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; left:1%; top:1%; border-radius:50%;',
"po2": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; right:1%; top:1.3%; border-radius:50%;',
"po3": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; width: 80px; left: 3%; bottom: 1%; border-radius: 50%;',
"po4": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; right:1%; bottom:1%; border-radius:50%;',
"po0" : 'position: fixed; height: 0; width: 0 '
};
var ogPositions = {
"po1": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; left:1%; top:1%; border-radius:50%;',
"po2": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; right:1%; top:1.3%; border-radius:50%;',
"po3": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; width: 80px; left: 3%; bottom: 1%; border-radius: 50%;',
"po4": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; right:1%; bottom:1%; border-radius:50%;',
"po0" : 'position: fixed; height: 0; width: 0 '
};
//var startPositions = {
// "start_po1": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; left:1%; top:1%; border-radius:5px;',
// "start_po2": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; top:1%; border-radius:5px;',
// "start_po3": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; left:1%; bottom:1%; border-radius:5px;',
// "start_po4": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; bottom:1%; border-radius:5px;'
//};
var startPositions = {
"start_po1": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; left:1%; top:1%; border-radius:5px;',
"start_po2": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; right:1%; top:-5%; border-radius:5px;',
"start_po3": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; left:-5em; bottom:1%; border-radius:5px;',
"start_po4": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; right:1%; bottom:1%; border-radius:5px;'
};
//var chatBoxPositions = {
// "chatBox_po1": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; left:1%; top:1%; border-radius:5px;',
// "chatBox_po2": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; top:1%; border-radius:5px;',
// "chatBox_po3": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; left:1%; bottom:1%; border-radius:5px;',
// "chatBox_po4": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; bottom:1%; border-radius:5px;'
//};
var chatBoxPositions = {
"chatBox_po1": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; left:1%; top:1%; border-radius:5px;',
"chatBox_po2": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; right:1%; top:-5%; border-radius:5px;',
"chatBox_po3": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; left:-3%; bottom:1%; border-radius:5px;',
"chatBox_po4": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 655px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; right:1%; bottom:1%; border-radius:5px;'
};
//var minimizePositions = {
// "minimize_po1": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; left:1%; top:1%; border-radius:5px;',
// "minimize_po2": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; top:1%; border-radius:5px;',
// "minimize_po3": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; left:1%; bottom:1%; border-radius:5px;',
// "minimize_po4": 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; bottom:1%; border-radius:5px;'
//};
var minimizePositions = {
"minimize_po1": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; left:1%; top:1%; border-radius:5px;',
"minimize_po2": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; right:1%; top:-5%; border-radius:5px;',
"minimize_po3": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; left:-3%; bottom:1%; border-radius:5px;',
"minimize_po4": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 60px !important; max-height: 950px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 415px; right:1%; bottom:1%; border-radius:5px;'
};
//var styleFrameStart = 'border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 475px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 265px; right:1%; bottom:1%; border-radius:5px;';
var ic = "";
var executeAction = (data) => {
//console.log(data);
switch (data.type) {
case "start":
//launchChat state for animation
document.getElementById("xsChat").style = startPositions[data.type + "_" + ic];
break;
case "chatBox":
//chatbox, cpt obvio
document.getElementById("xsChat").style = chatBoxPositions[data.type + "_" + ic];
break;
case "minimize":
//currently shouldn't be used
document.getElementById("xsChat").style = minimizePositions[data.type + "_" + ic];
break;
case "refresh":
//document.getElementById('xsChat')['src'] += '';
reloadChat();
break;
case "addCss":
//document.getElementById('xsChat')['src'] += '';
addCustomCss(data.dt)
break;
case "requestData":
//document.getElementById('xsChat')['src'] += '';
SendMsg();
break;
case "getTabs":
//document.getElementById('xsChat')['src'] += '';
SendCurrentTabs();
break;
case "setClientToken":
//document.getElementById('xsChat')['src'] += '';
setCacheToken(data.dt);
break;
case "getClientToken":
//document.getElementById('xsChat')['src'] += '';
getCacheToken();
break;
case "getUsrWebData":
//document.getElementById('xsChat')['src'] += '';
SendUsrWebData();
break;
default:
// console.log("default included", data);
if(data.type){
if (data.type.includes("po")) {
ic = data.type;
//console.log(ic);
document.getElementById("xsChat").style = positions[data.type];
}
}
break;
}
}
//var widgetUrl = "https://dev.chatxs.softbox.mx"
//var widgetUrl ="https://dev.chatxs.softbox.mx/plugin"
//var widgetUrl = "http://192.168.0.12:4202"
var widgetUrl = "https://chatxs-widget.exagono.net"
function initchat() {
setTimeout(() => {
console.log("Init creation chat");
// document.addEventListener("DOMContentLoaded", function(event) {
var path = document.getElementById('Chat-XS-Widget').src;
let elem = document.querySelector('body');
//elem.innerHTML = elem.innerHTML + "";
//elem.innerHTML = elem.innerHTML + "";
var iframeSrc = widgetUrl + "/#/validations/" + path.substring(path.indexOf("?ref=") + 5, path.length) + "/" + window.location.host + ""
var iframStyle = positions["po0"];
console.log(iframeSrc);
var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'xsChat');
iframe.setAttribute('src', iframeSrc);
iframe.setAttribute('style', iframStyle);
elem.appendChild(iframe);
window.PIC = window.location.href;
window.addEventListener('message', function(event) {
executeAction(event.data);
}, false);
// });
}, 100);
}
function reloadChat() {
document.getElementById('xsChat').remove();
setTimeout(() => {
// document.addEventListener("DOMContentLoaded", function(event) {
var path = document.getElementById('Chat-XS-Widget').src;
let elem = document.querySelector('body');
//elem.innerHTML = elem.innerHTML + "";
//elem.innerHTML = elem.innerHTML + "";
var iframeSrc = widgetUrl + "/#/validations/" + path.substring(path.indexOf("?ref=") + 5, path.length) + "/" + window.location.host + ""
var iframStyle = positions["po0"];
var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'xsChat');
iframe.setAttribute('src', iframeSrc);
iframe.setAttribute('style', iframStyle);
elem.appendChild(iframe);
resetCss();
// });
}, 100);
}
function PIC() {
return window.location.href;
}
function GetCurrentPath(){
return window.location.pathname;
}
function SendMsg(){
var iFrame = document.getElementById('xsChat');
iFrame.contentWindow.postMessage({ 'type': "sendExtra", 'data': GetCurrentPath() }, "*");
//window.parent.postMessage({ 'type': "sendExtra", 'data': GetCurrentPath() }, "*");
}
function SendCurrentTabs(){
var iFrame = document.getElementById('xsChat');
iFrame.contentWindow.postMessage({ 'type': "sendTab", 'data': getContador() }, "*");
}
function SendOnCloseTabs(){
var iFrame = document.getElementById('xsChat');
iFrame.contentWindow.postMessage({ 'type': "closedTab", 'data': getContador() }, "*");
}
function SendUsrWebData(){
var iFrame = document.getElementById('xsChat');
iFrame.contentWindow.postMessage({ 'type': "sendUsrWebDataDom", 'data': getUserWebData() }, "*");
}
function addCustomCss(custom){
resetCss();
positions["po1"] = positions["po1"] + " " + custom;
positions["po2"] = positions["po2"] + " " + custom;
positions["po3"] = positions["po3"] + " " + custom;
positions["po4"] = positions["po4"] + " " + custom;
}
function resetCss(){
positions = {
"po1": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; left:1%; top:1%; border-radius:50%;',
"po2": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; right:1%; top:1.3%; border-radius:50%;',
"po3": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; width: 80px; left: 3%; bottom: 1%; border-radius: 50%;',
"po4": 'border: none; background: transparent; z-index: 999999999999; transform: translateZ(0px); position: fixed; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom; opacity: 1; height: 80px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px; width: 80px; right:1%; bottom:1%; border-radius:50%;',
"po0" : 'position: fixed; height: 0; width: 0 '
}
}
function SendCacheToken(token){
var iFrame = document.getElementById('xsChat');
iFrame.contentWindow.postMessage({ 'type': "getCacheToken", 'data': token }, "*");
//window.parent.postMessage({ 'type': "sendExtra", 'data': GetCurrentPath() }, "*");
}
function getCacheToken(){
if(localStorage.getItem('visitorToken')){
return localStorage.getItem('visitorToken');
}else{
return ""
}
}
function setCacheToken(token){
localStorage.setItem('visitorToken',token);
return localStorage.getItem('visitorToken');
}
function deleteCacheToken(){
localStorage.removeItem('visitorToken');
}
function getUserWebData(){
var data = {name : "", lastname : "", email : ""};
if( document.getElementById("hNombre")){
data.name = document.getElementById("hNombre").value;
}
if( document.getElementById("hApellido")){
data.lastname = document.getElementById("hApellido").value;
}
if( document.getElementById("hCorreo")){
data.email = document.getElementById("hCorreo").value;
}
return data;
}
//TabCountModule
var TabCount = /** @class */ (function () {
function TabCount() {
var _this = this;
/**
* @updateInterval: interval in milli seconds to count/update active tabs status.
* minimum value: 1000
*/
this.updateInterval = 10000;
/**
* @TabId: unique id for this tab.
*/
this.tabId = Math.random().toString(36).substring(7);
this.tabsCounter = 0;
this.onTabCountUpdate = [];
this.updateActiveInterval = 0;
this.tabsCount = function (skipCallback) {
if (skipCallback === void 0) { skipCallback = true; }
var data = _this.getData();
var listIds = Object.keys(data.list);
var now = Date.now();
var count = 0;
listIds.forEach(function (id) {
if (data.list[id].lastActive + _this.updateInterval * 1.2 > now) {
count++;
}
});
if (!skipCallback && _this.tabsCounter !== count) {
_this.onTabCountUpdate.forEach(function (event) {
event(count);
});
}
return _this.tabsCounter = count;
};
this.updateActive = function () {
var data = _this.getData(), now = Date.now();
if (data.list[_this.tabId] === undefined) {
data.list[_this.tabId] = {
TabOpenedTimeStamp: now
};
}
data.list[_this.tabId].url = window.location.href;
data.list[_this.tabId].lastActive = now;
if (undefined === data.lastCleaned || +data.lastCleaned + 20000 < now) {
data = _this.clearList(data);
}
_this.updateData(data);
_this.tabsCount(false);
};
/**
* Cleans data of closed tabs
*/
this.clearList = function (data) {
var listIds = Object.keys(data.list);
var now = Date.now();
listIds.forEach(function (id) {
if (data.list[id].lastActive + Math.max(8000, _this.updateInterval * 1.5) < now) { //If tab last update is older get rid of it.
delete data.list[id];
}
});
data.lastCleaned = now;
return data;
};
/**
*
* @param {function} callback
* @param {boolean} executeNow => optional, to execute the callback immediatly with current tab count.
*/
this.onTabChange = function (callback, executeNow) {
if (executeNow === void 0) { executeNow = false; }
if (typeof callback === "function") {
_this.onTabCountUpdate.push(callback);
if (executeNow) {
callback(_this.tabsCount());
}
}
};
this.updateData = function (data) {
localStorage.setItem('tabCountData', typeof (data) === "string" ? data : JSON.stringify(data));
};
this.getData = function () {
var savedData = localStorage.getItem('tabCountData');
return savedData == null ? { list: {}, lastCleaned: 0 } : JSON.parse(savedData);
};
/**
* Get list of urls of opened tabs.
* @param {boolean} getUnique =>get list of unique urls.
*/
this.getUrls = function (getUnique) {
if (getUnique === void 0) { getUnique = false; }
var data = _this.getData();
var urlList = [];
Object.keys(data.list).forEach(function (lt) {
if (!getUnique || urlList.indexOf(data.list[lt].url) === -1) {
urlList.push(data.list[lt].url);
}
});
return urlList;
};
this.setUpdateInterval = function (interval) {
if (interval === void 0) { interval = _this.updateInterval; }
if (null !== _this.updateActiveInterval) {
_this.pause();
}
_this.start(interval);
};
this.pause = function () {
clearInterval(_this.updateActiveInterval);
_this.updateActiveInterval = 0;
};
this.start = function (interval) {
if (interval === void 0) { interval = _this.updateInterval; }
_this.updateActiveInterval = setInterval(function () {
_this.updateActive();
}, _this.updateInterval = interval);
};
/**
* Initialise
*/
this.updateActive();
this.start();
window.onbeforeunload = function (e) {
var data = _this.getData();
delete data.list[_this.tabId];
_this.updateData(data);
localStorage.CHATXS_page_closes = Date.now();
};
}
return TabCount;
}());
var tabCount = new TabCount();