(function(w) { w.RocketChat = w.RocketChat || { _: [] }; var config = {}; var widget; var iframe; var hookQueue = []; var ready = false; var closeWidget = function() { widget.dataset.state = 'closed'; widget.style.height = '30px'; }; var openWidget = function() { widget.dataset.state = 'opened'; widget.style.height = '300px'; }; // hooks var callHook = function(action, params) { if (!ready) { return hookQueue.push(arguments); } var data = { src: 'rocketchat', fn: action, args: params }; iframe.contentWindow.postMessage(data, '*'); }; var api = { ready: function() { ready = true; if (hookQueue.length > 0) { hookQueue.forEach(function(hookParams) { callHook.apply(this, hookParams); }); hookQueue = []; } }, toggleWindow: function(/*forceClose*/) { if (widget.dataset.state === 'closed') { openWidget(); } else { closeWidget(); } }, openPopout: function() { closeWidget(); var popup = window.open(config.url + '?mode=popout', 'livechat-popout', 'width=400, height=450, toolbars=no'); popup.focus(); }, openWidget: function() { openWidget(); }, removeWidget: function() { document.getElementsByTagName('body')[0].removeChild(widget); } }; var pageVisited = function() { callHook('pageVisited', { location: JSON.parse(JSON.stringify(document.location)), title: document.title }); }; var setCustomField = function(key, value) { callHook('setCustomField', [ key, value ]); }; var currentPage = { href: null, title: null }; var trackNavigation = function() { setInterval(function() { if (document.location.href !== currentPage.href) { pageVisited(); currentPage.href = document.location.href; currentPage.title = document.title; } }, 800); }; var init = function(url) { if (!url) { return; } config.url = url; var chatWidget = document.createElement('div'); chatWidget.dataset.state = 'closed'; chatWidget.className = 'rocketchat-widget'; chatWidget.innerHTML = '