@hcaptcha/react-hcaptcha 1.8.0 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.js +22 -16
- package/dist/esm/utils.js +1 -1
- package/dist/index.js +21 -15
- package/dist/utils.js +1 -1
- package/package.json +1 -1
- package/src/index.js +18 -13
- package/src/utils.js +13 -12
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
2
2
|
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
|
|
3
3
|
import * as React from 'react';
|
|
4
|
-
import { generateQuery, getFrame, getMountElement } from
|
|
4
|
+
import { generateQuery, getFrame, getMountElement } from './utils.js';
|
|
5
5
|
var SCRIPT_ID = 'hcaptcha-api-script-id';
|
|
6
6
|
var HCAPTCHA_LOAD_FN_NAME = 'hcaptchaOnLoad'; // Prevent loading API script multiple times
|
|
7
7
|
|
|
@@ -58,8 +58,6 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
58
58
|
var _this;
|
|
59
59
|
|
|
60
60
|
_this = _React$Component.call(this, props) || this;
|
|
61
|
-
var element = getMountElement(_this.props.scriptLocation);
|
|
62
|
-
var frame = getFrame(element);
|
|
63
61
|
/**
|
|
64
62
|
* Internal reference to track hCaptcha API
|
|
65
63
|
*
|
|
@@ -67,7 +65,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
67
65
|
* not where the script and iFrames have been loaded.
|
|
68
66
|
*/
|
|
69
67
|
|
|
70
|
-
_this._hcaptcha =
|
|
68
|
+
_this._hcaptcha = undefined; // API Methods
|
|
71
69
|
|
|
72
70
|
_this.renderCaptcha = _this.renderCaptcha.bind(_assertThisInitialized(_this));
|
|
73
71
|
_this.resetCaptcha = _this.resetCaptcha.bind(_assertThisInitialized(_this));
|
|
@@ -82,11 +80,10 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
82
80
|
_this.handleOpen = _this.handleOpen.bind(_assertThisInitialized(_this));
|
|
83
81
|
_this.handleClose = _this.handleClose.bind(_assertThisInitialized(_this));
|
|
84
82
|
_this.handleChallengeExpired = _this.handleChallengeExpired.bind(_assertThisInitialized(_this));
|
|
85
|
-
var isApiReady = typeof _this._hcaptcha !== 'undefined';
|
|
86
83
|
_this.ref = /*#__PURE__*/React.createRef();
|
|
87
84
|
_this.apiScriptRequested = false;
|
|
88
85
|
_this.state = {
|
|
89
|
-
isApiReady:
|
|
86
|
+
isApiReady: false,
|
|
90
87
|
isRemoved: false,
|
|
91
88
|
elementId: props.id,
|
|
92
89
|
captchaId: ''
|
|
@@ -97,8 +94,13 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
97
94
|
var _proto = HCaptcha.prototype;
|
|
98
95
|
|
|
99
96
|
_proto.componentDidMount = function componentDidMount() {
|
|
97
|
+
var _this2 = this;
|
|
98
|
+
|
|
100
99
|
// Once captcha is mounted intialize hCaptcha - hCaptcha
|
|
101
|
-
var
|
|
100
|
+
var element = getMountElement(this.props.scriptLocation);
|
|
101
|
+
var frame = getFrame(element);
|
|
102
|
+
this._hcaptcha = frame.window.hcaptcha || undefined;
|
|
103
|
+
var isApiReady = typeof this._hcaptcha !== 'undefined';
|
|
102
104
|
/*
|
|
103
105
|
* Check if hCaptcha has already been loaded,
|
|
104
106
|
* If Yes, render the captcha
|
|
@@ -106,7 +108,11 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
106
108
|
*/
|
|
107
109
|
|
|
108
110
|
if (isApiReady) {
|
|
109
|
-
this.
|
|
111
|
+
this.setState({
|
|
112
|
+
isApiReady: true
|
|
113
|
+
}, function () {
|
|
114
|
+
_this2.renderCaptcha();
|
|
115
|
+
});
|
|
110
116
|
return;
|
|
111
117
|
}
|
|
112
118
|
|
|
@@ -136,18 +142,18 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
136
142
|
};
|
|
137
143
|
|
|
138
144
|
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
|
139
|
-
var
|
|
145
|
+
var _this3 = this;
|
|
140
146
|
|
|
141
147
|
// Prop Keys that could change
|
|
142
148
|
var keys = ['sitekey', 'size', 'theme', 'tabindex', 'languageOverride', 'endpoint']; // See if any props changed during component update
|
|
143
149
|
|
|
144
150
|
var match = keys.every(function (key) {
|
|
145
|
-
return prevProps[key] ===
|
|
151
|
+
return prevProps[key] === _this3.props[key];
|
|
146
152
|
}); // If they have changed, remove current captcha and render a new one
|
|
147
153
|
|
|
148
154
|
if (!match) {
|
|
149
155
|
this.removeCaptcha(function () {
|
|
150
|
-
|
|
156
|
+
_this3.renderCaptcha();
|
|
151
157
|
});
|
|
152
158
|
}
|
|
153
159
|
};
|
|
@@ -242,18 +248,18 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
242
248
|
};
|
|
243
249
|
|
|
244
250
|
_proto.handleOnLoad = function handleOnLoad() {
|
|
245
|
-
var
|
|
251
|
+
var _this4 = this;
|
|
246
252
|
|
|
247
253
|
this.setState({
|
|
248
254
|
isApiReady: true
|
|
249
255
|
}, function () {
|
|
250
|
-
var element = getMountElement(
|
|
256
|
+
var element = getMountElement(_this4.props.scriptLocation);
|
|
251
257
|
var frame = getFrame(element);
|
|
252
|
-
|
|
258
|
+
_this4._hcaptcha = frame.window.hcaptcha; // render captcha and wait for captcha id
|
|
253
259
|
|
|
254
|
-
|
|
260
|
+
_this4.renderCaptcha(function () {
|
|
255
261
|
// trigger onLoad if it exists
|
|
256
|
-
var onLoad =
|
|
262
|
+
var onLoad = _this4.props.onLoad;
|
|
257
263
|
if (onLoad) onLoad();
|
|
258
264
|
});
|
|
259
265
|
});
|
package/dist/esm/utils.js
CHANGED
|
@@ -14,7 +14,7 @@ function generateQuery(params) {
|
|
|
14
14
|
|
|
15
15
|
function getFrame(element) {
|
|
16
16
|
var doc = element && element.ownerDocument || document;
|
|
17
|
-
var win = doc.defaultView || doc.parentWindow;
|
|
17
|
+
var win = doc.defaultView || doc.parentWindow || window;
|
|
18
18
|
return {
|
|
19
19
|
document: doc,
|
|
20
20
|
window: win
|
package/dist/index.js
CHANGED
|
@@ -87,8 +87,6 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
87
87
|
|
|
88
88
|
(0, _classCallCheck2["default"])(this, HCaptcha);
|
|
89
89
|
_this = _super.call(this, props);
|
|
90
|
-
var element = (0, _utils.getMountElement)(_this.props.scriptLocation);
|
|
91
|
-
var frame = (0, _utils.getFrame)(element);
|
|
92
90
|
/**
|
|
93
91
|
* Internal reference to track hCaptcha API
|
|
94
92
|
*
|
|
@@ -96,7 +94,7 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
96
94
|
* not where the script and iFrames have been loaded.
|
|
97
95
|
*/
|
|
98
96
|
|
|
99
|
-
_this._hcaptcha =
|
|
97
|
+
_this._hcaptcha = undefined; // API Methods
|
|
100
98
|
|
|
101
99
|
_this.renderCaptcha = _this.renderCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
102
100
|
_this.resetCaptcha = _this.resetCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
@@ -111,11 +109,10 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
111
109
|
_this.handleOpen = _this.handleOpen.bind((0, _assertThisInitialized2["default"])(_this));
|
|
112
110
|
_this.handleClose = _this.handleClose.bind((0, _assertThisInitialized2["default"])(_this));
|
|
113
111
|
_this.handleChallengeExpired = _this.handleChallengeExpired.bind((0, _assertThisInitialized2["default"])(_this));
|
|
114
|
-
var isApiReady = typeof _this._hcaptcha !== 'undefined';
|
|
115
112
|
_this.ref = /*#__PURE__*/React.createRef();
|
|
116
113
|
_this.apiScriptRequested = false;
|
|
117
114
|
_this.state = {
|
|
118
|
-
isApiReady:
|
|
115
|
+
isApiReady: false,
|
|
119
116
|
isRemoved: false,
|
|
120
117
|
elementId: props.id,
|
|
121
118
|
captchaId: ''
|
|
@@ -126,8 +123,13 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
126
123
|
(0, _createClass2["default"])(HCaptcha, [{
|
|
127
124
|
key: "componentDidMount",
|
|
128
125
|
value: function componentDidMount() {
|
|
126
|
+
var _this2 = this;
|
|
127
|
+
|
|
129
128
|
// Once captcha is mounted intialize hCaptcha - hCaptcha
|
|
130
|
-
var
|
|
129
|
+
var element = (0, _utils.getMountElement)(this.props.scriptLocation);
|
|
130
|
+
var frame = (0, _utils.getFrame)(element);
|
|
131
|
+
this._hcaptcha = frame.window.hcaptcha || undefined;
|
|
132
|
+
var isApiReady = typeof this._hcaptcha !== 'undefined';
|
|
131
133
|
/*
|
|
132
134
|
* Check if hCaptcha has already been loaded,
|
|
133
135
|
* If Yes, render the captcha
|
|
@@ -135,7 +137,11 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
135
137
|
*/
|
|
136
138
|
|
|
137
139
|
if (isApiReady) {
|
|
138
|
-
this.
|
|
140
|
+
this.setState({
|
|
141
|
+
isApiReady: true
|
|
142
|
+
}, function () {
|
|
143
|
+
_this2.renderCaptcha();
|
|
144
|
+
});
|
|
139
145
|
return;
|
|
140
146
|
}
|
|
141
147
|
|
|
@@ -168,18 +174,18 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
168
174
|
}, {
|
|
169
175
|
key: "componentDidUpdate",
|
|
170
176
|
value: function componentDidUpdate(prevProps) {
|
|
171
|
-
var
|
|
177
|
+
var _this3 = this;
|
|
172
178
|
|
|
173
179
|
// Prop Keys that could change
|
|
174
180
|
var keys = ['sitekey', 'size', 'theme', 'tabindex', 'languageOverride', 'endpoint']; // See if any props changed during component update
|
|
175
181
|
|
|
176
182
|
var match = keys.every(function (key) {
|
|
177
|
-
return prevProps[key] ===
|
|
183
|
+
return prevProps[key] === _this3.props[key];
|
|
178
184
|
}); // If they have changed, remove current captcha and render a new one
|
|
179
185
|
|
|
180
186
|
if (!match) {
|
|
181
187
|
this.removeCaptcha(function () {
|
|
182
|
-
|
|
188
|
+
_this3.renderCaptcha();
|
|
183
189
|
});
|
|
184
190
|
}
|
|
185
191
|
}
|
|
@@ -279,18 +285,18 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
279
285
|
}, {
|
|
280
286
|
key: "handleOnLoad",
|
|
281
287
|
value: function handleOnLoad() {
|
|
282
|
-
var
|
|
288
|
+
var _this4 = this;
|
|
283
289
|
|
|
284
290
|
this.setState({
|
|
285
291
|
isApiReady: true
|
|
286
292
|
}, function () {
|
|
287
|
-
var element = (0, _utils.getMountElement)(
|
|
293
|
+
var element = (0, _utils.getMountElement)(_this4.props.scriptLocation);
|
|
288
294
|
var frame = (0, _utils.getFrame)(element);
|
|
289
|
-
|
|
295
|
+
_this4._hcaptcha = frame.window.hcaptcha; // render captcha and wait for captcha id
|
|
290
296
|
|
|
291
|
-
|
|
297
|
+
_this4.renderCaptcha(function () {
|
|
292
298
|
// trigger onLoad if it exists
|
|
293
|
-
var onLoad =
|
|
299
|
+
var onLoad = _this4.props.onLoad;
|
|
294
300
|
if (onLoad) onLoad();
|
|
295
301
|
});
|
|
296
302
|
});
|
package/dist/utils.js
CHANGED
|
@@ -31,7 +31,7 @@ function generateQuery(params) {
|
|
|
31
31
|
|
|
32
32
|
function getFrame(element) {
|
|
33
33
|
var doc = element && element.ownerDocument || document;
|
|
34
|
-
var win = doc.defaultView || doc.parentWindow;
|
|
34
|
+
var win = doc.defaultView || doc.parentWindow || window;
|
|
35
35
|
return {
|
|
36
36
|
document: doc,
|
|
37
37
|
window: win
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { generateQuery, getFrame, getMountElement } from
|
|
2
|
+
import { generateQuery, getFrame, getMountElement } from './utils.js';
|
|
3
3
|
|
|
4
4
|
const SCRIPT_ID = 'hcaptcha-api-script-id';
|
|
5
5
|
const HCAPTCHA_LOAD_FN_NAME = 'hcaptchaOnLoad';
|
|
@@ -8,7 +8,7 @@ const HCAPTCHA_LOAD_FN_NAME = 'hcaptchaOnLoad';
|
|
|
8
8
|
const scripts = [];
|
|
9
9
|
|
|
10
10
|
// Generate hCaptcha API script
|
|
11
|
-
const mountCaptchaScript = (params={}) => {
|
|
11
|
+
const mountCaptchaScript = (params = {}) => {
|
|
12
12
|
const element = getMountElement(params.scriptLocation);
|
|
13
13
|
delete params.scriptLocation;
|
|
14
14
|
|
|
@@ -52,16 +52,13 @@ class HCaptcha extends React.Component {
|
|
|
52
52
|
constructor (props) {
|
|
53
53
|
super(props);
|
|
54
54
|
|
|
55
|
-
const element = getMountElement(this.props.scriptLocation);
|
|
56
|
-
const frame = getFrame(element);
|
|
57
|
-
|
|
58
55
|
/**
|
|
59
56
|
* Internal reference to track hCaptcha API
|
|
60
57
|
*
|
|
61
58
|
* Required as window is relative to initialization in application
|
|
62
59
|
* not where the script and iFrames have been loaded.
|
|
63
60
|
*/
|
|
64
|
-
this._hcaptcha =
|
|
61
|
+
this._hcaptcha = undefined;
|
|
65
62
|
|
|
66
63
|
// API Methods
|
|
67
64
|
this.renderCaptcha = this.renderCaptcha.bind(this);
|
|
@@ -79,14 +76,11 @@ class HCaptcha extends React.Component {
|
|
|
79
76
|
this.handleClose = this.handleClose.bind(this);
|
|
80
77
|
this.handleChallengeExpired = this.handleChallengeExpired.bind(this);
|
|
81
78
|
|
|
82
|
-
|
|
83
|
-
const isApiReady = typeof this._hcaptcha !== 'undefined';
|
|
84
|
-
|
|
85
79
|
this.ref = React.createRef();
|
|
86
80
|
this.apiScriptRequested = false;
|
|
87
81
|
|
|
88
82
|
this.state = {
|
|
89
|
-
isApiReady,
|
|
83
|
+
isApiReady: false,
|
|
90
84
|
isRemoved: false,
|
|
91
85
|
elementId: props.id,
|
|
92
86
|
captchaId: ''
|
|
@@ -94,7 +88,11 @@ class HCaptcha extends React.Component {
|
|
|
94
88
|
}
|
|
95
89
|
|
|
96
90
|
componentDidMount () { // Once captcha is mounted intialize hCaptcha - hCaptcha
|
|
97
|
-
const
|
|
91
|
+
const element = getMountElement(this.props.scriptLocation);
|
|
92
|
+
const frame = getFrame(element);
|
|
93
|
+
this._hcaptcha = frame.window.hcaptcha || undefined;
|
|
94
|
+
|
|
95
|
+
const isApiReady = typeof this._hcaptcha !== 'undefined';
|
|
98
96
|
|
|
99
97
|
/*
|
|
100
98
|
* Check if hCaptcha has already been loaded,
|
|
@@ -102,7 +100,14 @@ class HCaptcha extends React.Component {
|
|
|
102
100
|
* If No, create script tag and wait to render the captcha
|
|
103
101
|
*/
|
|
104
102
|
if (isApiReady) {
|
|
105
|
-
this.
|
|
103
|
+
this.setState(
|
|
104
|
+
{
|
|
105
|
+
isApiReady: true
|
|
106
|
+
},
|
|
107
|
+
() => {
|
|
108
|
+
this.renderCaptcha();
|
|
109
|
+
}
|
|
110
|
+
);
|
|
106
111
|
|
|
107
112
|
return;
|
|
108
113
|
}
|
|
@@ -177,7 +182,7 @@ class HCaptcha extends React.Component {
|
|
|
177
182
|
sentry,
|
|
178
183
|
custom,
|
|
179
184
|
loadAsync,
|
|
180
|
-
scriptLocation
|
|
185
|
+
scriptLocation,
|
|
181
186
|
};
|
|
182
187
|
|
|
183
188
|
mountCaptchaScript(mountParams)
|
package/src/utils.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
function generateQuery(params) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
return Object.entries(params)
|
|
3
|
+
.filter(([key, value]) => value || value === false)
|
|
4
|
+
.map(([key, value]) => {
|
|
5
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
|
|
6
|
+
}).join("&");
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
function getFrame(element) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const doc = (element && element.ownerDocument) || document;
|
|
11
|
+
const win = doc.defaultView || doc.parentWindow || window;
|
|
12
|
+
|
|
13
|
+
return { document: doc, window: win };
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
function getMountElement(element) {
|
|
16
|
-
|
|
17
|
+
return element || document.head;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
generateQuery,
|
|
22
|
+
getFrame,
|
|
23
|
+
getMountElement
|
|
23
24
|
};
|