Socket
Socket
Sign inDemoInstall

vanilla-hcaptcha

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    vanilla-hcaptcha

Vanilla Web Component for hCaptcha. 0 dependencies. <1kb gzipped.


Version published
Maintainers
1
Created

Readme

Source

hCaptcha - Vanilla Web Component

hCaptcha logo

0 dependencies. <1kb gzipped. Integrates well with Vue.JS, React, Angular, etc.

Install

yarn add vanilla-hcaptcha
<script src="https://cdn.jsdelivr.net/npm/vanilla-hcaptcha"></script>

Usage

Being a vanilla web component, it is relatively easy to integrate in mainstream web frameworks such as: React, Vue.js, Angular, Stencil.js, etc. See below some examples.

Vue.JS

Example: display invisible hCaptcha and render programmatically.

  1. Import once in application (main.js). Ignore the custom element.

    import "vanilla-hcaptcha";
    
    Vue.config.ignoredElements = [
      "h-captcha"
    ];
    
  2. Add handling methods

    methods: {
        onCaptchaLoaded(e) {
          console.log("Captcha is loaded");
          e.target.render(); // Show captcha programmatically
        },
        onCaptchaVerified(e) {
          console.log("Captcha is verified", { key: e.key });
        }
    }
    
  3. Integrate in your vue component

    <template>
        ...
        <h-captcha site-key="10000000-ffff-ffff-ffff-000000000001"
                   size="invisible"
                   @loaded="onCaptchaLoaded"
                   @verified="onCaptchaVerified"></h-captcha>
        ...
    </template>
    

React.JS

Example: display normal size hCaptcha with dark theme.

  1. Import once in application (index.js).

    import 'vanilla-hcaptcha';
    
  2. Add event handler after mount

    componentDidMount() {
        const signupCaptcha = document.getElementById('signupCaptcha');
        signupCaptcha.addEventListener('verified', (e) => {
          console.log('verified event', { key: e.key });
        });
    }
    
  3. Integrate in your html template

     <h-captcha id="signupCaptcha"
                site-key="10000000-ffff-ffff-ffff-000000000001"
                size="normal"
                dark
                onVerified="onCaptchaVerified"></h-captcha>
    

Angular

Example: display default hCaptcha.

  1. Import once in application (main.ts).

    import 'vanilla-hcaptcha';
    
  2. Add CUSTOM_ELEMENTS_SCHEMA to @NgModule.schemas

  3. Integrate in your html template

     <h-captcha [attr.site-key]="siteKey"
                (verified)="onCaptchaVerified($event)"></h-captcha>
     
    

Vanilla.JS

Example: display normal size hCaptcha accessible by keyboard (see tabindex).

  1. <script src="https://cdn.jsdelivr.net/npm/vanilla-hcaptcha"></script>
    
    <h-captcha id="signupCaptcha"
               site-key="10000000-ffff-ffff-ffff-000000000001"
               size="normal"
               tabindex="0"></h-captcha>
    
    <script>
        const signupCaptcha = document.getElementById('signupCaptcha');
        
        signupCaptcha.addEventListener('verified', (e) => {
            console.log('verified event', { key: e.key });
        });
        signupCaptcha.addEventListener('error', (e) => {
            console.log('error event', { error: e.error });
        });
    </script>
    

Docs

Attributes
AttributeDescription
site-keyThe site key generated in hCaptcha dashboard.
sizeOne of: normal, compact, invisible
darkTo use the dark theme
tabindexSet the tabindex of the hCaptcha popup.
Events
EventDataDescription
loadednullEmitted when component is ready to show captcha. This is necessary if window.hcaptcha is not already in the dom.
verifiedkey - verification key to be checked on backend sideEvent emitted after captcha was successfully verified.
expirednullEmitted when a captcha was verified but it expired.
errorerrorEmitted when an error happened.
Methods
MethodDescription
reset()Resets the hCaptcha which requires user to fill captcha again.
remove()Removes the component from DOM.

Develop & Test

yarn build
yarn test

Keywords

FAQs

Last updated on 01 Aug 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc