Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

captcha: allow for interception #967

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions Resources/Private/Build/JavaScript/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class PowermailForm {

initialize = function () {
const that = this;
// for checking on the external site if the powermail version supports intercepting ajax forms
window.PowerMailFormOverrideAjaxExists = true;
that.#formValidationListener();
that.#moreStepFormListener();
that.#ajaxSubmitListener();
Expand Down Expand Up @@ -37,38 +39,45 @@ class PowermailForm {
const url = form.getAttribute('action');
const formUid = form.getAttribute('data-powermail-form');
const redirectUri = form.getAttribute('data-powermail-ajax-uri');

that.#addProgressbar(form);

fetch(url, {body: new FormData(form), method: 'post'})
.then((resp) => resp.text())
.then(function(data) {
const parser = new DOMParser();
const htmlDocument = parser.parseFromString(data, 'text/html');
const section = htmlDocument.documentElement.querySelector('[data-powermail-form="' + formUid + '"]');
if (section !== null) {
const container = document.querySelector('[data-powermail-form="' + formUid + '"]')
.closest('.tx-powermail');
container.innerHTML = '';
container.appendChild(section);
} else {
// no form markup found try to redirect via javascript
if (redirectUri !== null) {
Utility.redirectToUri(redirectUri);
function submitIt() {
fetch(url, { body: new FormData(form), method: 'post' })
.then((resp) => resp.text())
.then(function (data) {
const parser = new DOMParser();
const htmlDocument = parser.parseFromString(data, 'text/html');
const section = htmlDocument.documentElement.querySelector('[data-powermail-form="' + formUid + '"]');
if (section !== null) {
const container = document.querySelector('[data-powermail-form="' + formUid + '"]')
.closest('.tx-powermail');
container.innerHTML = '';
container.appendChild(section);
} else {
// fallback if no location found (but will effect 2x submit)
form.submit();
// no form markup found try to redirect via javascript
if (redirectUri !== null) {
Utility.redirectToUri(redirectUri);
} else {
// fallback if no location found (but will effect 2x submit)
form.submit();
}
}
}

// Fire existing listener again
that.#ajaxSubmitListener();
that.#formValidationListener();
that.#moreStepFormListener();
that.#reloadCaptchaImages();
})
.catch(function(error) {
console.log(error);
});

// Fire existing listener again
that.#ajaxSubmitListener();
that.#formValidationListener();
that.#moreStepFormListener();
that.#reloadCaptchaImages();
})
.catch(function (error) {
console.log(error);
});
}
if (window.PowerMailFormOverrideAjax) {
window.PowerMailFormOverrideAjax().then(submitIt());
} else {
submitIt();
}
}
});
});
Expand Down