1.2.4 • Published 3 months ago

@hcaptcha/loader v1.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

hCaptcha Loader

This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling. It also includes a retry mechanism that will attempt to load the hCaptcha script several times in the event if fails to load due to a network or unforeseen issue.

hCaptcha is a drop-replacement for reCAPTCHA that protects user privacy.

Sign up at hCaptcha to get your sitekey today. You need a sitekey to use this library.

  1. Installation
  2. Implementation
  3. Props
  4. Legacy Support

Installation

npm install @hcaptcha/loader

Implementation

import { hCaptchaLoader } from '@hcaptcha/loader';

await hCaptchaLoader();

hcaptcha.render({
  sitekey: '<your_sitekey>'
});

const { response } = await hcaptcha.execute({ async: true });

Props

NameValues/TypeRequiredDefaultDescription
loadAsyncBooleanNotrueSet if the script should be loaded asynchronously.
cleanupBooleanNotrueRemove script tag after setup.
crossOriginStringNo-Set script cross origin attribute such as "anonymous".
scriptSourceStringNohttps://js.hcaptcha.com/1/api.jsSet script source URI. Takes precedence over secureApi.
scriptLocationHTMLElementNodocument.headLocation of where to append the script tag. Make sure to add it to an area that will persist to prevent loading multiple times in the same document view.
secureApiBooleanNofalseSee enterprise docs.
apihostStringNo-See enterprise docs.
assethostStringNo-See enterprise docs.
endpointStringNo-See enterprise docs.
hlStringNo-See enterprise docs.
hostStringNo-See enterprise docs.
imghostStringNo-See enterprise docs.
recaptchacompatStringNo-See enterprise docs.
reportapiStringNo-See enterprise docs.
sentryBooleanNo-See enterprise docs.
customBooleanNo-See enterprise docs.

Legacy Support

In order to support older browsers, a separate bundle is generated in which all ES6 code is compiled down to ES5 along with an optional polyfill bundle.

  • polyfills.js: Provides polyfills for features not supported in older browsers.
  • index.es5.js: @hcaptcha/loader package compiled for ES5 environments.

Import Bundle(s)

Both bundles generated use IIFE format rather than a more modern import syntax such as require or esm.

// Optional polyfill import
import '@hCaptcha/loader/dist/polyfills.js';
// ES5 version of hCaptcha Loader
import '@hCaptcha/loader/dist/index.es5.js';

hCaptchaLoader().then(function(hcaptcha) {
    var element = document.createElement('div');
    // hCaptcha API is ready
    hcaptcha.render(element, {
        sitekey: 'YOUR_SITE_KEY',
        // Additional options here
    });
});

TypeScript

To handle typescript with ES5 version, use the following statement.

declare global {
  interface Window {
    hCaptchaLoader: any;
  }
}

CDN

The hCaptcha Loader targeted for older browsers can also be imported via CDN by using UNPKG, see example below.

<!DOCTYPE html>
<head>
    <script type="text/javascript" src="https://unpkg.com/@hcaptcha/loader@latest/dist/polyfills.js"></script>
    <script type="text/javascript" src="https://unpkg.com/@hcaptcha/loader@latest/dist/index.es5.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    hCaptchaLoader().then(function(hcaptcha) {
        // hCaptcha API is ready
        hcaptcha.render('container', {
            sitekey: 'YOUR_SITE_KEY',
            // Additional options here
        });
    });
</script>
</body>
</html>
1.2.4

3 months ago

1.2.3

4 months ago

1.2.2

4 months ago

1.2.1

4 months ago

1.2.0

4 months ago

1.1.3

5 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.10

7 months ago

1.0.9

7 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago