Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fredriks authored Mar 14, 2017
2 parents 4343774 + a853512 commit 4312d0f
Show file tree
Hide file tree
Showing 16 changed files with 508 additions and 92 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ node_js:
before_script:
- npm install -g grunt-cli bower
- bower install
- "export CHROME_BIN=chromium-browser"
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"

script:
- grunt karma:ci

after_script:
- grunt coverage
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function (grunt) {
},
ci: {
configFile: 'karma.conf.js',
browsers: ['Chrome', 'Firefox', 'FirefoxNightly'],
browsers: ['Chrome_travis_ci', 'Firefox', 'FirefoxNightly'],
singleRun: true
}
},
Expand Down
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ AngularJS reCaptcha

[![Build Status](https://travis-ci.org/VividCortex/angular-recaptcha.svg?branch=master)](https://travis-ci.org/VividCortex/angular-recaptcha)
[![Coverage Status](https://coveralls.io/repos/VividCortex/angular-recaptcha/badge.svg?branch=master)](https://coveralls.io/r/VividCortex/angular-recaptcha?branch=master)
![image](https://img.shields.io/npm/dm/angular-recaptcha.svg)

Add a [reCaptcha](https://www.google.com/recaptcha/intro/index.html) to your [AngularJS](angularjs.org) project.

Expand Down Expand Up @@ -41,20 +42,7 @@ See [the demo file](demo/usage.html) for a quick usage example.

- First, you need to get a valid recaptcha key for your domain. Go to http://www.google.com/recaptcha.

- Include the reCaptcha [API](https://developers.google.com/recaptcha/docs/display#AJAX) using this script in your HTML:

```html
<script
src="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
async defer
></script>
```

As you can see, we are specifying a `onload` callback, which will notify the angular service once the api is ready for usage.

The `onload` callback name defaults to `vcRecaptchaApiLoaded`, but can be overridden by the service provider via `vcRecaptchaServiceProvider.setOnLoadFunctionName('myOtherFunctionName');`.

- Also include the vc-recaptcha script and make your angular app depend on the `vcRecaptcha` module.
- Include the vc-recaptcha script and make your angular app depend on the `vcRecaptcha` module.

```html
<script type="text/javascript" src="angular-recaptcha.js"></script>
Expand Down Expand Up @@ -131,9 +119,12 @@ You can optionally pass a __theme__ the captcha should use, as an html attribute
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
lang="---- language code ----"
></div>
```

**Language Codes**: https://developers.google.com/recaptcha/docs/language

In this case we are specifying that the captcha should use the theme named _light_.

Listeners
Expand All @@ -153,6 +144,7 @@ There are three listeners you can use with the directive, `on-create`, `on-succe
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
lang=""
></div>
```

Expand Down Expand Up @@ -205,19 +197,24 @@ myApp.config(function(vcRecaptchaServiceProvider){
vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')
vcRecaptchaServiceProvider.setSize('---- compact or normal ----')
vcRecaptchaServiceProvider.setType('---- audio or image ----')
vcRecaptchaServiceProvider.setLang('---- language code ----')
});
```

**Language Codes**: https://developers.google.com/recaptcha/docs/language

You can also set all of the values at once.

```javascript
myApp.config(function(vcRecaptchaServiceProvider){
vcRecaptchaServiceProvider.setDefaults({
key: '---- YOUR PUBLIC KEY GOES HERE ----',
theme: '---- light or dark ----',
stoken: '--- YOUR GENERATED SECURE TOKEN ---',
size: '---- compact or normal ----',
type: '---- audio or image ----'
key: '---- YOUR PUBLIC KEY GOES HERE ----',
theme: '---- light or dark ----',
stoken: '--- YOUR GENERATED SECURE TOKEN ---',
size: '---- compact or normal ----',
type: '---- audio or image ----',
lang: '---- language code ----'
});
});
```
Note: any value omitted will be undefined, even if previously set.
Expand All @@ -236,6 +233,7 @@ Differences with the old reCaptcha
Recent Changelog
----------------

- 3.0.0 - Removed the need to include the Google recaptcha api.
- 2.2.3 - Removed _cleanup_ after creating the captcha element.
- 2.0.1 - Fixed onload when using ng-route and recaptcha is placed in a secondary view.
- 2.0.0 - Rewritten service to support new reCaptcha
Expand Down
5 changes: 2 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-recaptcha",
"version": "3.0.4",
"version": "4.0.3",
"keywords": ["angular", "captcha", "recaptcha", "vividcortex", "human", "form", "validation", "signup", "security", "login"],
"main": "release/angular-recaptcha.js",
"ignore": [
Expand All @@ -14,7 +14,6 @@
"angular": "1.*"
},
"devDependencies": {
"angular-mocks": "~1.*",
"jquery": "~2.1.3"
"angular-mocks": "~1.*"
}
}
4 changes: 0 additions & 4 deletions demo/usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

<!-- Include AngularJS -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js"></script>

<!-- Include the JS ReCaptcha API -->
<script src="//www.google.com/recaptcha/api.js?render=explicit&onload=vcRecaptchaApiLoaded" async defer></script>

<!-- Include the ngReCaptcha directive -->
<script src="./angular-recaptcha.js"></script>

Expand Down
8 changes: 7 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ module.exports = function (config) {

// list of files / patterns to load in the browser
files: [
'bower_components/jquery/dist/jquery.min.js',
'bower_components/angular/angular.js',

'bower_components/angular-mocks/angular-mocks.js',

'src/module.js',
'src/*.js',

'tests/*.driver.js',
'tests/*_test.js'
],

Expand Down Expand Up @@ -70,6 +70,12 @@ module.exports = function (config) {
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS', 'Chrome', 'IE', 'Safari', 'Firefox', 'FirefoxNightly', 'ChromeCanary'],

customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
Expand Down
45 changes: 26 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"name": "angular-recaptcha",
"version": "3.0.4",
"version": "4.0.3",
"description": "An AngularJS module to ease usage of reCaptcha inside a form",
"author": "VividCortex",
"license": "MIT",
"homepage": "https://github.com/vividcortex/angular-recaptcha",
"contributors": [
{
"name" : "Eduardo Daniel Cuomo",
"email" : "[email protected]",
"url" : "https://github.com/reduardo7/angular-recaptcha"
}
],
"main": "index.js",
"repository": {
"type": "git",
Expand All @@ -14,23 +21,23 @@
"test": "grunt test"
},
"devDependencies": {
"bower": "^1.3.3",
"grunt": "~0.4.2",
"grunt-bump": "0.0.13",
"grunt-cli": "~0.1.11",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-uglify": "~0.4.0",
"grunt-karma": "^0.10.1",
"grunt-karma-coveralls": "^2.5.3",
"jasmine-core": "^2.2.0",
"karma": "^0.12.31",
"karma-chrome-launcher": "^0.1.7",
"karma-coverage": "^0.2.7",
"karma-firefox-launcher": "^0.1.4",
"karma-ie-launcher": "^0.1.5",
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^0.1.4",
"karma-safari-launcher": "^0.1.1"
"bower": "^1.8.0",
"grunt": "^1.0.1",
"grunt-bump": "^0.8.0",
"grunt-cli": "^1.2.0",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-jshint": "^1.1.0",
"grunt-contrib-uglify": "^2.0.0",
"grunt-karma": "^2.0.0",
"grunt-karma-coveralls": "^2.5.4",
"jasmine-core": "^2.5.2",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-firefox-launcher": "^1.0.0",
"karma-ie-launcher": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.2",
"karma-safari-launcher": "^1.0.0"
}
}
83 changes: 76 additions & 7 deletions release/angular-recaptcha.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* angular-recaptcha build:2016-07-19
* https://github.com/vividcortex/angular-recaptcha
* Copyright (c) 2016 VividCortex
* @license angular-recaptcha build:2017-02-02
* https://github.com/vividcortex/angular-recaptcha
* Copyright (c) 2017 VividCortex
**/

/*global angular, Recaptcha */
Expand Down Expand Up @@ -90,6 +90,15 @@
config.type = type;
};

/**
* Sets the reCaptcha language which will be used by default is not specified in a specific directive instance.
*
* @param lang The reCaptcha language.
*/
provider.setLang = function(lang){
config.lang = lang;
};

/**
* Sets the reCaptcha configuration values which will be used by default is not specified in a specific directive instance.
*
Expand All @@ -100,8 +109,8 @@
provider.onLoadFunctionName = onLoadFunctionName;
};

provider.$get = ['$rootScope','$window', '$q', function ($rootScope, $window, $q) {
var deferred = $q.defer(), promise = deferred.promise, recaptcha;
provider.$get = ['$rootScope','$window', '$q', '$document', function ($rootScope, $window, $q, $document) {
var deferred = $q.defer(), promise = deferred.promise, instances = {}, recaptcha;

$window.vcRecaptchaApiLoadedCallback = $window.vcRecaptchaApiLoadedCallback || [];

Expand Down Expand Up @@ -138,6 +147,13 @@
// Check if grecaptcha is not defined already.
if (ng.isDefined($window.grecaptcha)) {
callback();
} else {
// Generate link on demand
var script = $window.document.createElement('script');
script.async = true;
script.defer = true;
script.src = 'https://www.google.com/recaptcha/api.js?onload='+provider.onLoadFunctionName+'&render=explicit';
$document.find('body').append(script);
}

return {
Expand All @@ -156,12 +172,15 @@
conf.stoken = conf.stoken || config.stoken;
conf.size = conf.size || config.size;
conf.type = conf.type || config.type;
conf.hl = conf.lang || config.lang;

if (!conf.sitekey || conf.sitekey.length !== 40) {
throwNoKeyException();
}
return getRecaptcha().then(function (recaptcha) {
return recaptcha.render(elm, conf);
var widgetId = recaptcha.render(elm, conf);
instances[widgetId] = elm;
return widgetId;
});
},

Expand All @@ -171,13 +190,45 @@
reload: function (widgetId) {
validateRecaptchaInstance();

// $log.info('Reloading captcha');
recaptcha.reset(widgetId);

// Let everyone know this widget has been reset.
$rootScope.$broadcast('reCaptchaReset', widgetId);
},

/**
* Get/Set reCaptcha language
*/
useLang: function (widgetId, lang) {
var instance = instances[widgetId];

if (instance) {
var iframe = instance.querySelector('iframe');
if (lang) {
// Setter
if (iframe && iframe.src) {
var s = iframe.src;
if (/[?&]hl=/.test(s)) {
s = s.replace(/([?&]hl=)\w+/, '$1' + lang);
} else {
s += ((s.indexOf('?') === -1) ? '?' : '&') + 'hl=' + lang;
}

iframe.src = s;
}
} else {
// Getter
if (iframe && iframe.src && /[?&]hl=\w+/.test(iframe.src)) {
return iframe.src.replace(/.+[?&]hl=(\w+)([^\w].+)?/, '$1');
} else {
return null;
}
}
} else {
throw new Error('reCaptcha Widget ID not exists', widgetId);
}
},

/**
* Gets the response from the reCaptcha widget.
*
Expand All @@ -189,6 +240,20 @@
validateRecaptchaInstance();

return recaptcha.getResponse(widgetId);
},

/**
* Gets reCaptcha instance and configuration
*/
getInstance: function (widgetId) {
return instances[widgetId];
},

/**
* Destroy reCaptcha instance.
*/
destroy: function (widgetId) {
delete instances[widgetId];
}
};

Expand All @@ -215,6 +280,7 @@
theme: '=?',
size: '=?',
type: '=?',
lang: '=?',
tabindex: '=?',
required: '=?',
onCreate: '&',
Expand Down Expand Up @@ -247,6 +313,7 @@
stoken: scope.stoken || attrs.stoken || null,
theme: scope.theme || attrs.theme || null,
type: scope.type || attrs.type || null,
lang: scope.lang || attrs.lang || null,
tabindex: scope.tabindex || attrs.tabindex || null,
size: scope.size || attrs.size || null,
'expired-callback': expired
Expand Down Expand Up @@ -299,6 +366,8 @@
}

function cleanup(){
vcRecaptcha.destroy(scope.widgetId);

// removes elements reCaptcha added.
ng.element($document[0].querySelectorAll('.pls-container')).parent().remove();
}
Expand Down
Loading

0 comments on commit 4312d0f

Please sign in to comment.