window.cvtr = window.cvtr || {}; if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: ''); } cvtr.loader = function(){ var turl = getQueryVariable("turl"), wp = getQueryVariable("wp"), locale = getQueryVariable("locale"); var scriptTag = document.getElementById('cvtr-form-render'), element = document.getElementById(scriptTag.getAttribute('data-target')), guid = null, url = scriptTag.getAttribute('data-url'), ourl = window.location.origin, hostname = window.location.hostname, turl = turl ? turl : window.location.href ; var cookieName = "rm_c", cookieTime = (60 * 24 *10), expiry = new Date(); if (document.cookie.indexOf(cookieName + "=") >= 0) { guid = getCookie(cookieName); } else if (getUrlParameter(cookieName)) { guid = getUrlParameter(cookieName); } else { // Set a new cookie on the client domain expiry.setTime(expiry.getTime() + (cookieTime * 60 * 1000)); // Ten minutes guid = getGuid(); document.cookie = cookieName + "=" + guid + "; expires=" + expiry.toGMTString() + '; path=/; secure=true; SameSite=None'; } /** * Returns cookie by cookie name * * @param name * @returns {string | undefined} */ function getCookie(name){ var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length == 2) return parts.pop().split(";").shift(); } /** * Creates random GUID * * @returns {string} */ function getGuid(){ function s4(){ return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); } function getUrlParameter(name){ name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); } /** * Listens to events emitted from inside the iFrame */ window.addEventListener('message', function(message){ if (message.origin.indexOf('.cvtr.io') >= 0 && typeof message.data === 'string') { var messageBody = {}; try { messageBody = JSON.parse(message.data); } catch(e) {} if (typeof messageBody.type === 'string' && messageBody.type === 'unset_cookie') { var regex = new RegExp(cookieName+'\=.*', 'g'); if (document.cookie.match(regex) !== null) { document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;secure;path=/;SameSite=None"; window.location.reload(false); } else { parent.window.postMessage(message.data, getUrlParameter('ourl')); } } } }); if (url.indexOf('?') >= 0) { url += '&purl='+hostname+'&ourl='+ourl; } else { url += '?purl='+hostname+'&ourl='+ourl; } url +='&rm_c='+guid; url +='&turl='+ encodeURIComponent(turl); if (wp.length > 0) { url += '&wp='+wp; } if (locale.length > 0) { url += '&locale='+locale; } var lpAttributes = element.getAttribute('data-attributes'); if (lpAttributes !== null && lpAttributes.length > 0) { lpAttributes = JSON.parse(lpAttributes); } else { lpAttributes = {}; } lpAttributes.src = url; lpAttributes.id = 'cvtrIframe'; if (lpAttributes.width === undefined) { lpAttributes.width = '100%'; } if (lpAttributes.style === undefined) { lpAttributes.style = 'border:0;'; } if (lpAttributes.id === undefined) { lpAttributes.style = 'border:0;'; } if (lpAttributes.class === undefined) { lpAttributes.class = 'iframe-form'; } if (lpAttributes.allowTransparency === undefined) { lpAttributes.allowTransparency = 'true'; } if (lpAttributes.allowfullscreen === undefined) { lpAttributes.allowfullscreen = 'true'; } if (lpAttributes.webkitallowfullscreen === undefined) { lpAttributes.webkitallowfullscreen = 'true'; } if (lpAttributes.mozallowfullscreen === undefined) { lpAttributes.mozallowfullscreen = 'true'; } var iframe = document.createElement('iframe'); for (var name in lpAttributes) { iframe.setAttribute(name, lpAttributes[name]); } element.append(iframe); window.addEventListener('message', function(message){ if (message.origin.indexOf('.cvtr.io') >= 0 && typeof message.data === 'string') { var messageBody = {}; try { messageBody = JSON.parse(message.data); } catch(e) {} if (typeof messageBody.type !== 'undefined' && typeof messageBody.height === 'number' && messageBody.type == 'iframe_height') { document.getElementById('cvtrIframe').style.height = messageBody.height.toString() + 'px'; } } }); } window.onload = cvtr.loader; function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i