Created
February 19, 2021 18:10
-
-
Save arturmkrtchyan/adc8f237e820dd006ee138c791eb758c to your computer and use it in GitHub Desktop.
Softr Add User Email and RecordID to a Form
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 jobIdFieldName = 'JobID'; | |
const formId = 'submitreferral'; | |
const userEmail = getUserEmail(); | |
const recordId = getUrlParam('recordId'); | |
$('#' + formId).append('<input class="sw-form-capture-element" id="sw-form-capture-Email" type="hidden" data-type="short_text" name="'+emailFieldName+'" value="'+userEmail+'">'); | |
$('#' + formId).append('<input class="sw-form-capture-element" id="sw-form-capture-JobID" type="hidden" data-type="short_text" name="'+jobIdFieldName+'" value="'+recordId+'">'); | |
$('#sw-form-capture-submit-btn').click(function() { | |
setTimeout(function(){ | |
$('#sw-form-capture-Email').val(userEmail); | |
$('#sw-form-capture-JobID').val(recordId); | |
}, 4000); | |
}); | |
function getUrlParam(name) { | |
const url = new URL(window.location.href); | |
return url.searchParams.get(name); | |
} | |
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