Created
March 4, 2021 22:44
-
-
Save arturmkrtchyan/f64162cf42f365578d162cc37fb28d5d 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 () { | |
$.ajaxPrefilter(function(options, originalOptions, jqXHR) { | |
console.log(options); | |
if(options.url.includes('hooks.zapier.com')) { | |
const data = JSON.parse(originalOptions.data); | |
data['LOGGED_IN_USER_EMAIL'] = getUserEmail(); | |
options.data = JSON.stringify(data); | |
} | |
}); | |
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