Browse Source

Merge pull request #556 from mpanarin/11.0-imp-website_form_recaptcha

[IMP] 11.0 website_form_recaptcha improve JS
pull/563/head
Pedro M. Baeza 9 months ago
parent
commit
d581aac6ab
No account linked to committer's email address

+ 5
- 0
website_form_recaptcha/README.rst View File

@@ -25,6 +25,10 @@ To use this module, you need to:
* Set `website_form_recaptcha` to `True` on that model (similar to enabling forms)
* Add an element with the `o_website_form_recaptcha` class anywhere in the form

Captcha will try to use the language of your website. If it, for whatever
reason, can't find, it will default to google API and use the language of the
browser.

Look at `website_crm_recaptcha` module for example implementation


@@ -55,6 +59,7 @@ Contributors
------------

* Dave Lasley <dave@laslabs.com>
* Mykhailo Panarin <m.panarin@mobilunity.com>

Maintainer
----------

+ 1
- 1
website_form_recaptcha/__manifest__.py View File

@@ -4,7 +4,7 @@
{
"name": "Website Form - ReCaptcha",
"summary": 'Provides a ReCaptcha field for Website Forms',
"version": "11.0.1.0.0",
"version": "11.0.1.1.0",
"category": "Website",
"website": "https://github.com/OCA/website",
"author": "LasLabs, Odoo Community Association (OCA)",

+ 37
- 6
website_form_recaptcha/static/src/js/field_recaptcha.js View File

@@ -11,22 +11,53 @@ odoo.define('website_form_recaptcha.recaptcha', function (require) {

snippet_animation.registry.form_builder_send = form_builder_send.extend({

// https://developers.google.com/recaptcha/docs/language
captcha_languages: [
"ar", "af", "am", "hy", "az", "eu", "bn", "bg", "ca", "zh-HK",
"zh-CN", "zh-TW", "hr", "cs", "da", "nl", "en-GB", "en", "et",
"fil", "fi", "fr", "fr-CA", "gl", "ka", "de", "de-AT", "de-CH",
"el", "gu", "iw", "hi", "hu", "is", "id", "it", "ja", "kn", "ko",
"lo", "lv", "lt", "ms", "ml", "mr", "mn", "no", "fa", "pl", "pt",
"pt-BR", "pt-PT", "ro", "ru", "sr", "si", "sk", "sl", "es", "es-419",
"sw", "sv", "ta", "te", "th", "tr", "uk", "ur", "vi", "zu",
],
recaptcha_js_url: "https://www.google.com/recaptcha/api.js",
start: function () {
var self = this;
this._super();
this.$captchas = self.$('.o_website_form_recaptcha');
ajax.post('/website/recaptcha/', {}).then(
this.handle_captcha();
},
handle_captcha: function() {
var self = this;
return ajax.post('/website/recaptcha/', {}).then(
function (result) {
var data = JSON.parse(result);
self.$captchas.append($(
'<div class="g-recaptcha" data-sitekey="' +
data.site_key + '"></div>'
));
self.$captchas.append(self._get_captcha_elem(data));
if (self.$captchas.length) {
$.getScript('https://www.google.com/recaptcha/api.js');
$.getScript(self._get_captcha_script_url(data));
}
}
);
},
_get_captcha_elem: function (data) {
return $('<div/>', {
'class': 'g-recaptcha',
'data-sitekey': data.site_key
});
},
_get_captcha_script_url: function() {
var lang = $("html").attr("lang").replace("_", "-");
if (this.captcha_languages.includes(lang)) {
// lookup for specific localization (ie: fr-FR)
return this.recaptcha_js_url + "?hl=" + lang;
}
if (this.captcha_languages.includes(lang.slice(0, 2))) {
// fallback to main lang
return this.recaptcha_js_url + "?hl=" + lang.slice(0, 2);
}
// if both failed -> let google handle it itself (picks browser language)
return this.recaptcha_js_url;
}
});
});

Loading…
Cancel
Save