UNPKG

@drozdik.m/recaptcha

Version:
89 lines (88 loc) 3.65 kB
exports.__esModule = true; var event_1 = require("@drozdik.m/event"); var RecaptchaV2Args_1 = require("../args/RecaptchaV2Args"); var RecaptchaAPI_1 = require("./RecaptchaAPI"); //-------------------------------------------------- //----------CAPTCHA--------------------------------- //-------------------------------------------------- var RecaptchaV2 = /** @class */ (function () { //-------------------------------------------------- //---------CONSTRUCTOR------------------------------ //-------------------------------------------------- /** * Creates recaptcha object * @param id Id of target * @param siteKey Site key of the recaptcha */ function RecaptchaV2(id, siteKey) { //-------------------------------------------------- //----------VARIABLES------------------------------- //-------------------------------------------------- this.id = ""; this.siteKey = ""; this.OnValidate = new event_1.Event(); this.OnUnvalidate = new event_1.Event(); this.OnStateChange = new event_1.Event(); this.validated = false; this.id = id; this.siteKey = siteKey; var captcha = document.getElementById(id); //No captcha with that ID if (!captcha) { console.error("Captcha(#" + id + ", " + siteKey + ", ...) - Captcha with that id not found"); return; } //Init recaptcha var object = this; RecaptchaAPI_1.RecaptchaAPI.Load().Then(function () { object.InitCaptcha(); }); } /** * Initiates Captcha using grecaptcha * */ RecaptchaV2.prototype.InitCaptcha = function () { //Check grecaptcha if (typeof grecaptcha == "undefined") { console.error("Object \"grecaptcha\" is not defined"); return; } //Create new captcha var object = this; this.widgetId = grecaptcha.render(object.id, { "sitekey": object.siteKey, "callback": function () { object.validated = true; object.OnValidate.Invoke(this, new RecaptchaV2Args_1.RecaptchaV2Args(object.id, object.siteKey, true)); object.OnStateChange.Invoke(this, new RecaptchaV2Args_1.RecaptchaV2Args(object.id, object.siteKey, true)); }, "expired-callback": function () { object.validated = false; object.OnUnvalidate.Invoke(this, new RecaptchaV2Args_1.RecaptchaV2Args(object.id, object.siteKey, false)); object.OnStateChange.Invoke(this, new RecaptchaV2Args_1.RecaptchaV2Args(object.id, object.siteKey, true)); } }); }; /** * Returns the recaptcha response (send in form posts) * */ RecaptchaV2.prototype.GetResponse = function () { return grecaptcha.getResponse(this.widgetId); }; /** * Returns true if the captcha is validated, else false * */ RecaptchaV2.prototype.Validated = function () { return this.validated; }; RecaptchaV2.prototype.Reset = function () { grecaptcha.reset(this.widgetId); if (this.validated) { this.validated = false; this.OnUnvalidate.Invoke(this, new RecaptchaV2Args_1.RecaptchaV2Args(this.id, this.siteKey, false)); this.OnStateChange.Invoke(this, new RecaptchaV2Args_1.RecaptchaV2Args(this.id, this.siteKey, true)); } }; return RecaptchaV2; }()); exports.RecaptchaV2 = RecaptchaV2;