Home

Awesome

Codacy Code Climate

visualCaptcha-frontend-core

A JavaScript library for visualCaptcha front-end core files, for development and building the individual bower packages.

Installation

You need Node.js installed with npm.

Install dependencies with

npm install

Coding guidelines / Code style

View http://jscode.org/readable

Running the tests

grunt test

Building the library

grunt build

Usage

Initialization

  1. Include the visualCaptcha front-end core JS library and CSS styles into the HTML page:

    <!-- Somewhere inside the HEAD tag -->
    <link href="/path_to/visualcaptcha.css" media="all" rel="stylesheet">
    
    <!-- Somewhere inside the BODY tag, ideally at the bottom -->
    <script src="/path_to/visualcaptcha.js"></script>
    
  2. Create a visualCaptcha container into the HTML page:

    <div id="sample-captcha"></div>
    
  3. Initialize the visualCaptcha object with the visualCaptcha( element, options ) javascript function that returns a visualCaptcha object, where options is a JavaScript object with the visualCaptcha options:

    var captcha = visualCaptcha( 'sample-captcha', {
        imgPath: 'img/',
        captcha: {
            numberOfImages: 5,
                callbacks: {
                    loading: function( captcha ){
                    console.log( 'I am loading.', captcha );
                },
                loaded: function( captcha ){
                    console.log( 'I am loaded.', captcha );
                }
            }
        }
    });
    

visualCaptcha options

The JavaScript object of the visualCaptcha options can contain the following parameters:

visualCaptcha core options

The JavaScript object with the visualCaptcha core options can contain the following parameters:

visualCaptcha object methods

All the following methods are available from the visualCaptcha core object, that will be returned by the visualCaptcha( element, options ) function (as described above, in “Initialization”, step 3).

Initialization of multiple captchas on the same page

There are two fields: namespace and namespaceFieldName that you need to use, for creating multiple captchas. The namespace option can be loaded from the data-namespace attribute:

<form id="login-form">
    <!-- ... -->
    <div id="login-captcha" data-namespace="login"></div>
    <!-- ... -->
</form>

<form id="search-form">
    <!-- ... -->
    <div id="search-captcha" data-namespace="search"></div>
    <!-- ... -->
</form>

And the namespaceFieldName option can be loaded from the captcha options (this is the name of the parameter sent to the back-end, which you'll have to setup to use when initializing visualCaptcha's session):

var loginCaptcha = visualCaptcha( 'login-captcha', {
    captcha: {
        namespaceFieldName: 'myFieldName'
    }
});

var searchCaptcha = visualCaptcha( 'search-captcha', {
    captcha: {
        namespaceFieldName: 'myFieldName'
    }
});

Such configuration will create a hidden field in each form with a captcha, with the field name of namespaceFieldName and the field value of namespace, which you can then catch on the back-end.

Localization

When initializing visualCaptcha, send a language object. For example:

// Object with localized strings
var portugueseTexts = {
    accessibilityAlt: 'Ícone de Som',
    accessibilityTitle: 'Opção de Acessibilidade: ouça uma questão e responda à mesma!',
    accessibilityDescription: 'Escreva em baixo a <strong>resposta</strong> ao que ouve. Números ou palavras:',
    explanation: 'Clique ou toque no/a <strong>ANSWER</strong>',
    refreshAlt: 'Ícone de Refrescar/recarregar',
    refreshTitle: 'Refrescar/recarregar: obtenha novas imagens e opção de acessibilidade!'
};

var el = $( '#sample-captcha' ).visualCaptcha( {
    imgPath: 'img/',
    captcha: {
        numberOfImages: 5
    },
    language: portugueseTexts
});

// Use the following code to get the captcha object, with jQuery
var captcha = el.data( 'captcha' );

License

View the LICENSE file.