This commit is contained in:
Anthony Stirling
2025-01-08 16:58:32 +00:00
parent a4afe5b708
commit 198fc1ced3
2 changed files with 36 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
document.addEventListener('DOMContentLoaded', function() {
// Get CSRF token from cookie
const getCsrfToken = () => {
return document.cookie
.split('; ')
.find(row => row.startsWith('XSRF-TOKEN='))
?.split('=')[1];
};
// Function to decode the URI-encoded cookie value
const decodeCsrfToken = (token) => {
if (token) {
return decodeURIComponent(token);
}
return null;
};
// Find all forms and add CSRF token
const forms = document.querySelectorAll('form');
forms.forEach(form => {
// Remove any existing CSRF input fields
const existingCsrfInputs = form.querySelectorAll('input[name="_csrf"]');
existingCsrfInputs.forEach(input => input.remove());
// Create and add new CSRF input field
const csrfToken = decodeCsrfToken(getCsrfToken());
if (csrfToken) {
const csrfInput = document.createElement('input');
csrfInput.type = 'hidden';
csrfInput.name = '_csrf';
csrfInput.value = csrfToken;
form.appendChild(csrfInput);
}
});
});

View File

@@ -73,6 +73,7 @@
<script th:src="@{'/js/cacheFormInputs.js'}" th:if="${currentPage != 'home'}"></script>
<script th:src="@{'/js/tab-container.js'}"></script>
<script th:src="@{'/js/darkmode.js'}"></script>
<script th:src="@{'/js/csrf.js'}"></script>
<script th:inline="javascript">
const stirlingPDFLabel = /*[[${@StirlingPDFLabel}]]*/ '';
const analyticsEnabled = /*[[${@analyticsEnabled}]]*/ false;