Skip to content

Instantly share code, notes, and snippets.

@2called-chaos
Created January 20, 2019 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2called-chaos/163ef4eaecf2920c188ff9ef4b66e674 to your computer and use it in GitHub Desktop.
Save 2called-chaos/163ef4eaecf2920c188ff9ef4b66e674 to your computer and use it in GitHub Desktop.
AppOS module excerpt
class AppOS.Component.Recaptcha extends AppOS.Component
name: "recaptcha"
API_URL: "https://www.google.com/recaptcha/api.js?onload=%callback&render=explicit&hl=%hl"
init: ->
@pending = []
@apiState = "unloaded"
# init event
$(document).on "recaptcha:init", (ev, el) =>
if el
@initRecaptcha(el)
else
$("div.g-recaptcha").each (i, el) => @initRecaptcha(el)
# global callback for API
window.AppOSRecaptchaAPILoaded = =>
@apiState = "loaded" if @apiState == "loading"
@_initElement(@pending.shift()) while @pending.length
pageLoad: -> $(document).trigger("recaptcha:init")
initRecaptcha: (el) ->
if @apiState == "unloaded"
@apiState = "loading"
@pending.push(el)
$.getScript(@API_URL.replace("%callback", "AppOSRecaptchaAPILoaded").replace("%hl", $(el).data("hl"))).fail =>
@apiState = "blocked"
window.AppOSRecaptchaAPILoaded()
else if @apiState == "loading"
@pending.push(el)
else
@_initElement(el)
_initElement: (el) ->
if @apiState == "blocked"
$(el).html("<strong>Google ReCaptcha got blocked by your browser, login is not possible! Disable content blocking for this site.</strong>")
return
grecaptcha.render $(el).get(0),
sitekey: $(el).data("sitekey")
theme: $(el).data("theme") || "light"
size: $(el).data("size") || "normal"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment