(function () { function createInlineIframe () { var frame = document.createElement('iframe') frame.id = 'kayako-messenger-frame' frame.style.border = 'none' frame.style.width = '100%' frame.style.height = '100%' frame.style.minHeight = 'initial' return frame } function createMessengerContainer () { var div = document.createElement('div') div.id = 'kayako-messenger' div.style.position = 'fixed' div.style.right = 0 div.style.bottom = 0 div.style.width = 0 div.style.height = 0 return div } function createScriptTag () { var script = document.createElement('script') script.async = true script.type = 'text/javascript' script.src = 'https://assets.kayako.com/messenger/build-56b493e275f5c4f7cc07.js' return script } function insertIframe () { var messengerContainer = createMessengerContainer() var iframe = createInlineIframe() document.body.appendChild(messengerContainer) messengerContainer.appendChild(iframe) iframe.contentWindow.document.open('text/html', 'replace') iframe.contentWindow.document.write('') iframe.contentWindow.document.write('') iframe.contentWindow.document.write('') iframe.contentWindow.document.write('') iframe.contentWindow.document.write('') iframe.contentWindow.document.body.appendChild(createScriptTag()) iframe.contentWindow.document.close() } function fetchSettings (endpoint, callback) { var xhr = new window.XMLHttpRequest() xhr.open('GET', endpoint, true) xhr.setRequestHeader('X-API-Token', '440be73f-7a9a-492f-a88b-745e5affb049') xhr.onload = function (e) { if (xhr.readyState === 4) { if (xhr.status === 200) { try { var settings = JSON.parse(xhr.responseText).data APICache(window.kayako._settings.apiUrl, 'settings/current').set(settings) callback(null, settings) } catch (e) { console.error('Failed to parse response JSON') callback(true) } } else { console.error(xhr.statusText) callback(true) } } } xhr.onerror = function () { console.error(xhr.statusText) callback(true) } xhr.send(null) // initiate the call } function APICache (apiURL, endpoint) { var key = 'kayako:messenger:' + apiURL + ':' + endpoint var expiry = 1000 * 60 * 5 // 5 minutes return { set: function (data) { try { var cachedContent = { timestamp: (new Date()).valueOf(), data: data } window.localStorage.setItem(key, JSON.stringify(cachedContent)) } catch (e) { // ignore, localStorage not supported, caching not possible } }, retrieve: function () { try { stringifiedContent = window.localStorage.getItem(key) if (!stringifiedContent) { return null } var cachedContent = JSON.parse(stringifiedContent) if ((cachedContent.timestamp + expiry) > (new Date().valueOf())) { return cachedContent.data } window.localStorage.removeItem(key) return null } catch (e) { return null } } } } function loadSettings (err, settings) { var getReplyTimeLocale = function (replyTimeExpectation) { if (replyTimeExpectation === null) { return '' } var expectationToLocaleMap = { AUTO: '', ASAP: 'reply.asap', FEW_MINS: 'reply.in.few.minutes', FEW_HOURS: 'reply.in.few.hours' } return expectationToLocaleMap[replyTimeExpectation] } if (!err) { // we replace the settings window.kayako._settings.homeTitles = settings.home_titles window.kayako._settings.homeSubtitles = settings.home_subtitles window.kayako._settings.widgets = settings.metadata.widgets window.kayako._settings.styles = settings.metadata.styles window.kayako._settings.replyTimeLocale = getReplyTimeLocale(settings.reply_time_expectation) window.kayako._settings.teamName = settings.brand.name window.kayako._settings.businessHour = settings.businesshour window.kayako._settings.enableSuggestions = settings.enable_suggestions } if (window.kayako && window.kayako.config) { // override _settings in [embed code] or [those from api] with locally defined customizations if any window.kayako._settings = mergeRecursive(window.kayako._settings, window.kayako.config) } insertIframe() } /* * Recursively merge properties of two objects, * Modifies first object for final result. functions and arrays are copied by reference. */ function mergeRecursive (destination, source) { for (var prop in source) { if (destination.hasOwnProperty(prop) && destination[prop].constructor === Object && source[prop].constructor === Object) { destination[prop] = mergeRecursive(destination[prop], source[prop]) } else { destination[prop] = source[prop] } } return destination; } function boot () { var apiUrl = window.kayako._settings.apiUrl var settingsCache = APICache(apiUrl, 'settings/current') var settings = settingsCache.retrieve() if (settings) { loadSettings(false, settings) return } var settingsEndpoint = apiUrl + '/messenger/settings/current?include=locale_field,brand,business_hour,businesshour_holiday' fetchSettings(settingsEndpoint, loadSettings) } if (window.kayako && window.kayako.newEmbedCode) { boot() } else { document.body.appendChild(createScriptTag()) } })()