@hcaptcha/react-hcaptcha 1.6.1 → 1.7.0

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/README.md CHANGED
@@ -139,7 +139,8 @@ return <HCaptcha ref={captchaRef} onLoad={onLoad} sitekey={sitekey} {...props} /
139
139
  |`reportapi`|String|No|`-`|See enterprise docs.|
140
140
  |`sentry`|String|No|`-`|See enterprise docs.|
141
141
  |`custom`|Boolean|No|`-`|See enterprise docs.|
142
- |`loadAsync`|Boolean|No|`true`|Set if the script should be loaded asynchronously|
142
+ |`loadAsync`|Boolean|No|`true`|Set if the script should be loaded asynchronously.|
143
+ |`scriptLocation`|Element|No|`document.head`| Location of where to append the script tag. Make sure to add it to an area that will persist to prevent loading multiple times in the same document view. Note: If `null` is provided, the `document.head` will be used.|
143
144
 
144
145
  #### Events
145
146
 
package/dist/esm/index.js CHANGED
@@ -17,7 +17,11 @@ var mountCaptchaScript = function mountCaptchaScript(params) {
17
17
  params = {};
18
18
  }
19
19
 
20
- if (document.getElementById(SCRIPT_ID)) {
20
+ var parent = params.scriptLocation || document.head;
21
+ delete params.scriptLocation;
22
+ var doc = parent.ownerDocument || document;
23
+
24
+ if (doc.getElementById(SCRIPT_ID)) {
21
25
  // API was already requested
22
26
  return mountPromise;
23
27
  } // Create global onload callback
@@ -26,7 +30,7 @@ var mountCaptchaScript = function mountCaptchaScript(params) {
26
30
  window[HCAPTCHA_LOAD_FN_NAME] = resolveFn;
27
31
  var domain = params.apihost || "https://js.hcaptcha.com";
28
32
  delete params.apihost;
29
- var script = document.createElement("script");
33
+ var script = doc.createElement("script");
30
34
  script.id = SCRIPT_ID;
31
35
  script.src = domain + "/1/api.js?render=explicit&onload=" + HCAPTCHA_LOAD_FN_NAME;
32
36
  script.async = params.loadAsync !== undefined ? params.loadAsync : true;
@@ -38,7 +42,7 @@ var mountCaptchaScript = function mountCaptchaScript(params) {
38
42
 
39
43
  var query = generateQuery(params);
40
44
  script.src += query !== "" ? "&" + query : "";
41
- document.head.appendChild(script);
45
+ parent.appendChild(script);
42
46
  return mountPromise;
43
47
  };
44
48
 
@@ -148,7 +152,8 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
148
152
  reportapi = _this$props.reportapi,
149
153
  sentry = _this$props.sentry,
150
154
  custom = _this$props.custom,
151
- loadAsync = _this$props.loadAsync;
155
+ loadAsync = _this$props.loadAsync,
156
+ scriptLocation = _this$props.scriptLocation;
152
157
  var mountParams = {
153
158
  apihost: apihost,
154
159
  assethost: assethost,
@@ -160,7 +165,8 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
160
165
  reportapi: reportapi,
161
166
  sentry: sentry,
162
167
  custom: custom,
163
- loadAsync: loadAsync
168
+ loadAsync: loadAsync,
169
+ scriptLocation: scriptLocation
164
170
  };
165
171
  mountCaptchaScript(mountParams).then(this.handleOnLoad)["catch"](this.handleError);
166
172
  this.apiScriptRequested = true;
package/dist/index.js CHANGED
@@ -43,8 +43,11 @@ var mountPromise = new Promise(function (resolve, reject) {
43
43
 
44
44
  var mountCaptchaScript = function mountCaptchaScript() {
45
45
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
46
+ var parent = params.scriptLocation || document.head;
47
+ delete params.scriptLocation;
48
+ var doc = parent.ownerDocument || document;
46
49
 
47
- if (document.getElementById(SCRIPT_ID)) {
50
+ if (doc.getElementById(SCRIPT_ID)) {
48
51
  // API was already requested
49
52
  return mountPromise;
50
53
  } // Create global onload callback
@@ -53,7 +56,7 @@ var mountCaptchaScript = function mountCaptchaScript() {
53
56
  window[HCAPTCHA_LOAD_FN_NAME] = resolveFn;
54
57
  var domain = params.apihost || "https://js.hcaptcha.com";
55
58
  delete params.apihost;
56
- var script = document.createElement("script");
59
+ var script = doc.createElement("script");
57
60
  script.id = SCRIPT_ID;
58
61
  script.src = "".concat(domain, "/1/api.js?render=explicit&onload=").concat(HCAPTCHA_LOAD_FN_NAME);
59
62
  script.async = params.loadAsync !== undefined ? params.loadAsync : true;
@@ -65,7 +68,7 @@ var mountCaptchaScript = function mountCaptchaScript() {
65
68
 
66
69
  var query = (0, _utils.generateQuery)(params);
67
70
  script.src += query !== "" ? "&".concat(query) : "";
68
- document.head.appendChild(script);
71
+ parent.appendChild(script);
69
72
  return mountPromise;
70
73
  };
71
74
 
@@ -182,7 +185,8 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
182
185
  reportapi = _this$props.reportapi,
183
186
  sentry = _this$props.sentry,
184
187
  custom = _this$props.custom,
185
- loadAsync = _this$props.loadAsync;
188
+ loadAsync = _this$props.loadAsync,
189
+ scriptLocation = _this$props.scriptLocation;
186
190
  var mountParams = {
187
191
  apihost: apihost,
188
192
  assethost: assethost,
@@ -194,7 +198,8 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
194
198
  reportapi: reportapi,
195
199
  sentry: sentry,
196
200
  custom: custom,
197
- loadAsync: loadAsync
201
+ loadAsync: loadAsync,
202
+ scriptLocation: scriptLocation
198
203
  };
199
204
  mountCaptchaScript(mountParams).then(this.handleOnLoad)["catch"](this.handleError);
200
205
  this.apiScriptRequested = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hcaptcha/react-hcaptcha",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "types": "types/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/index.js CHANGED
@@ -14,7 +14,12 @@ const mountPromise = new Promise((resolve, reject) => {
14
14
 
15
15
  // Generate hCaptcha API script
16
16
  const mountCaptchaScript = (params={}) => {
17
- if (document.getElementById(SCRIPT_ID)) {
17
+ const parent = params.scriptLocation || document.head;
18
+ delete params.scriptLocation;
19
+
20
+ const doc = parent.ownerDocument || document;
21
+
22
+ if (doc.getElementById(SCRIPT_ID)) {
18
23
  // API was already requested
19
24
  return mountPromise;
20
25
  }
@@ -25,7 +30,7 @@ const mountCaptchaScript = (params={}) => {
25
30
  const domain = params.apihost || "https://js.hcaptcha.com";
26
31
  delete params.apihost;
27
32
 
28
- const script = document.createElement("script");
33
+ const script = doc.createElement("script");
29
34
  script.id = SCRIPT_ID;
30
35
  script.src = `${domain}/1/api.js?render=explicit&onload=${HCAPTCHA_LOAD_FN_NAME}`;
31
36
 
@@ -37,7 +42,7 @@ const mountCaptchaScript = (params={}) => {
37
42
  const query = generateQuery(params);
38
43
  script.src += query !== ""? `&${query}` : "";
39
44
 
40
- document.head.appendChild(script);
45
+ parent.appendChild(script);
41
46
  return mountPromise;
42
47
  };
43
48
 
@@ -143,7 +148,8 @@ class HCaptcha extends React.Component {
143
148
  reportapi,
144
149
  sentry,
145
150
  custom,
146
- loadAsync
151
+ loadAsync,
152
+ scriptLocation
147
153
  } = this.props;
148
154
  const mountParams = {
149
155
  apihost,
@@ -156,7 +162,8 @@ class HCaptcha extends React.Component {
156
162
  reportapi,
157
163
  sentry,
158
164
  custom,
159
- loadAsync
165
+ loadAsync,
166
+ scriptLocation
160
167
  };
161
168
 
162
169
  mountCaptchaScript(mountParams)
package/types/index.d.ts CHANGED
@@ -29,6 +29,7 @@ interface HCaptchaProps {
29
29
  id?: string;
30
30
  reCaptchaCompat?: boolean;
31
31
  loadAsync?: boolean;
32
+ scriptLocation?: HTMLElement | null;
32
33
  }
33
34
 
34
35
  interface ExecuteResponse {