Advertisement
Guest User

index.js

a guest
Mar 11th, 2021
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  4.  
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6.  
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8.  
  9. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  10.  
  11. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  12.  
  13. var React = require('react');
  14.  
  15. // Borrowed from https://github.com/ai/nanoid/blob/3.0.2/non-secure/index.js
  16. // This alphabet uses `A-Za-z0-9_-` symbols. A genetic algorithm helped
  17. // optimize the gzip compression for this alphabet.
  18. var urlAlphabet = 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
  19.  
  20. var nanoid = function nanoid() {
  21. var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 21;
  22.  
  23. var id = '';
  24. // A compact alternative for `for (var i = 0; i < step; i++)`.
  25. var i = size;
  26. while (i--) {
  27. // `| 0` is more compact and faster than `Math.floor()`.
  28. id += urlAlphabet[Math.random() * 64 | 0];
  29. }
  30. return id;
  31. };
  32.  
  33. // Create script to init hCaptcha
  34. var onLoadListeners = [];
  35. var captchaScriptCreated = false;
  36.  
  37. // Generate hCaptcha API Script
  38. var CaptchaScript = function CaptchaScript(hl, reCaptchaCompat) {
  39. // Create global onload callback
  40. window.hcaptchaOnLoad = function () {
  41. // Iterate over onload listeners, call each listener
  42. onLoadListeners = onLoadListeners.filter(function (listener) {
  43. listener();
  44. return false;
  45. });
  46. };
  47.  
  48. var script = document.createElement("script");
  49. script.src = "https://hcaptcha.com/1/api.js?render=explicit&onload=hcaptchaOnLoad";
  50. script.async = true;
  51. if (hl) {
  52. script.src += '&hl=' + hl;
  53. }
  54. if (reCaptchaCompat === false) {
  55. script.src += '&recaptchacompat=off';
  56. }
  57.  
  58. document.head.appendChild(script);
  59. };
  60.  
  61. var HCaptcha = function (_React$Component) {
  62. _inherits(HCaptcha, _React$Component);
  63.  
  64. function HCaptcha(props) {
  65. _classCallCheck(this, HCaptcha);
  66.  
  67. var _this = _possibleConstructorReturn(this, (HCaptcha.__proto__ || Object.getPrototypeOf(HCaptcha)).call(this, props));
  68.  
  69. var _props$id = props.id,
  70. id = _props$id === undefined ? null : _props$id;
  71.  
  72. // API Methods
  73.  
  74. _this.renderCaptcha = _this.renderCaptcha.bind(_this);
  75. _this.resetCaptcha = _this.resetCaptcha.bind(_this);
  76. _this.removeCaptcha = _this.removeCaptcha.bind(_this);
  77.  
  78. // Event Handlers
  79. _this.handleOnLoad = _this.handleOnLoad.bind(_this);
  80. _this.handleSubmit = _this.handleSubmit.bind(_this);
  81. _this.handleExpire = _this.handleExpire.bind(_this);
  82. _this.handleError = _this.handleError.bind(_this);
  83.  
  84. var isApiReady = typeof hcaptcha !== 'undefined';
  85.  
  86. if (!isApiReady) captchaScriptCreated = false;
  87.  
  88. _this.state = {
  89. isApiReady: isApiReady,
  90. isRemoved: false,
  91. elementId: id || 'hcaptcha-' + nanoid(),
  92. captchaId: ''
  93. };
  94. return _this;
  95. }
  96.  
  97. _createClass(HCaptcha, [{
  98. key: 'componentDidMount',
  99. value: function componentDidMount() {
  100. //Once captcha is mounted intialize hCaptcha - hCaptcha
  101. var _props = this.props,
  102. languageOverride = _props.languageOverride,
  103. reCaptchaCompat = _props.reCaptchaCompat;
  104. var _state = this.state,
  105. isApiReady = _state.isApiReady,
  106. elementId = _state.elementId;
  107.  
  108.  
  109. if (!isApiReady) {
  110. //Check if hCaptcha has already been loaded, if not create script tag and wait to render captcha elementID - hCaptcha
  111.  
  112. if (!captchaScriptCreated) {
  113. // Only create the script tag once, use a global variable to track
  114. captchaScriptCreated = true;
  115. CaptchaScript(languageOverride, reCaptchaCompat);
  116. }
  117.  
  118. // Add onload callback to global onload listeners
  119. onLoadListeners.push(this.handleOnLoad);
  120. } else {
  121. this.renderCaptcha();
  122. }
  123. }
  124. }, {
  125. key: 'componentWillUnmount',
  126. value: function componentWillUnmount() {
  127. var _state2 = this.state,
  128. isApiReady = _state2.isApiReady,
  129. isRemoved = _state2.isRemoved,
  130. captchaId = _state2.captchaId;
  131.  
  132. if (!isApiReady || isRemoved) return;
  133.  
  134. // Reset any stored variables / timers when unmounting
  135. hcaptcha.reset(captchaId);
  136. hcaptcha.remove(captchaId);
  137. }
  138. }, {
  139. key: 'shouldComponentUpdate',
  140. value: function shouldComponentUpdate(nextProps, nextState) {
  141. // Prevent component re-rendering when these internal state variables are updated
  142. if (this.state.isApiReady !== nextState.isApiReady || this.state.isRemoved !== nextState.isRemoved) {
  143. return false;
  144. }
  145.  
  146. return true;
  147. }
  148. }, {
  149. key: 'componentDidUpdate',
  150. value: function componentDidUpdate(prevProps) {
  151. var _this2 = this;
  152.  
  153. var endpoint = this.props.endpoint;
  154.  
  155. // Prop Keys that could change
  156.  
  157. var keys = ['sitekey', 'size', 'theme', 'tabindex', 'languageOverride', 'endpoint'];
  158. // See if any props changed during component update
  159. var match = keys.every(function (key) {
  160. return prevProps[key] === _this2.props[key];
  161. });
  162.  
  163. // If they have changed, remove current captcha and render a new one
  164. if (!match) {
  165. this.removeCaptcha();
  166. this.renderCaptcha();
  167. }
  168. }
  169. }, {
  170. key: 'renderCaptcha',
  171. value: function renderCaptcha() {
  172. var _state3 = this.state,
  173. isApiReady = _state3.isApiReady,
  174. elementId = _state3.elementId;
  175.  
  176. if (!isApiReady) return;
  177.  
  178. //Render hCaptcha widget and provide neccessary callbacks - hCaptcha
  179. var captchaId = hcaptcha.render(document.getElementById(elementId), _extends({}, this.props, {
  180. "error-callback": this.handleError,
  181. "expired-callback": this.handleExpire,
  182. "callback": this.handleSubmit
  183. }));
  184.  
  185. this.setState({ isRemoved: false, captchaId: captchaId });
  186. }
  187. }, {
  188. key: 'resetCaptcha',
  189. value: function resetCaptcha() {
  190. var _state4 = this.state,
  191. isApiReady = _state4.isApiReady,
  192. isRemoved = _state4.isRemoved,
  193. captchaId = _state4.captchaId;
  194.  
  195.  
  196. if (!isApiReady || isRemoved) return;
  197. // Reset captcha state, removes stored token and unticks checkbox
  198. hcaptcha.reset(captchaId);
  199. }
  200. }, {
  201. key: 'removeCaptcha',
  202. value: function removeCaptcha() {
  203. var _state5 = this.state,
  204. isApiReady = _state5.isApiReady,
  205. isRemoved = _state5.isRemoved,
  206. captchaId = _state5.captchaId;
  207.  
  208.  
  209. if (!isApiReady || isRemoved) return;
  210.  
  211. this.setState({ isRemoved: true }, function () {
  212. hcaptcha.remove(captchaId);
  213. });
  214. }
  215. }, {
  216. key: 'handleOnLoad',
  217. value: function handleOnLoad() {
  218. var _this3 = this;
  219.  
  220. this.setState({ isApiReady: true }, function () {
  221. _this3.renderCaptcha();
  222. });
  223. }
  224. }, {
  225. key: 'handleSubmit',
  226. value: function handleSubmit(event) {
  227. var onVerify = this.props.onVerify;
  228. var _state6 = this.state,
  229. isRemoved = _state6.isRemoved,
  230. captchaId = _state6.captchaId;
  231.  
  232.  
  233. if (typeof hcaptcha === 'undefined' || isRemoved) return;
  234.  
  235. var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget - hCaptcha
  236. onVerify(token); //Dispatch event to verify user response
  237. }
  238. }, {
  239. key: 'handleExpire',
  240. value: function handleExpire() {
  241. var onExpire = this.props.onExpire;
  242. var _state7 = this.state,
  243. isApiReady = _state7.isApiReady,
  244. isRemoved = _state7.isRemoved,
  245. captchaId = _state7.captchaId;
  246.  
  247.  
  248. if (!isApiReady || isRemoved) return;
  249. hcaptcha.reset(captchaId); // If hCaptcha runs into error, reset captcha - hCaptcha
  250.  
  251. if (onExpire) onExpire();
  252. }
  253. }, {
  254. key: 'handleError',
  255. value: function handleError(event) {
  256. var onError = this.props.onError;
  257. var _state8 = this.state,
  258. isApiReady = _state8.isApiReady,
  259. isRemoved = _state8.isRemoved,
  260. captchaId = _state8.captchaId;
  261.  
  262.  
  263. if (!isApiReady || isRemoved) return;
  264.  
  265. hcaptcha.reset(captchaId); // If hCaptcha runs into error, reset captcha - hCaptcha
  266. if (onError) onError(event);
  267. }
  268. }, {
  269. key: 'execute',
  270. value: function execute() {
  271. var _state9 = this.state,
  272. isApiReady = _state9.isApiReady,
  273. isRemoved = _state9.isRemoved,
  274. captchaId = _state9.captchaId;
  275.  
  276.  
  277. if (!isApiReady || isRemoved) return;
  278.  
  279. hcaptcha.execute(captchaId);
  280. }
  281. }, {
  282. key: 'render',
  283. value: function render() {
  284. var elementId = this.state.elementId;
  285.  
  286. return React.createElement('div', { id: elementId });
  287. }
  288. }]);
  289.  
  290. return HCaptcha;
  291. }(React.Component);
  292.  
  293. module.exports = HCaptcha;
  294.  
  295.  
  296. // WEBPACK FOOTER //
  297. // ../node_modules/@hcaptcha/react-hcaptcha/dist/index.js
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement