From 1a19b54c497dca17e18b82dbd60867060c8cf579 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 23 Jun 2015 01:06:09 -0700 Subject: [PATCH] Share captcha with original post form. --- src/Posting/Captcha.replace.coffee | 21 ++++++++++------- src/Posting/Captcha.v1.coffee | 37 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/Posting/Captcha.replace.coffee b/src/Posting/Captcha.replace.coffee index d98aa709c..e118edded 100644 --- a/src/Posting/Captcha.replace.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -20,15 +20,20 @@ Captcha.replace = $.onExists doc, 'iframe', true, Captcha.replace.iframe v1: -> - return unless old = $.id 'g-recaptcha' - container = $.el 'div', - id: 'captchaContainerAlt' - $.replace old, container - Captcha.v1.setupScript() - if link = $ '#togglePostFormLink > a, #form-link' - $.on link, 'click', -> $.event 'captcha:setup', null, container + return unless $.id 'g-recaptcha' + Captcha.v1.replace() + if link = $.id 'form-link' + $.on link, 'click', -> Captcha.v1.create() + else if location.hostname is 'boards.4chan.org' + form = $.id 'postForm' + 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 - $.event 'captcha:setup', null, container + Captcha.v1.create() v2: -> return unless old = $.id 'captchaContainerAlt' diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index afaa24e71..a9d488b9a 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -3,12 +3,6 @@ Captcha.v1 = return if d.cookie.indexOf('pass_enabled=1') >= 0 return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt' - @setupScript() - captchaContainer = $.el 'div', - id: 'captchaContainer' - hidden: true - $.add d.body, captchaContainer - imgContainer = $.el 'div', className: 'captcha-img' title: 'Reload reCAPTCHA' @@ -21,7 +15,6 @@ Captcha.v1 = @nodes = img: imgContainer.firstChild input: input - container: captchaContainer $.on input, 'blur', QR.focusout $.on input, 'focus', QR.focusin @@ -37,30 +30,38 @@ Captcha.v1 = QR.captcha.clear() $.sync 'captchas', @sync - new MutationObserver(@afterSetup).observe captchaContainer, childList: true - @beforeSetup() @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 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 + if old = $.id 'g-recaptcha' + container = $.el 'div', + id: 'captchaContainerAlt' + $.replace old, container + + create: -> + @replace() $.globalEval ''' - document.addEventListener('captcha:setup', function(e) { - if (e.target.firstChild) return; + (function() { + var container = document.getElementById("captchaContainerAlt"); + if (container.firstChild) return; var options = {theme: "clean"}; if (window.Recaptcha) { - window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); + window.Recaptcha.create("<%= meta.recaptchaKey %>", container, 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); + window.Recaptcha.create("<%= meta.recaptchaKey %>", container, options); }, false); } - }, false); + })(); ''' cb: @@ -87,7 +88,7 @@ Captcha.v1 = setup: (focus, force) -> return unless @isEnabled and (@needed() or force) - $.event 'captcha:setup', null, @nodes.container + @create() @nodes.input.focus() if focus afterSetup: -> @@ -117,9 +118,9 @@ Captcha.v1 = QR.nodes.el.style.bottom = '0px' destroy: -> - return unless @isEnabled + return unless @script $.globalEval 'Recaptcha.destroy()' - @beforeSetup() + @beforeSetup() if @nodes sync: (captchas=[]) -> QR.captcha.captchas = captchas