// Loading the data of the CTA that will be rendered on the page. var sniply = { ran: false, ctaData: JSON.parse(''), website: { id: '', referrer: '', should_have_cta: false, // Example value, replace accordingly snip_outbound_links: false // Example value, replace accordingly }, isMobileDevice: function() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); }, init: function() { console.log("sniply init called"); // Debug if (sniply.ran) return; sniply.ran = true; if (sniply.website.should_have_cta) { // Set CTA data on window window.ctaData = sniply.ctaData; // Create and add React CTA renderer root element to body var ctaRendererRootDiv = document.createElement('div'); ctaRendererRootDiv.id = "sniply-cta-root"; switch (sniply.ctaData.position) { case 'top': ctaRendererRootDiv.style.top = '0'; ctaRendererRootDiv.style.left = '0'; break; case 'top-right': ctaRendererRootDiv.style.top = '0'; ctaRendererRootDiv.style.right = '0'; break; case 'bottom': ctaRendererRootDiv.style.bottom = '0'; ctaRendererRootDiv.style.left = '0'; break; case 'bottom-right': ctaRendererRootDiv.style.bottom = '0'; ctaRendererRootDiv.style.right = '0'; break; default: console.warn("Unexpected CTA position value:", sniply.ctaData.position); break; } if (sniply.ctaData.theme === 'fullwidth') { ctaRendererRootDiv.style.width = '100vw'; } else if (sniply.ctaData.image_action !== null) { // Check if CTA type is an image // Set a default width and height for the CTA ctaRendererRootDiv.style.width = '100%'; ctaRendererRootDiv.style.height = '200px'; // example default height // Once the React app loads and renders the image, you can adjust the height. // This example assumes the image has an ID. If not, you might need another way to select it. var image = document.querySelector("#sniply-cta-root img"); if (image) { image.onload = function() { ctaRendererRootDiv.style.height = this.height + 'px'; }; } } // Common styles to make the CTA sticky. ctaRendererRootDiv.style.position = 'fixed'; ctaRendererRootDiv.style.zIndex = '1000'; document.body.appendChild(ctaRendererRootDiv); if (sniply.isMobileDevice() && sniply.ctaData.theme !== 'fullwidth') { // Get the viewport width in pixels const viewportWidth = window.innerWidth; // Calculate 95% of the viewport width const desiredWidth = Math.floor(viewportWidth * 0.95); // Using Math.floor to round down to whole pixel value // Set the width of the sniply-cta-root div if (ctaRendererRootDiv) { ctaRendererRootDiv.style.width = `${desiredWidth}px`; } } // Create and add script tag that loads and runs the React CTA renderer code var scriptElement = document.createElement('script'); scriptElement.src = ''; // Replace with actual path document.head.appendChild(scriptElement); } if (sniply.website.snip_outbound_links) { sniply.snip_outbound_links.init(); } }, snip_outbound_links: { init: function() { console.log("snip_outbound_links init called"); // Debug var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { anchors[i].addEventListener('click', sniply.snip_outbound_links.click_event, false); } }, click_event: function(evt) { if (sniply.snip_outbound_links) { evt.preventDefault(); var link = this.getAttribute('href'); var urlParam = 'url=' + encodeURIComponent(link); var siteidParam = 'siteid=' + encodeURIComponent(sniply.website.id || ''); var referrerParam = 'referrer=' + encodeURIComponent(sniply.website.referrer); var destination_url = 'https:///external-lib-redirect/v2/?' + urlParam + '&' + siteidParam + '&' + referrerParam; window.open(destination_url, this.getAttribute('target') || '_self'); } return false; }, should_snip: function (url) { if (url == null || url == undefined || url == "" || url.indexOf('tel:') == 0 || url.indexOf('mailto:') == 0 || url.indexOf('javascript:') == 0) { return false; } var parsed_uri = sniply.snip_outbound_links.get_qualified_url(url); var re = new RegExp('^https?\:\/\/[^\/?#]*' + sniply.snip_outbound_links.escape_reg_ex(sniply.snip_outbound_links.get_domain())); if (!re.test(parsed_uri)) { if (url.indexOf('snip=false') > -1) { return false } else { return true; } } else { return false; } }, get_qualified_url: function (url) { var div = document.createElement('div'); div.innerHTML = '<' + 'a href=' + url + "><" + "/a>"; return div.firstChild.href; }, escape_reg_ex: function (str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); }, get_domain: function () { var i=0,domain=document.domain,p=domain.split('.'),s='_gd'+(new Date()).getTime(); while(i<(p.length-1) && document.cookie.indexOf(s+'='+s)==-1) { domain = p.slice(-1-(++i)).join('.'); document.cookie = s+"="+s+";domain="+domain+";"; } document.cookie = s+"=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain="+domain+";"; return domain; } } }; // Make sure everything gets run document.addEventListener("DOMContentLoaded", sniply.init); window.setTimeout(function() { if (!sniply.ran) { sniply.init(); } }, 2000);