(function(){ var _global_name = "external_consent"; var _host = "https://google-consent.mw.dynata.com" var _popupWindow = null; function noConsent(consentType,coronaSession,callbackFn){ var xhr = new XMLHttpRequest(); xhr.open('POST', _host+'/no-consent'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = function(){ if(callbackFn){ callbackFn() } }; xhr.send(JSON.stringify({ corona_session:coronaSession, consent_type:consentType, })); } function openPopup(consentType,coronaSession,callbackFn) { openConsentPopup({ callbackFn:callbackFn, consentType:consentType, coronaSession:coronaSession, target:"_blank", respondentId:null, }) } function openConsentPopup(opts){ if((!opts.callbackFn && !opts.returnUrl) || !opts.consentType || (!opts.coronaSession && !opts.respondentId)){ throw "consentType, (coronaSession or respondentId), (callbackFn or returnUrl) required"; return; } if(opts.returnUrl){ location.replace(_host+"/get-consent?respondentId="+opts.respondentId+"&consentType="+opts.consentType+"&returnUrl="+encodeURIComponent(opts.returnUrl)); return } //if the window is already open... if(_popupWindow != null){ _popupWindow.close(); } //open an empty window, will will set the location to the redirect url. //when need todo this now so we don't get a popup blocker var left = (screen.width/2)-(800/2); var top = (screen.height/2)-(600/2); if(opts.target) { _popupWindow = window.open(_host + '/start.html', opts.target); } else { _popupWindow = window.open(_host + '/start.html', "Consent", 'toolbar=no, menubar=no, copyhistory=no, scrollbars=no, width=800, height=600, top=' + top + ', left=' + left); } var xhr = new XMLHttpRequest(); var xhrOnLoad = function(){ if (xhr.status === 200) { var data = JSON.parse(xhr.responseText) //they already consented, just call the callback function. if(data.consented){ _popupWindow.close(); _popupWindow = null; opts.callbackFn(data); return; } //they have not consented, need to open a window and show the external consent var complete = false; _popupWindow.location = data.redirect_url; var callbackHandler = function receiveMessage(e) { // Validate the origin if (e.origin !== _host && e.origin !== 'https://google-consent.qa.mw.dynata.com') { console.log("Invalid host " + e.origin) return } complete = true ; if( _popupWindow != null) { _popupWindow.close(); _popupWindow = null; opts.callbackFn(e.data); } window.removeEventListener('message', callbackHandler) }; window.addEventListener("message",callbackHandler) //this will check to see if the user just closed the popup without going through the consent flow. var timer = setInterval(checkPopupWindowClose, 500); function checkPopupWindowClose() { if (_popupWindow == null){ clearInterval(timer); return; } if (_popupWindow.closed) { if(!complete){ opts.callbackFn({ closed_window:true, consented:false }) } _popupWindow = null; clearInterval(timer); window.removeEventListener('message', callbackHandler) } } } else { console.error("unknown external consent status",xhr); _popupWindow.close(); _popupWindow = null; opts.callbackFn({ closed_window:true, consented:false }) } } xhr.open('POST', _host+'/generate-consent-redirect'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = xhrOnLoad; xhr.send(JSON.stringify({ respondent_id:opts.respondentId, locale:opts.locale, corona_session:opts.coronaSession, consent_type:opts.consentType, })); } function revoke(opts){ if(!opts.consentType || !opts.coronaSession){ throw "consentType and coronaSession required"; return; } let xhr = new XMLHttpRequest(); xhr.open('POST', _host+'/revoke'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = function(){ if (xhr.status === 200) { var data = JSON.parse(xhr.responseText); opts.callbackFn(data); } else{ opts.callbackFn({ error:xhr.response, status:xhr.status }); } }; xhr.send(JSON.stringify({ locale:opts.locale, corona_session:opts.coronaSession, consent_type:opts.consentType, })); } function status(opts){ if(!opts.consentType || !opts.coronaSession){ throw "consentType and coronaSession required"; return; } let xhr = new XMLHttpRequest(); xhr.open('POST', _host+'/status'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = function(){ if (xhr.status === 200) { var data = JSON.parse(xhr.responseText) opts.callbackFn(data); } else{ console.error("unknown external consent status",xhr); opts.callbackFn({ error:xhr.response, status:xhr.status }); } }; xhr.send(JSON.stringify({ locale:opts.locale, corona_session:opts.coronaSession, consent_type:opts.consentType, })); } window[_global_name] = { openConsentPopup: openConsentPopup, openPopup: openPopup, noConsent: noConsent, revoke:revoke, status:status, } }());