(function (url, position) {var BOTTOM_LEFT = 'bottom_left'; var BOTTOM_RIGHT = 'bottom_right'; var iframe = document.createElement('iframe'); iframe.setAttribute('src', url); iframe.style.width = '0'; iframe.style.height = '0'; iframe.style.border = '0'; iframe.style.margin = '0'; iframe.style.padding = '0'; iframe.style.scroll = 'no'; iframe.addEventListener('load', onFrameLoad, false); var needChatOpen = needChatClose = false; acceptEvent('mia:open', function () { needChatOpen = true; }); acceptEvent('mia:close', function () { needChatClose = true; }); function onFrameLoad(event) { var frame = event.target; setInterval(function() { frame.style.height = frame.contentWindow.document.body.clientHeight + 'px'; frame.style.width = frame.contentWindow.document.body.scrollWidth + 'px'; needChatOpen && deliverEvent('mia:open', function () { needChatOpen = false; }); needChatClose && deliverEvent('mia:close', function () { needChatClose = false; }); }, 0); if (customPosition) { frame.contentWindow.document.body.className += ' mia-chat-custom-position'; } } function deliverEvent(eventName, callback) { var event = new Event(eventName); iframe.contentWindow.document.dispatchEvent(event); typeof callback === 'function' && callback(); } function acceptEvent(eventName, callback) { window.document.addEventListener(eventName, function (event) { event.preventDefault(); event.stopPropagation(); typeof callback === 'function' && callback(); }); } var customPosition = false; var wrapper = document.querySelector('[data-mia-chat="wrapper"]'); if (wrapper) { customPosition = true; wrapper.appendChild(iframe); } else { wrapper = document.createElement('div'); wrapper.className = 'mia-chat'; wrapper.style.position = 'fixed'; switch (position) { case BOTTOM_RIGHT: wrapper.style.right = '5px'; wrapper.style.bottom = '-4px'; break; case BOTTOM_LEFT: wrapper.style.left = '5px'; wrapper.style.bottom = '-4px'; break; default: wrapper.style.left = '5px'; wrapper.style.bottom = '-4px'; break; } wrapper.style.margin = '0'; wrapper.style.padding = '0'; wrapper.style.overflow = 'hidden'; wrapper.style.zIndex = 1000000; wrapper.appendChild(iframe); document.body.appendChild(wrapper); } })('/mia-proxy/chat/window/render', 'bottom_right');