philomena/assets/js/captcha.ts

23 lines
719 B
TypeScript
Raw Normal View History

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
function insertCaptcha(_event: Event, target: HTMLInputElement) {
const { parentElement, dataset: { sitekey } } = target;
if (!parentElement) { return; }
2019-10-05 02:09:52 +02:00
2020-09-12 19:43:16 +02:00
const script = makeEl('script', {src: 'https://hcaptcha.com/1/api.js', async: true, defer: true});
const frame = makeEl('div', {className: 'h-captcha'});
frame.dataset.sitekey = sitekey;
2019-10-05 02:09:52 +02:00
clearEl(parentElement);
2020-09-12 19:43:16 +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() {
delegate(document, 'click', {'.js-captcha': leftClick(insertCaptcha)});
}