Created
February 28, 2021 20:18
-
-
Save arturmkrtchyan/ae5f7fac86dffdd6bea83b87263c4a16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
document.addEventListener("DOMContentLoaded", function () { | |
const forms = ['form1', 'form2', 'form3', 'form4']; | |
const emailFieldName = 'Email'; | |
const userEmail = getUserEmail(); | |
forms.forEach(form => { | |
$('#' + form).append('<input class="sw-form-capture-element" id="sw-form-capture-Email" type="hidden" data-type="short_text" name="'+emailFieldName+'" value="'+userEmail+'">'); | |
$('#' + form + ' #sw-form-capture-submit-btn').click(function() { | |
setTimeout(function(){ | |
$('#' + form + ' #sw-form-capture-Email').val(userEmail); | |
}, 4000); | |
}); | |
}); | |
function getUserEmail() { | |
var token = getCookie('jwtToken'); | |
if (token) { | |
var userData = parseJwt(token); | |
if (userData && userData.sub) { | |
return userData.sub.toLowerCase(); | |
} | |
} | |
return ''; | |
} | |
function getCookie(cookieName) { | |
var name = cookieName + "="; | |
var decodedCookie = decodeURIComponent(document.cookie); | |
var ca = decodedCookie.split(';'); | |
for (var i = 0; i < ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
} | |
function parseJwt(token) { | |
var base64Url = token.split('.')[1]; | |
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | |
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) { | |
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); | |
}).join('')); | |
return JSON.parse(jsonPayload); | |
}; | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment