npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@marmot-webdev/recaptcha

v1.0.0

Published

A module for dynamic loading, rendering, and validating Google reCAPTCHA v2 widgets (checkbox type) in web forms.

Downloads

8

Readme

@marmot-webdev/recaptcha

This module offers functionality for dynamic loading, rendering, and validating Google reCAPTCHA v2 widgets (checkbox type). It is designed to optimize page performance by loading and rendering reCAPTCHA widgets only when users interact with form elements.

Key features:

  1. Lazy Loading.
  2. Built-in Validation.
  3. Multiple Widgets Support.
  4. Flexibility and Small Size.

Installation

npm install @marmot-webdev/recaptcha

Usage

HTML

<form><div class="js-recaptcha"></div></form>

JS

import Recaptcha from '@marmot-webdev/recaptcha';

const recaptcha = new Recaptcha('.js-recaptcha', {
  sitekey: 'your-site-key'
});

Configuration options can also be set directly on the HTML elements using data attributes:

<div class="js-recaptcha" data-sitekey="your-site-key" data-validation-message="Confirm that you are a real person."></div>

CSS

/* Make sure that the parent of .g-recaptcha-checkbox has relative positioning. */

.g-recaptcha-checkbox {
  position: absolute;
  top: 36px;
  left: 20px;
  z-index: -1;
  opacity: 0;
}

Configuration

Here's a quick overview of the default configuration options:

{
  sitekey: null,
  theme: 'auto',
  size: 'auto',
  tabindex: 0,
  constraintValidation: true,
  validationMessage: 'Please confirm that you are not a robot.',
  hl: 'auto',
  url: 'https://www.google.com/recaptcha/api.js',
  callback: null,
  expiredCallback: null,
  errorCallback: null,
  invalidCallback: null,
  loadCallback: null,
  renderCallback: null,
  resetCallback: null
}

Any configuration option can be a function. It should return the appropriate value if it is not a callback (see example in the FAQ section).

sitekey

Type: String | Function
Default: null

Your site key. This is a required option.

theme

Type: String | Function
Default: auto

The color theme of the widget. Available values are light, dark, and auto. The auto value follows the user's system preference for light or dark mode.

size

Type: String | Function
Default: auto

The size of the widget. Available values are compact, normal, and auto. The auto value adjusts the size based on the viewport width.

tabindex

Type: Number | Function
Default: 0

The tabindex of the widget.

constraintValidation

Type: Boolean | Function
Default: true

Whether or not to use the Constraint Validation API, particularly for displaying validation messages.

validationMessage

Type: String | Function
Default: 'Please confirm that you are not a robot.'

Specifies the validation message that is shown when the reCAPTCHA checkbox is not checked and validation fails.

hl

Type: String | Function
Default: 'auto'

Sets the language code for localization of the reCAPTCHA widget.

url

Type: String | Function
Default: 'https://www.google.com/recaptcha/api.js'

The URL to load the reCAPTCHA API script. You can provide an alternative URL if needed.

callback(dataObject)

Type: Function
Default: null

This callback is executed when reCAPTCHA successfully validates a user's response.

expiredCallback(dataObject)

Type: Function
Default: null

This callback is executed when the reCAPTCHA response expires.

errorCallback(dataObject)

Type: Function
Default: null

This callback is executed when an error occurs during reCAPTCHA validation.

invalidCallback(dataObject)

Type: Function
Default: null

This callback is executed when the user response to the reCAPTCHA challenge is considered invalid.

loadCallback(dataObject)

Type: Function
Default: null

This callback is executed when the reCAPTCHA script is successfully loaded.

renderCallback(dataObject)

Type: Function
Default: null

This callback is executed when the reCAPTCHA widget is successfully rendered.

resetCallback(dataObject)

Type: Function
Default: null

This callback is executed when the reCAPTCHA is reset explicitly using the reset method.

FAQ

reCAPTCHA is not working. What am I doing wrong?

If your reCAPTCHA is not working, make sure that it is placed within a <form> element.

How do I customize the reCAPTCHA size based on the form width?

const recaptcha = new Recaptcha('.js-recaptcha', {
  size: (dataObject) => {
    const { width } = dataObject.form.getBoundingClientRect();

    if (width > 320) {
      return 'normal';
    }

    return 'compact';
  }
});

How do I customize the validation message on a multilingual site?

const recaptcha = new Recaptcha('.js-recaptcha', {
  validationMessage: getValidationMessage
});

function getValidationMessage() {
  const { lang } = document.documentElement;

  const messages = {
    'en-GB': 'Please confirm you\'re not a robot.',
    'es-ES': 'Por favor, confirma que no eres un robot.',
    'sv-SE': 'Vänligen bekräfta att du inte är en robot.',
    'uk-UA': 'Будь ласка, підтвердіть, що Ви не робот.'
  };

  return messages[lang] || messages['en-GB'];
}

How can I create a custom validation message?

const recaptcha = new Recaptcha('.js-recaptcha', {
  constraintValidation: false,
  renderCallback: ({ recaptcha }) => addMessage(recaptcha, 'Please confirm that you are not a robot.'),
  callback: ({ recaptcha }) => hideMessage(recaptcha, true),
  invalidCallback: ({ recaptcha }) => hideMessage(recaptcha, false)
});

function addMessage(recaptcha, message) {
  const container = `<p class="g-recaptcha-error" hidden>${message}</p>`;
  recaptcha.parentElement.insertAdjacentHTML('beforeend', container);
}

function hideMessage(recaptcha, shouldHide) {
  recaptcha.parentElement.querySelector('.g-recaptcha-error').hidden = shouldHide;
}

Copyright and license

Copyright (c) 2023—present, Serhii Babakov.

This code is provided under the MIT License.