Skip to content

Commit f767cfc

Browse files
authored
Merge pull request #3 from dachcom-digital/friendly_captcha
Friendly captcha
2 parents 6c585b6 + 5c8ea52 commit f767cfc

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ npm i jquery-pimcore-formbuilder
1414
- [Conditional Logic Extension](./docs/02_conditionalLogic.md)
1515
- [Repeater Extension](./docs/03_repeater.md)
1616
- [Tracker Extension](./docs/04_tracker.md)
17-
- [reCAPTCHA V3 Extension](./docs/05_recaptchaV3.md)
17+
- [reCAPTCHA V3 Extension](./docs/05_1_recaptchaV3.md)
18+
- [Friendly Captcha Extension](./docs/05_2_friendlyCaptcha.md)
1819

1920
## Upgrade Notes
2021

22+
### 1.1.0
23+
- **[NEW FEATURE]**: Add FriendlyCaptcha Component
24+
2125
### 1.0.1
2226
- Allow multiple Instances [#1](https://github.com/dachcom-digital/jquery-pimcore-formbuilder/issues/1)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Project: PIMCORE FormBuilder
3+
* Extension: Friendly Captcha Injector
4+
* Since: 1.1.0
5+
* Author: DACHCOM.DIGITAL
6+
* License: GPLv3
7+
8+
*/
9+
(function (global, factory) {
10+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jquery'), window) :
11+
typeof define === 'function' && define.amd ? define(['exports', 'jquery', 'window'], factory) :
12+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory({}, global.jQuery, window));
13+
}(this, (function (exports, $, window) {
14+
'use strict';
15+
16+
var clName = 'FriendlyCaptcha';
17+
18+
function FriendlyCaptcha(form, options) {
19+
this.siteKey = null;
20+
this.dataSolutionFieldName = null;
21+
this.start = null;
22+
this.lang = null;
23+
this.callback = null;
24+
this.puzzleEndpoint = null;
25+
this.$form = $(form);
26+
this.options = $.extend({}, $.fn.formBuilderFriendlyCaptcha.defaults, options);
27+
this.init();
28+
}
29+
30+
$.extend(FriendlyCaptcha.prototype, {
31+
32+
init: function () {
33+
34+
this.$friendlyCaptchaField = this.$form.find(this.options.friendlyCaptchaFieldClass);
35+
36+
if (this.$friendlyCaptchaField.length === 0) {
37+
return;
38+
}
39+
40+
if (this.$friendlyCaptchaField.length > 1) {
41+
alert('Form has invalid amount of friendly captcha fields. There should be only one dedicated captcha field!');
42+
return;
43+
}
44+
45+
this.siteKey = this.$friendlyCaptchaField.data('sitekey');
46+
this.dataSolutionFieldName = this.$friendlyCaptchaField.data('data-solution-field-name');
47+
this.start = this.$friendlyCaptchaField.data('start');
48+
this.lang = this.$friendlyCaptchaField.data('lang');
49+
this.callback = this.$friendlyCaptchaField.data('callback');
50+
this.puzzleEndpoint = this.$friendlyCaptchaField.data('puzzle-endpoint');
51+
52+
this.$form.on('formbuilder.success', this.onReset.bind(this));
53+
this.$form.on('formbuilder.fail', this.onReset.bind(this));
54+
55+
this.bindDependency();
56+
},
57+
58+
bindDependency: function () {
59+
60+
var widgetUrl = 'https://cdn.jsdelivr.net/npm/friendly-challenge/widget.min.js';
61+
62+
if (this.options.autoWidgetVersionToLoad !== null) {
63+
widgetUrl = `https://cdn.jsdelivr.net/npm/friendly-challenge@${this.options.autoWidgetVersionToLoad}/widget.min.js`;
64+
}
65+
66+
if (this.options.useAutoWidget === false) {
67+
68+
if (typeof this.options.setupField === 'function') {
69+
this.options.setupField(this.$friendlyCaptchaField, this.$form, {
70+
sitekey: this.siteKey,
71+
dataSolutionFieldName: this.dataSolutionFieldName,
72+
start: this.start,
73+
lang: this.lang,
74+
callback: this.callback,
75+
puzzleEndpoint: this.puzzleEndpoint,
76+
});
77+
}
78+
79+
return;
80+
}
81+
82+
if (typeof window.friendlyChallenge !== 'undefined') {
83+
return;
84+
}
85+
86+
$.getScript(widgetUrl);
87+
},
88+
89+
onReset: function () {
90+
91+
if (typeof window.friendlyChallenge === 'undefined') {
92+
return;
93+
}
94+
95+
if (this.$friendlyCaptchaField.length === 0) {
96+
return;
97+
}
98+
99+
window.friendlyChallenge.autoWidget.reset();
100+
}
101+
});
102+
103+
$.fn.formBuilderFriendlyCaptcha = function (options) {
104+
this.each(function () {
105+
if (!$.data(this, 'fb-' + clName)) {
106+
$.data(this, 'fb-' + clName, new FriendlyCaptcha(this, options));
107+
}
108+
});
109+
return this;
110+
};
111+
112+
$.fn.formBuilderFriendlyCaptcha.defaults = {
113+
useAutoWidget: true,
114+
autoWidgetVersionToLoad: null,
115+
setupField: null,
116+
friendlyCaptchaFieldClass: 'div.form-builder-friendly-captcha',
117+
};
118+
})));
File renamed without changes.

docs/05_2_friendlyCaptcha.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Friendly Captcha Component
2+
3+
This component will enable Friendly Captcha functionality on your form.
4+
5+
## Back-End Configuration
6+
First, you need to set up some server side configuration via form builder. Read more about it [here](https://github.com/dachcom-digital/pimcore-formbuilder/blob/master/docs/03_SpamProtection.md#friendly-captcha).
7+
8+
## Enable Component
9+
```js
10+
import 'jquery-pimcore-formbuilder/dist/jquery.fb.ext.friendly-captcha';
11+
```
12+
13+
```js
14+
document.addEventListener('DOMContentLoaded', () => {
15+
$('form.formbuilder').formBuilderFriendlyCaptcha();
16+
});
17+
```
18+
19+
## Default Options
20+
21+
```js
22+
{
23+
useAutoWidget: true,
24+
autoWidgetVersionToLoad: null, // change to explict version you want to load from cdn: e.g. 0.9.16, null means latest
25+
setupField: null,
26+
friendlyCaptchaFieldClass: 'div.form-builder-friendly-captcha'
27+
}
28+
29+
```
30+
## Extended Usage
31+
```js
32+
document.addEventListener('DOMContentLoaded', () => {
33+
$('form.formbuilder').formBuilderFriendlyCaptcha({
34+
useAutoWidget: false, // disable it to use your own implementation (see next option "setupField")
35+
setupField: function (element, options) {
36+
37+
const friendlyCaptionOptions = {
38+
doneCallback: function (solution) {
39+
console.log("CAPTCHA completed successfully, solution:", solution);
40+
},
41+
sitekey: options.sitekey,
42+
}
43+
44+
const widget = new WidgetInstance(element, friendlyCaptionOptions);
45+
46+
widget.start();
47+
48+
formBuilderForm.addEventListener('formbuilder.success', () => widget.reset());
49+
formBuilderForm.addEventListener('formbuilder.error', () => widget.reset());
50+
}
51+
});
52+
});
53+
```

0 commit comments

Comments
 (0)