Share captcha with original post form.

This commit is contained in:
ccd0 2015-06-23 01:06:09 -07:00
parent f27b240b6e
commit 1a19b54c49
2 changed files with 32 additions and 26 deletions

View File

@ -20,15 +20,20 @@ Captcha.replace =
$.onExists doc, 'iframe', true, Captcha.replace.iframe $.onExists doc, 'iframe', true, Captcha.replace.iframe
v1: -> v1: ->
return unless old = $.id 'g-recaptcha' return unless $.id 'g-recaptcha'
container = $.el 'div', Captcha.v1.replace()
id: 'captchaContainerAlt' if link = $.id 'form-link'
$.replace old, container $.on link, 'click', -> Captcha.v1.create()
Captcha.v1.setupScript() else if location.hostname is 'boards.4chan.org'
if link = $ '#togglePostFormLink > a, #form-link' form = $.id 'postForm'
$.on link, 'click', -> $.event 'captcha:setup', null, container form.addEventListener 'focus', (-> Captcha.v1.create()), true
form.addEventListener 'blur', ->
$.queueTask ->
unless form.contains(document.activeElement) or $.id('qr')?.contains(document.activeElement)
Captcha.v1.destroy()
, true
else else
$.event 'captcha:setup', null, container Captcha.v1.create()
v2: -> v2: ->
return unless old = $.id 'captchaContainerAlt' return unless old = $.id 'captchaContainerAlt'

View File

@ -3,12 +3,6 @@ Captcha.v1 =
return if d.cookie.indexOf('pass_enabled=1') >= 0 return if d.cookie.indexOf('pass_enabled=1') >= 0
return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt' return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt'
@setupScript()
captchaContainer = $.el 'div',
id: 'captchaContainer'
hidden: true
$.add d.body, captchaContainer
imgContainer = $.el 'div', imgContainer = $.el 'div',
className: 'captcha-img' className: 'captcha-img'
title: 'Reload reCAPTCHA' title: 'Reload reCAPTCHA'
@ -21,7 +15,6 @@ 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
@ -37,30 +30,38 @@ Captcha.v1 =
QR.captcha.clear() QR.captcha.clear()
$.sync 'captchas', @sync $.sync 'captchas', @sync
new MutationObserver(@afterSetup).observe captchaContainer, childList: true
@beforeSetup() @beforeSetup()
@setup() if Conf['Auto-load captcha'] @setup() if Conf['Auto-load captcha']
new MutationObserver(@afterSetup).observe $.id('captchaContainerAlt'), childList: true
@afterSetup() # reCAPTCHA might have loaded before the QR.
setupScript: -> replace: ->
return if @script return if @script
unless @script = $ 'script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]', d.head unless @script = $ 'script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]', d.head
@script = $.el 'script', @script = $.el 'script',
src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js' src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js'
$.add d.head, @script $.add d.head, @script
if old = $.id 'g-recaptcha'
container = $.el 'div',
id: 'captchaContainerAlt'
$.replace old, container
create: ->
@replace()
$.globalEval ''' $.globalEval '''
document.addEventListener('captcha:setup', function(e) { (function() {
if (e.target.firstChild) return; var container = document.getElementById("captchaContainerAlt");
if (container.firstChild) return;
var options = {theme: "clean"}; var options = {theme: "clean"};
if (window.Recaptcha) { if (window.Recaptcha) {
window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); window.Recaptcha.create("<%= meta.recaptchaKey %>", container, options);
} else { } else {
var script = document.head.querySelector('script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]'); var script = document.head.querySelector('script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]');
script.addEventListener('load', function() { script.addEventListener('load', function() {
window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); window.Recaptcha.create("<%= meta.recaptchaKey %>", container, options);
}, false); }, false);
} }
}, false); })();
''' '''
cb: cb:
@ -87,7 +88,7 @@ Captcha.v1 =
setup: (focus, force) -> setup: (focus, force) ->
return unless @isEnabled and (@needed() or force) return unless @isEnabled and (@needed() or force)
$.event 'captcha:setup', null, @nodes.container @create()
@nodes.input.focus() if focus @nodes.input.focus() if focus
afterSetup: -> afterSetup: ->
@ -117,9 +118,9 @@ Captcha.v1 =
QR.nodes.el.style.bottom = '0px' QR.nodes.el.style.bottom = '0px'
destroy: -> destroy: ->
return unless @isEnabled return unless @script
$.globalEval 'Recaptcha.destroy()' $.globalEval 'Recaptcha.destroy()'
@beforeSetup() @beforeSetup() if @nodes
sync: (captchas=[]) -> sync: (captchas=[]) ->
QR.captcha.captchas = captchas QR.captcha.captchas = captchas