Generalize Recaptcha.v1 script setup to something we can use outside the Quick Reply.

This commit is contained in:
ccd0 2015-06-22 22:19:55 -07:00
parent 12e44e0ca5
commit 7ea5bf79a1

View File

@ -3,9 +3,7 @@ Captcha.v1 =
return if d.cookie.indexOf('pass_enabled=1') >= 0 return if d.cookie.indexOf('pass_enabled=1') >= 0
return unless @isEnabled = !!$.id 'g-recaptcha' return unless @isEnabled = !!$.id 'g-recaptcha'
script = $.el 'script', @setupScript()
src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js'
$.add d.head, script
captchaContainer = $.el 'div', captchaContainer = $.el 'div',
id: 'captchaContainer' id: 'captchaContainer'
hidden: true hidden: true
@ -23,6 +21,7 @@ Captcha.v1 =
@nodes = @nodes =
img: imgContainer.firstChild img: imgContainer.firstChild
input: input input: input
container: captchaContainer
$.on input, 'blur', QR.focusout $.on input, 'blur', QR.focusout
$.on input, 'focus', QR.focusin $.on input, 'focus', QR.focusin
@ -38,11 +37,32 @@ Captcha.v1 =
QR.captcha.clear() QR.captcha.clear()
$.sync 'captchas', @sync $.sync 'captchas', @sync
new MutationObserver(@afterSetup).observe $.id('captchaContainer'), childList: true new MutationObserver(@afterSetup).observe captchaContainer, childList: true
@beforeSetup() @beforeSetup()
@setup() if Conf['Auto-load captcha'] @setup() if Conf['Auto-load captcha']
setupScript: ->
return if @script
unless @script = $ 'script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]', d.head
@script = $.el 'script',
src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js'
$.add d.head, @script
$.globalEval '''
document.addEventListener('captcha:setup', function(e) {
if (e.target.firstChild) return;
var options = {theme: "clean"};
if (window.Recaptcha) {
window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options);
} else {
var script = document.head.querySelector('script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]');
script.addEventListener('load', function() {
window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options);
}, false);
}
}, false);
'''
cb: cb:
focus: -> QR.captcha.setup false, true focus: -> QR.captcha.setup false, true
@ -67,20 +87,7 @@ Captcha.v1 =
setup: (focus, force) -> setup: (focus, force) ->
return unless @isEnabled and (@needed() or force) return unless @isEnabled and (@needed() or force)
$.globalEval ''' $.event 'captcha:setup', null, @nodes.container
(function() {
var captchaContainer = document.getElementById("captchaContainer");
if (captchaContainer.firstChild) return;
function setup() {
if (window.Recaptcha) {
Recaptcha.create(recaptchaKey, captchaContainer, {theme: "clean"});
} else {
setTimeout(setup, 25);
}
}
setup();
})()
'''
@nodes.input.focus() if focus @nodes.input.focus() if focus
afterSetup: -> afterSetup: ->