(function() { var metaViewport = document.querySelector('meta[name="viewport"]'); if(metaViewport) { if (window.location.hostname == 'les.moscow') { metaViewport.setAttribute('content', 'width=600, user-scalable=no'); } else { metaViewport.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'); } } var widgetTarget = '#realisteWidget'; const passUtmParametersAndProcessUrl = (iframeSrc) => { let additionalGetParams = []; // passing parent origin for cross frame communication const parentOriginParam = 'parentOrigin='+encodeURIComponent(window.location.origin); additionalGetParams.push(parentOriginParam); // passing utm info var info = null; try { info = localStorage.getItem(infoKey); var infoPrefix = 'realisteUTMInfo-', infoKey = infoPrefix + (new Date()).toISOString().substr(0, 10), r = document.referrer.toString(), l = window.location.toString(); currentInfo = JSON.stringify({'parentReferrer': r, 'parentLocation': l}); for(k in localStorage) if(k.indexOf(infoPrefix) == 0 && k != infoKey) delete localStorage[k]; if(!info) { info = currentInfo; localStorage.setItem(infoKey, currentInfo); } additionalGetParams.push('parentInfo=' + encodeURIComponent(info)); } catch(e) { } if (window.location.search) { const urlParams = new URLSearchParams(window.location.search); const utms = ['utm_medium', 'utm_source', 'utm_campaign', 'utm_content']; utms.forEach(el => { if (!urlParams.get(el)) return; const v = el + '=' + encodeURIComponent(urlParams.get(el)); additionalGetParams.push(v); }); //reassign plugin for other host plugins for ad campaigns if param passed if((urlParams.get('_p') || "").indexOf(window.location.toString().split('/')[2].replaceAll('.','-')) > -1){ iframeSrc = 'https://' + urlParams.get('_p') + '.realiste.io'; } } const hashparts = iframeSrc.split('/#/') if(hashparts.length > 1) iframeSrc = hashparts.shift() + '/' + hashparts.join('/#/'); let result = iframeSrc + (iframeSrc.indexOf('?') > -1 ? '&' : '?') + additionalGetParams.join('&'); return result; } const watchIframeFocus = (onFocus, onBlur) => { let iframeClickedLast; const windowBlurred = (e) => { const el = document.activeElement; if (el.tagName.toLowerCase() == 'iframe') { iframeClickedLast = true; onFocus(); } } const windowFocussed = (e) => { if (iframeClickedLast) { iframeClickedLast = false; onBlur(); } } window.addEventListener('focus', windowFocussed, true); window.addEventListener('blur', windowBlurred, true); } const onWidgetFocus = () => { realisteWidget.scrollIntoView({ behavior: 'smooth' }); } const scrollStop = (callback, refresh = 66) => { if (!callback || typeof callback !== 'function') return; let isScrolling; const detectIsScrolling = () => { window.clearTimeout(isScrolling); isScrolling = setTimeout(callback, refresh); }; 'mousewheel wheel DOMMouseScroll'.split(' ').forEach(el => window.addEventListener(el, detectIsScrolling, false)); }; function processStorageCommand(command) { if (['get', 'set', 'remove', 'get_settings'].indexOf(command.operation) >= 0) { var settings = JSON.parse(localStorage.getItem("__realisteSettings") || '{}') || {}; if(command.operation == 'get') { return settings[command.key]; } if(command.operation == 'set') { settings[command.key] = command.value; localStorage.setItem("__realisteSettings", JSON.stringify(settings)); } if(command.operation == 'remove') { delete settings[command.key]; localStorage.setItem("__realisteSettings", JSON.stringify(settings)); } realisteIframe.contentWindow.postMessage( JSON.stringify({operation: 'update_settings', value: settings}), window.__realisteOrigin); const evnt = new CustomEvent('realisteStorageEvent', { detail: command }); document.querySelector(widgetTarget).dispatchEvent(evnt); } else { console.log('Invalid storage command', command); } } function initMessageListener() { window.addEventListener("message", (event) => { if(!window.__realisteOrigin && window.realisteIframe) try { window.__realisteOrigin = realisteIframe.src.split('/')[0] + '//'+realisteIframe.src.split('/')[2] } catch {} if (event.origin !== window.__realisteOrigin) return; var data = JSON.parse(event.data); processStorageCommand(data); }, false); } const iframeScale = () => { let banner = document.querySelector('#realisteIframe'); const iframeTop = banner.getBoundingClientRect().top; const windowHeight = window.innerHeight; const iframePaddingBottom = 20; const iframeMaxHeight = windowHeight - iframeTop - iframePaddingBottom; banner.style.transformOrigin = 'top center'; if (iframeMaxHeight > 575 || window.innerWidth < 1024 || iframeMaxHeight < 460) { banner.style.transform = 'scale(1)'; return; }; banner.style.transform = `scale(${iframeMaxHeight / 575})`; } function initFrame() { var banner = document.querySelector(widgetTarget); if(banner && banner.getAttribute('data-widget') && !banner.getAttribute('initialized')) { banner.setAttribute('initialized', 'true'); banner.style.setProperty('z-index', '2147483647', 'important'); banner.style.setProperty('position', 'relative', 'important'); var src = passUtmParametersAndProcessUrl(banner.getAttribute('data-widget')); var iframe = banner.querySelector('#realisteIframe'); if(!iframe){ iframe = document.createElement('iframe'); iframe.setAttribute('style', 'display: none;'); iframe.setAttribute('id', 'realisteIframe'); banner.innerHTML = ''; banner.appendChild(iframe); } iframe.setAttribute('style','width: 100%; max-width: 700px; height: 575px; min-height: 575px !important; min-width: 320px !important; border-radius: 5px; overflow: hidden; box-shadow: 0 0 30px rgb(0 0 0 / 40%); margin: 30px auto; display: block; transition-property: transform; transition-duration: 500ms;'); iframe.setAttribute('frameborder', '0'); iframe.setAttribute('src', src); if(!window.__realisteIframeScaleInterval) { setTimeout(iframeScale,1000); window.__realisteIframeScaleInterval = setInterval(iframeScale, 3000); } banner.addEventListener('realisteStorageEvent', realisteStorageEventCallbacks) } } const realisteStorageEventCallbacks = function(event) { if (event.detail && event.detail.key == 'customParentParamsQuery' && event.detail.operation == 'set' && event.detail.value > '') { // если требуется передавать кастомные значения типа GoogleClientID и пр., ставим мониторинг на них (т.к мы не можем отследить момент инициализации) if(!window.__realisteCustomParentParamsQueryInterval) { window.__realisteCustomParentParamsQueryInterval = setInterval(function(){ parentParams = Function(event.detail.value)().toString(); processStorageCommand({operation: 'set', key: 'customDataParentParams', value: parentParams}); }, 1000); } } // колбек для внешних апи if (event.detail && event.detail.key == 'currentClaimDetails' && event.detail.operation == 'set') { const evnt = new CustomEvent('claimDetailsSet', { detail: event.detail.value }); document.querySelector(widgetTarget).dispatchEvent(evnt); } // колбек для событий хранилища плагина const storageEventCallbackForParent = processStorageCommand({operation: 'get', key: 'storageEventCallbackForParent'}); if((typeof storageEventCallbackForParent == "string") && (storageEventCallbackForParent.trim() !== "")) { try { Function("storageEvent", storageEventCallbackForParent)(event.detail); } catch(e) { console.log("Realiste storageEventCallbackForParent invocation error", e); } } } const changeMouseEvent = () => { window.addEventListener("message", (event) => { const data = JSON.parse(event.data); if (data.action == 'disable_mouse_event') { realisteIframe.style.pointerEvents = 'none'; } }) scrollStop(() => { realisteIframe.style.pointerEvents = 'auto'; }); } initMessageListener(); setInterval(initFrame, 1000); initFrame(); })();