philomena/assets/js/captcha.ts

22 lines
756 B
TypeScript
Raw Normal View History

2024-06-09 22:30:21 +02:00
import { assertNotNull } from './utils/assert';
2020-09-12 19:43:16 +02:00
import { delegate, leftClick } from './utils/events';
import { clearEl, makeEl } from './utils/dom';
2019-10-05 02:09:52 +02:00
2024-06-09 22:30:21 +02:00
function insertCaptcha(_event: Event, target: HTMLInputElement) {
const parentElement = assertNotNull(target.parentElement);
2019-10-05 02:09:52 +02:00
2024-07-04 02:27:59 +02:00
const script = makeEl('script', { src: 'https://hcaptcha.com/1/api.js', async: true, defer: true });
const frame = makeEl('div', { className: 'h-captcha' });
2020-09-12 19:43:16 +02:00
2024-06-09 22:30:21 +02:00
frame.dataset.sitekey = target.dataset.sitekey;
2019-10-05 02:09:52 +02:00
2024-06-09 22:30:21 +02:00
clearEl(parentElement);
2020-09-12 19:43:16 +02:00
2024-06-09 22:30:21 +02:00
parentElement.insertAdjacentElement('beforeend', frame);
parentElement.insertAdjacentElement('beforeend', script);
2019-10-05 02:09:52 +02:00
}
2020-09-12 19:43:16 +02:00
export function bindCaptchaLinks() {
2024-07-04 02:27:59 +02:00
delegate(document, 'click', { '.js-captcha': leftClick(insertCaptcha) });
2020-09-12 19:43:16 +02:00
}