const tanaisYandexCaptchaOptions = { sitekey: "ysc1_GJAVZ07KlL5xo4HQEiUaunaG4AbeJts1k9pTEziocb15aa72", hl: "ru", test: false, invisible: false, shieldPosition: "bottom-right", hideShield: false, }; // Глобальная функция для переинициализации капчи window.reinitTanaisYandexCaptcha = function() { onloadTanaisYandexCaptchaInitialization(); if (tanaisYandexCaptchaOptions.invisible) { attachTanaisYandexCaptchaInvisibleExecutor(); } }; BX.ready(function () { BX.addCustomEvent("onAjaxSuccess", function() { window.reinitTanaisYandexCaptcha(); }); // Обработка открытия POPUP окон BX.addCustomEvent("onPopupShow", function(popup) { setTimeout(function() { window.reinitTanaisYandexCaptcha(); }, 100); }); }); $(document).ready(function() { window.reinitTanaisYandexCaptcha(); // Обработка динамического появления контента $(document).on('DOMNodeInserted', function(e) { var target = e.target; if ($(target).find('.tanais-yandexcaptcha').length > 0 || $(target).hasClass('tanais-yandexcaptcha')) { setTimeout(function() { window.reinitTanaisYandexCaptcha(); }, 50); } }); }); // Функция для привязки обработчиков к невидимой капче function attachTanaisYandexCaptchaInvisibleExecutor() { let forms = $("form:has(.tanais-yandexcaptcha)"); forms.each(function () { if ($(this).attr("data-added-submit-event-handler") !== "Y") { $(this).find('[type="submit"]').on("click", function (e) { let captchaToken = $(this).closest("form").find('.tanais-yandexcaptcha [name="smart-token"]'); if (captchaToken.val() === "") { e.preventDefault(); } let widgetId = $(this).closest("form").attr("data-yandex-captcha-id"); if (widgetId) { window.smartCaptcha.execute(widgetId); } }); $(this).attr("data-added-submit-event-handler", "Y"); } }); } // Основная функция инициализации капчи function onloadTanaisYandexCaptchaInitialization() { if (window.smartCaptcha) { // Находим все контейнеры капчи, которые еще не инициализированы let forms = $("form:has(.tanais-yandexcaptcha:not([data-initialized]))"); forms.each(function () { const captchaContainer = $(this).find(".tanais-yandexcaptcha"); // Проверяем, что контейнер существует и видим if (captchaContainer.length === 0 || !captchaContainer.is(':visible')) { return; } // Проверяем, не инициализирована ли уже капча в этом контейнере if (captchaContainer.attr('data-initialized') === 'true') { return; } let options = Object.assign({}, tanaisYandexCaptchaOptions); if (options.invisible) { options.callback = function(token) { BX.ready(() => { // Находим форму, к которой принадлежит эта капча let form = captchaContainer.closest('form'); if (form.length) { // Устанавливаем токен в скрытое поле form.find('[name="smart-token"]').val(token); // Отправляем форму form.submit(); } }); }; } try { let widgetId = window.smartCaptcha.render(captchaContainer.get(0), options); $(this).attr("data-yandex-captcha-id", widgetId); captchaContainer.attr('data-initialized', 'true'); console.log('Yandex Captcha initialized with ID:', widgetId); } catch (error) { console.error('Error initializing Yandex Captcha:', error); } }); } else { console.warn('Yandex SmartCaptcha not loaded yet'); } }