function reloadScript(src, id) {
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.src = src;
	script.id = id;
	head.appendChild(script);
	return script;
}

window.addEventListener("vvveb.loadUrl", function () {
	var elements;

	elements = document.querySelectorAll(".cf-turnstile");
	if (elements.length) {
		if (typeof turnstile == "undefined") {
			var script = document.getElementById("turnstile-js");
			if (script) {
				var js = reloadScript(script.src, script.id);
				js.onload = () => elements.forEach(e => turnstile.execute(e, {}));
				script.remove();
			}
		} else {
			elements.forEach(e => turnstile.execute(e, {}));
		}
	}

	elements = document.querySelectorAll(".h-captcha");
	if (elements.length) {
		if (typeof hcaptcha == "undefined") {
			var script = document.getElementById("hcaptcha-js");
			if (script) {
				var js = reloadScript(script.src, script.id);
				js.onload = () => elements.forEach(e => hcaptcha.render(e, {}));
				script.remove();
			}
		} else {
			elements.forEach(e => hcaptcha.render(e, {}));
		}
	}

	elements = document.querySelectorAll(".g-recaptcha");
	if (elements.length) {
		if (typeof grecaptcha == "undefined") {
			var script = document.getElementById("recaptcha-js");
			if (script) {
				var js = reloadScript(script.src, script.id);
				js.onload = () => elements.forEach(e => {
					try { grecaptcha.render(e, {}); } catch (err) {}
				});
				script.remove();
			}
		} else {
			elements.forEach(e => {
				try { grecaptcha.render(e, {}); } catch (err) {}
			});
		}
	}
});
