This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from DarkGhostHunter/master
Reworked frontend default script
- Loading branch information
Showing
3 changed files
with
82 additions
and
30 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Help me support this package | ||
|
||
ko_fi: DarkGhostHunter | ||
custom: ['https://paypal.me/darkghosthunter'] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,60 @@ | ||
<script src="https://www.google.com/recaptcha/api.js?render={{ $key }}&onload=captchavelCallback" defer></script> | ||
<script> | ||
// Start Captchavel Script | ||
window.captchavelCallback = function () { | ||
let site_key = "{{ $key }}"; | ||
const recaptchaSiteKey = "{{ $key }}"; | ||
if (site_key === '') { | ||
const recatpchaForms = Array.from(document.getElementsByTagName('form')) | ||
.filter(form => form.dataset.recaptcha === 'true'); | ||
recatpchaForms.forEach((form) => { | ||
if (! form.dataset.recaptchaDontPrevent) { | ||
form.addEventListener('submit', event => { | ||
if (form.recaptcha_unresolved) { | ||
event.preventDefault(); | ||
} | ||
}); | ||
} | ||
}) | ||
window.captchavelCallback = () => { | ||
if (recaptchaSiteKey === '') { | ||
console.error("You haven't set your Site Key for reCAPTCHA v3. Get it on https://g.co/recaptcha/admin."); | ||
return; | ||
} | ||
Array.from(document.getElementsByTagName('form')) | ||
.filter((form) => form.dataset.recaptcha === 'true') | ||
.forEach((form) => { | ||
let action = form.action.includes('://') ? (new URL(form.action)).pathname : form.action; | ||
form.addEventListener('submit', (event) => { | ||
event.preventDefault(); | ||
grecaptcha.execute(site_key, { | ||
action: action | ||
.substring(action.indexOf('?'), action.length) | ||
.replace(/[^A-z\/_]/gi, '') | ||
}).then((token) => { | ||
if (token) { | ||
let child = document.createElement('input'); | ||
child.setAttribute('type', 'hidden'); | ||
child.setAttribute('name', '_recaptcha'); | ||
child.setAttribute('value', token); | ||
form.appendChild(child); | ||
form.submit(); | ||
} | ||
}); | ||
}); | ||
function refreshToken(form) { | ||
form.recaptcha_unresolved = true; | ||
let action = form.action.includes('://') ? (new URL(form.action)).pathname : form.action; | ||
console.log('action: ' + action) | ||
grecaptcha.execute(recaptchaSiteKey, { | ||
action: action.substring(action.indexOf('?'), action.length).replace(/[^A-z\/_]/gi, '') | ||
}).then(token => { | ||
// Remove all inputs with an old reCAPTCHA token. | ||
form.removeChild( | ||
Array.from(form.getElementsByTagName('input')) | ||
.filter(input => input.name === '_recaptcha') | ||
); | ||
let child = document.createElement('input'); | ||
child.setAttribute('type', 'hidden'); | ||
child.setAttribute('name', '_recaptcha'); | ||
child.setAttribute('value', token); | ||
form.appendChild(child); | ||
form.recaptcha_unresolved = false; | ||
}); | ||
} | ||
recatpchaForms.forEach(form => { | ||
refreshToken(form); | ||
setInterval(() => refreshToken(form), 100 * 1000); | ||
}); | ||
// If the user hits history back or forward, we will forcefully refresh the token. | ||
window.addEventListener('popstate', () => { | ||
recatpchaForms.forEach(form => { | ||
refreshToken(form); | ||
}) | ||
}) | ||
}; | ||
// End Captchavel Script | ||
</script> |