Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
GitHub - caiter/angular-recaptcha: Angular directive to add a reCaptcha widget to your form · GitHub
Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - caiter/angular-recaptcha: Angular directive to add a reCaptcha widget to your form · GitHub
Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - caiter/angular-recaptcha: Angular directive to add a reCaptcha widget to your form · GitHub
Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' GitHub - caiter/angular-recaptcha: Angular directive to add a reCaptcha widget to your form · GitHub
Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - caiter/angular-recaptcha: Angular directive to add a reCaptcha widget to your form · GitHub
Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - caiter/angular-recaptcha: Angular directive to add a reCaptcha widget to your form · GitHub
Skip to content

Repository files navigation

AngularJS reCaptcha

Build StatusCoverage Status

Add a reCaptcha to your AngularJS project.

Demo: http://vividcortex.github.io/angular-recaptcha/

Installation

Manual

Download the latest release.

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

See the demo file for a quick usage example.

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.
<scriptsrc="https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit"
asyncdefer></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.
<scripttype="text/javascript" src="angular-recaptcha.js"></script>
varapp=angular.module('myApp',['vcRecaptcha']);
  • After that, you can place a container for the captcha widget in your view, and call the vc-recaptcha directive on it like this:
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

Here, the key attribute is passed to the directive's scope, so you can use either a property in your scope or just a hardcoded string. Be careful to use your public key, not your private one.

Form Validation

By default, if placed in a form using formControl the captcha will need to be checked for the form to be valid. If the captcha is not checked (if the user has not checked the box or the check has expired) the form will be marked as invalid. The validation key is recaptcha. You can opt out of this feature by setting the required attribute to false or a scoped variable that will evaluate to false. Any other value, or omitting the attribute will opt in to this feature.

Response Validation

To validate this object from your server, you need to use the API described in the verify section. Validation is outside of the scope of this tool, since is mandatory to do that at the server side.

You can simple supply a value for ng-model which will be dynamically populated and cleared as the response becomes available and expires, respectfully. When you want the value of the response, you can grab it from the scoped variable that was passed to ng-model. It works just like adding ng-model to any other input in your form.

...
<formname="myForm" ng-submit="mySubmit(myFields)">
...
<divvc-recaptchang-model="myFields.myRecaptchaResponse"
></div>
...
</form>
...
 ...
$scope.mySubmit=function(myFields){console.log(myFields.myRecaptchaResponse);}...

Or you can programmatically get the response that you need to send to your server, use the method getResponse() from the vcRecaptchaService angular service. This method receives an optional argument widgetId, useful for getting the response of a specific reCaptcha widget (in case you render more than one widget). If no widget ID is provided, the response for the first created widget will be returned.

varresponse=vcRecaptchaService.getResponse(widgetId);// returns the string response

Using ng-model is recommended for normal use as the value is tied directly to the reCaptcha instance through the directive and there is no need to manage or pass a widgetId.

Other Parameters

You can optionally pass a theme the captcha should use, as an html attribute:

<divvc-recaptchang-model="gRecaptchaResponse"
theme="---- light or dark ----"
size="---- compact or normal ----"
type="'---- audio or image ----'"
key="'---- YOUR PUBLIC KEY GOES HERE ----'"
></div>

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

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It's called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It's called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It's called when the captcha response expires and the user needs to solve a new captcha.
<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
ng-model="gRecaptchaResponse"
on-create="setWidgetId(widgetId)"
on-success="setResponse(response)"
on-expire="cbExpiration()"
></div>

Example

app.controller('myController',['$scope','vcRecaptchaService',function($scope,recaptcha){$scope.setWidgetId=function(widgetId){// store the `widgetId` for future usage.// For example for getting the response with// `recaptcha.getResponse(widgetId)`.};$scope.setResponse=function(response){// send the `response` to your server for verification.};$scope.cbExpiration=function(){// reset the 'response' object that is on scope};}]);

Secure Token

If you want to use a secure token pass it along with the site key as an html attribute.

<divvc-recaptchakey="'---- YOUR PUBLIC KEY GOES HERE ----'"
stoken="'--- YOUR GENERATED SECURE TOKEN ---'"
></div>

Please note that you have to encrypt your token yourself with your private key upfront! To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application's config function. This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance. The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')vcRecaptchaServiceProvider.setTheme('---- light or dark ----')vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')vcRecaptchaServiceProvider.setSize('---- compact or normal ----')vcRecaptchaServiceProvider.setType('---- audio or image ----')});

You can also set all of the values at once.

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 ----'});

Note: any value omitted will be undefined, even if previously set.

Differences with the old reCaptcha

  • If you want to force a language, you'll need to add a hl parameter to the script of the reCaptcha API (?onload=onloadCallback&render=explicit&hl=es).
  • Parameter tabindex is no longer used by reCaptcha and its usage has no effect.
  • Access to the input text is no longer supported.
  • Challenge is no longer provided by reCaptcha. The response text is used along with the private key and user's IP address for verification.
  • Switching between image and audio is now handled by reCaptcha.
  • Help display is now handled by reCaptcha.

Recent Changelog

  • 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
  • 1.0.2 - added extra Recaptcha object methods to the service, i.e. switch_type, showhelp, etc.
  • 1.0.0 - the key attribute is now a scope property of the directive
  • Added the destroy() method to the service. Thanks to @endorama.
  • We added a different integration method (see demo/2.html) which is safer because it doesn't relies on a timeout on the reload event of the recaptcha. Thanks to @sboisse for reporting the issue and suggesting the solution.
  • The release is now built using GruntJS so if you were using the source files (the src directory) in your projects you should now use the files in the release directory.

About

Angular directive to add a reCaptcha widget to your form

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages