Advertisement
fauzanjeg

JNews || Fix Recaptcha in Dark Mode

Jul 7th, 2025
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * JNews || Fix Recaptcha in Dark Mode
  3.  */
  4. (function ($) {
  5.     function render_recaptcha(mode) {
  6.         if (grecaptcha.render) {
  7.             $('.g-recaptcha').each(function () {
  8.                 const clonned = $(this).clone().empty();
  9.  
  10.                 if (clonned[0]) {
  11.                     clonned.insertAfter(this);
  12.                     $(this).remove();
  13.  
  14.                     grecaptcha.render(clonned[0], {
  15.                         sitekey: clonned.data('sitekey'),
  16.                         theme: mode
  17.                     });
  18.                 }
  19.             });
  20.         }
  21.     }
  22.  
  23.     // === OBSERVER ===
  24.     const targetNode = document.body;
  25.     const config = { attributes: true, attributeFilter: ['class'] };
  26.  
  27.     const observer = new MutationObserver(function (mutationsList) {
  28.         for (const mutation of mutationsList) {
  29.             if (mutation.type === 'attributes') {
  30.                 const isDark = document.body.classList.contains('jnews-dark-mode');
  31.  
  32.                 if (isDark) {
  33.                     render_recaptcha('dark');
  34.                 } else {
  35.                     render_recaptcha('light');
  36.                 }
  37.             }
  38.         }
  39.     });
  40.  
  41.     observer.observe(targetNode, config);
  42.  
  43.     // Optional: trigger at page load
  44.     if (document.body.classList.contains('jnews-dark-mode')) {
  45.         render_recaptcha('dark');
  46.     } else {
  47.         render_recaptcha('light');
  48.     }
  49. })(jQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement