Jobley / website · jobley.ai / assets / app-turnstile.js
| 1 | // /app/turnstile/: the page the Mac and iPhone apps load in a small web view to get a Cloudflare |
| 2 | // Turnstile token before they call POST /api/jobs, /api/candidates or /api/moderators. The token |
| 3 | // is handed to the app through the "turnstile" script message handler: |
| 4 | // { type: "token", token } | { type: "expired" } | { type: "error", message } | { type: "not_needed" } |
| 5 | (function () { |
| 6 | var box = document.getElementById('challenge'); |
| 7 | function send(message) { |
| 8 | try { window.webkit.messageHandlers.turnstile.postMessage(message); } catch (e) { /* opened in a browser */ } |
| 9 | box.dataset.state = message.type; |
| 10 | } |
| 11 | fetch('/api/meta', { headers: { accept: 'application/json' } }) |
| 12 | .then(function (r) { return r.json(); }) |
| 13 | .then(function (m) { |
| 14 | if (!m.turnstile_site_key) return send({ type: 'not_needed' }); |
| 15 | window.onAppTurnstile = function () { |
| 16 | window.turnstile.render(box, { |
| 17 | sitekey: m.turnstile_site_key, appearance: 'interaction-only', |
| 18 | callback: function (token) { send({ type: 'token', token: token }); }, |
| 19 | 'expired-callback': function () { send({ type: 'expired' }); }, |
| 20 | 'error-callback': function (code) { send({ type: 'error', message: String(code || 'challenge error') }); } |
| 21 | }); |
| 22 | }; |
| 23 | var s = document.createElement('script'); |
| 24 | s.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=onAppTurnstile'; |
| 25 | s.async = true; |
| 26 | document.head.appendChild(s); |
| 27 | }) |
| 28 | .catch(function () { send({ type: 'error', message: 'could not reach the server' }); }); |
| 29 | })(); |