const TOLSTOY = { IS_DEV: false, PUBLISH_ID: 'l8yu9sxhmf4y5' }; const DESKTOP_PADDING = 0; const MOBILE_PADDING = 0; const DESKTOP_WIDTH_BREAKPOINT = 840; const SHOULD_HIDE_INTEGRATIONS = { tolstoyHideGorgias: true, tolstoyHideIntercom: true, tolstoyHideTawkTo: true, tolstoyHideLiveChat: true, tolstoyHideHubSpot: true, tolstoyHideDrift: true, tolstoyHideZendesk: true, tolstoyHideHelpScout: true, tolstoyHideFreshChat: true, tolstoyHideReamaze: true, tolstoyHideVfChat: true }; const HIDE_ELEMENTS = [ '#shopify-section-footer', '#footer', '.shopify-section.sections-rich-text' ]; const handleHideGorgias = () => { if (window.GorgiasChat) { window.GorgiasChat.close?.(); document.head.insertAdjacentHTML( 'beforeend', `#gorgias-chat-container iframe#chat-button { visibility: hidden; display: none; }` ); } }; const handleHideIntercom = () => { const timeout = setTimeout(() => clearInterval(interval), 9000); const interval = setInterval(() => { if (window.Intercom) { window.Intercom('update', { hide_default_launcher: true }); clearInterval(interval); clearTimeout(timeout); } }, 100); }; const handleHideTawkTo = () => { if (window.Tawk_API) { window.Tawk_API.hideWidget(); } }; const handleHideLiveChat = () => { if (window.LiveChatWidget) { window.LiveChatWidget.call('hide'); } }; const handleHideHubSpot = () => { if (window.HubSpotConversations && window.HubSpotConversations.widget) { window.HubSpotConversations.widget.close(); } }; const handleHideDrift = () => { if (window.drift) { window.drift.api.widget.hide(); } }; const handleHideZendesk = () => { const timeout = setTimeout(() => clearInterval(interval), 5000); const interval = setInterval(() => { const zendekButton = document.querySelector(`[title='Button to launch messaging window']`); const zendeMessage = document.querySelector(`[title='Message from company']`); zendekButton?.style?.setProperty('display', 'none'); zendeMessage?.parentElement?.style?.setProperty('display', 'none'); if (zendekButton && zendeMessage) { clearInterval(interval); clearTimeout(timeout); } }, 100); if (window.zE) { window.zE('messenger', 'close'); } }; const handleHideHelpScout = () => { if (window.Beacon) { window.Beacon('config', { display: { zIndex: '0' } }); } }; const handleHideFreshChat = () => { if (window.fcWidget) { window.fcWidget.close(); window.fcWidget.hide(); } }; const handleHideReamaze = () => { if (window._support?.ui && window.Reamaze?.reload) { delete window._support.ui; window.Reamaze.reload(); } }; const handleHideVfChat = () => { hideElementBySelector('#vfChat+div'); hideElementBySelector('#vfChat+div+div'); }; const HIDE_INTEGRATIONS = { tolstoyHideGorgias: handleHideGorgias, tolstoyHideIntercom: handleHideIntercom, tolstoyHideTawkTo: handleHideTawkTo, tolstoyHideLiveChat: handleHideLiveChat, tolstoyHideHubSpot: handleHideHubSpot, tolstoyHideDrift: handleHideDrift, tolstoyHideZendesk: handleHideZendesk, tolstoyHideHelpScout: handleHideHelpScout, tolstoyHideFreshChat: handleHideFreshChat, tolstoyHideReamaze: handleHideReamaze, tolstoyHideVfChat: handleHideVfChat }; const handleHideIntegration = integrationKey => { if (HIDE_INTEGRATIONS[integrationKey]) { setTimeout(HIDE_INTEGRATIONS[integrationKey], 500); } }; const hideIntegrations = () => { for (const [integrationKey, shouldHide] of Object.entries(SHOULD_HIDE_INTEGRATIONS)) { if (shouldHide) { handleHideIntegration(integrationKey); } } }; const getIsMobile = () => { const match = window.matchMedia || window.msMatchMedia; if (!match) { return false; } const matchCoarse = match('(pointer:coarse)'); const matchWidth = match(`(max-width: ${DESKTOP_WIDTH_BREAKPOINT || 0}px)`); return matchCoarse.matches || matchWidth.matches; }; const getHeaderEnd = () => { const header = document.querySelector('#shopify-section-header') || document.querySelector('#header') || document.querySelector('#shopify-section-header-02'); if (!header) { return 0; } return header.offsetTop + header.offsetHeight; }; const getPadding = () => { const isMobile = getIsMobile(); if (MOBILE_PADDING && isMobile) { return MOBILE_PADDING; } if (DESKTOP_PADDING && !isMobile) { return DESKTOP_PADDING; } return 0; }; const styleIframeContainer = () => { const headerEnd = getHeaderEnd(); const tolstoyIframeContainer = document.querySelector('#tolstoy-container'); if (!tolstoyIframeContainer) { return; } tolstoyIframeContainer.style.top = headerEnd + 'px'; tolstoyIframeContainer.style.height = `calc(100% - ${headerEnd}px)`; const padding = getPadding(); if (padding) { tolstoyIframeContainer.style.padding = `${padding}px`; } }; const hideElementBySelector = selector => { const element = document.querySelector(selector); if (!element) { return; } element.style.display = 'none'; }; const getIframeSrc = () => { const baseUrl = TOLSTOY.IS_DEV ? 'http://localhost:3001' : 'https://player.gotolstoy.com'; const parentUrl = `${window.location.origin}${window.location.pathname}`.slice(0, 1999); return `${baseUrl}/feed/${TOLSTOY.PUBLISH_ID}?host&url=${parentUrl}&safari=${Boolean( window?.safari )}&playerType=tv`; }; const initTv = () => { const tolstoyIframe = document.querySelector('#tolstoy-tv'); if (!tolstoyIframe) { return; } styleIframeContainer(); tolstoyIframe.src = getIframeSrc(); HIDE_ELEMENTS.forEach(hideElementBySelector); hideIntegrations(); }; document.addEventListener('DOMContentLoaded', initTv, false);