1 | var reCaptcha = { |
||
2 | /** |
||
3 | * @type {Number} counter executions |
||
4 | */ |
||
5 | gexec_count: 0, |
||
6 | key: "6LeLW-MUAAAAALgiXAKP0zo2oslXXbCy57CjFcie", |
||
7 | |||
8 | /** |
||
9 | * Javascript caller |
||
10 | * @param {String} url |
||
11 | * @param {Function} callback |
||
12 | */ |
||
13 | js: function (url, callback) { |
||
14 | var script = document.createElement("script"); |
||
15 | script.type = "text/javascript"; |
||
16 | |||
17 | View Code Duplication | if (script.readyState) { |
|
18 | //IE |
||
19 | script.onreadystatechange = function () { |
||
20 | if (script.readyState == "loaded" || script.readyState == "complete") { |
||
21 | script.onreadystatechange = null; |
||
22 | if (typeof callback == "function") { |
||
23 | callback(); |
||
24 | } |
||
25 | } |
||
26 | }; |
||
27 | } else { |
||
28 | //Others |
||
29 | script.onload = function () { |
||
30 | if (typeof callback == "function") { |
||
31 | callback(); |
||
32 | } |
||
33 | }; |
||
34 | } |
||
35 | |||
36 | script.src = url; |
||
37 | document.getElementsByTagName("head")[0].appendChild(script); |
||
38 | }, |
||
39 | |||
40 | /** |
||
41 | * Set recaptcha site key |
||
42 | * @param {String} key |
||
43 | */ |
||
44 | set_key: function (key) { |
||
45 | reCaptcha.key = key; |
||
46 | }, |
||
47 | |||
48 | /** |
||
49 | * Start recaptcha |
||
50 | */ |
||
51 | start: function () { |
||
52 | reCaptcha.reCaptcha_buttons(true, function () { |
||
53 | reCaptcha.js( |
||
54 | "https://www.google.com/recaptcha/api.js?render=" + |
||
55 | reCaptcha.key + |
||
56 | "&render=explicit", |
||
57 | function () { |
||
58 | grecaptcha.ready(function () { |
||
59 | var msg = |
||
60 | "first_start_" + |
||
61 | location.href |
||
62 | .replace(/[^a-zA-Z0-9 ]/g, "_") |
||
63 | .replace(/\_{2,99}/g, "_") |
||
64 | .replace(/\_$/g, ""); |
||
65 | reCaptcha.exec(msg); |
||
66 | }); |
||
67 | } |
||
68 | ); |
||
69 | }); |
||
70 | }, |
||
71 | |||
72 | /** |
||
73 | * Initialize Recaptcha by defining jquery |
||
74 | */ |
||
75 | init: function () { |
||
76 | if (typeof jQuery == "undefined" || typeof jQuery == "undefined") { |
||
77 | reCaptcha.js( |
||
78 | "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js", |
||
79 | reCaptcha.start |
||
80 | ); |
||
81 | } else { |
||
82 | reCaptcha.start(); |
||
83 | } |
||
84 | }, |
||
85 | retry_count: 0, |
||
86 | /** |
||
87 | * load or refreshing google recaptcha |
||
88 | */ |
||
89 | exec: function (action, retry, callback) { |
||
90 | //console.log('gtag is ' + typeof gtag); |
||
91 | if (typeof gtag == "function") { |
||
92 | gtag("event", "recaptcha", { |
||
93 | action: action, |
||
94 | }); |
||
95 | } |
||
96 | if ( |
||
97 | typeof grecaptcha == "undefined" || |
||
98 | typeof grecaptcha.execute != "function" |
||
99 | ) { |
||
100 | if (typeof toastr == "undefined") { |
||
101 | console.error("recaptcha not loaded"); |
||
102 | } else { |
||
103 | toastr.error( |
||
104 | "recaptcha not loaded, retrying...", |
||
105 | "captcha information" |
||
106 | ); |
||
107 | } |
||
108 | for (let index = 0; index < 3; index++) { |
||
109 | reCaptcha.exec(action, true); |
||
110 | if (index == 3 - 1) { |
||
111 | toastr.error("recaptcha has reached limit", "captcha information"); |
||
112 | } |
||
113 | } |
||
114 | return; |
||
115 | } else if (retry) { |
||
116 | if (typeof toastr == "undefined") { |
||
117 | console.info("recaptcha loaded successfully"); |
||
118 | } else { |
||
119 | toastr.success("recaptcha loaded successfully", "captcha information"); |
||
120 | } |
||
121 | } |
||
122 | reCaptcha.gexec_count++; |
||
123 | var execute = grecaptcha.execute(reCaptcha.key, { |
||
124 | action: action || "location.href", |
||
125 | }); |
||
126 | if (!execute) { |
||
127 | if (typeof toastr != "undefined") { |
||
128 | toastr.error("failed getting token"); |
||
129 | } |
||
130 | return; |
||
131 | } |
||
132 | if (execute) { |
||
133 | execute.then( |
||
134 | /** |
||
135 | * Process token string from recaptcha |
||
136 | * and distribute it into all form elements |
||
137 | * @param {String} token |
||
138 | */ |
||
139 | function (token) { |
||
140 | reCaptcha.reCaptcha_buttons(false, null); |
||
141 | //console.info(token); |
||
142 | reCaptcha.insert(token); |
||
143 | if (typeof callback == "function") { |
||
144 | callback(token); |
||
145 | } |
||
146 | } |
||
147 | ); |
||
148 | } |
||
149 | }, |
||
150 | /** |
||
151 | * Insert reCaptcha Token |
||
152 | * @param {String} token |
||
153 | */ |
||
154 | insert: function (token) { |
||
155 | framework().sc("token", token, 1); |
||
156 | if (typeof jQuery == "undefined") { |
||
157 | console.log("jQuery Not Loaded"); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
158 | reCaptcha.js( |
||
159 | "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js", |
||
160 | function () { |
||
161 | reCaptcha.distribute_token(token); |
||
162 | } |
||
163 | ); |
||
164 | } else { |
||
165 | reCaptcha.distribute_token(token); |
||
166 | } |
||
167 | }, |
||
168 | /** |
||
169 | * Distribute reCaptcha Token |
||
170 | * @param {String} token |
||
171 | */ |
||
172 | distribute_token: function (token) { |
||
173 | var form = $("form"); |
||
174 | form.each(function (i, el) { |
||
175 | var fg = $(this).find('[name="g-recaptcha-response"]'); |
||
176 | console.log(fg.length); |
||
0 ignored issues
–
show
|
|||
177 | if (!fg.length) { |
||
178 | $( |
||
179 | '<input type="hidden" readonly value="' + |
||
180 | token + |
||
181 | '" name="g-recaptcha-response">' |
||
182 | ).appendTo($(this)); |
||
183 | } else { |
||
184 | fg.val(token); |
||
185 | } |
||
186 | }); |
||
187 | }, |
||
188 | /** |
||
189 | * Get token recaptcha |
||
190 | */ |
||
191 | get: function () { |
||
192 | var gr = $('input[name="g-recaptcha-response"]'); |
||
193 | if (gr.length) { |
||
194 | var vr = gr[0].getAttribute("value"); |
||
195 | return vr; |
||
196 | } |
||
197 | return null; |
||
198 | }, |
||
199 | /** |
||
200 | * Button Controller |
||
201 | * @param {Boolean} reCaptcha_disable |
||
202 | * @param {Function} callback |
||
203 | */ |
||
204 | reCaptcha_buttons: function (reCaptcha_disable, callback) { |
||
205 | //toastr.info((reCaptcha_disable ? "disabling" : "enabling") + " button", "Recaptcha initialize"); |
||
206 | $('button,[type="submit"],input') |
||
207 | .not('[data-recaptcha="no-action"]') |
||
208 | .not("[recaptcha-exclude]") |
||
209 | .each(function (i, e) { |
||
210 | if ($(this).attr("type") == "radio") { |
||
211 | return; |
||
212 | } |
||
213 | if (reCaptcha_disable) { |
||
214 | if ($(this).is(":disabled")) { |
||
215 | $(this).attr("recaptcha-exclude", makeid(5)); |
||
216 | } |
||
217 | } |
||
218 | $(this).prop("disabled", reCaptcha_disable); |
||
219 | }); |
||
220 | if (typeof callback == "function") { |
||
221 | callback(); |
||
222 | } |
||
223 | }, |
||
224 | }; |
||
225 | /** |
||
226 | * Hidden reCaptcha v3 object initializer |
||
227 | */ |
||
228 | function recaptcha() { |
||
229 | return reCaptcha; |
||
230 | } |
||
231 |