Created
March 4, 2021 20:30
-
-
Save arturmkrtchyan/fe896d02b3b9982d317d99540b898ba2 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 emailFieldName = 'email'; | |
const formId = 'form2'; | |
const userEmail = getUserEmail(); | |
$('#' + formId).append('<input class="sw-form-capture-element" id="sw-form-capture-Email" type="hidden" data-type="short_text" name="'+emailFieldName+'" value="'+userEmail+'">'); | |
$('#sw-form-capture-submit-btn').click(function() { | |
setTimeout(function(){ | |
$('#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