From 1892ad1973044767ea5062518d29143845c100cc Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 18 Apr 2015 21:27:37 -0700 Subject: [PATCH] Integrate v1 captcha into current QR code. --- src/General/Config.coffee | 5 +++++ src/Posting/Captcha.v1.coffee | 33 +++++++++++++++++++++++++-------- src/Posting/QR.coffee | 16 +++++++++++++--- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/General/Config.coffee b/src/General/Config.coffee index e18322dc0..b8eb7d363 100755 --- a/src/General/Config.coffee +++ b/src/General/Config.coffee @@ -463,6 +463,11 @@ Config = 'Use the non-Javascript fallback captcha in the QR even if Javascript is enabled.' 1 ] + 'Use Recaptcha v1': [ + false + 'Use the old version of Recaptcha before the introduction of the checkbox.' + 1 + ] 'Auto-load captcha': [ false 'Automatically load the captcha in the QR even if your post is empty.' diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index fa9822543..b6a89ebf3 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -44,14 +44,31 @@ Captcha.v1 = @beforeSetup() @afterSetup() # reCAPTCHA might have loaded before the QR. + + cb: + focus: -> QR.captcha.setup false, true + beforeSetup: -> {img, input} = @nodes img.parentNode.hidden = true input.value = '' input.placeholder = 'Focus to load reCAPTCHA' @count() - $.on input, 'focus', @setup - setup: -> + $.on input, 'focus click', @cb.focus + + needed: -> + captchaCount = @captchas.length + captchaCount++ if QR.req + postsCount = QR.posts.length + postsCount = 0 if postsCount is 1 and !Conf['Auto-load captcha'] and !QR.posts[0].com and !QR.posts[0].file + captchaCount < postsCount + + onNewPost: -> + + onPostChange: -> + + setup: (focus, force) -> + return unless @isEnabled and (@needed() or force) $.globalEval ''' (function() { var captchaContainer = document.getElementById("captchaContainer"); @@ -66,6 +83,8 @@ Captcha.v1 = setup(); })() ''' + @nodes.input.focus() if focus + afterSetup: -> return unless challenge = $.id 'recaptcha_challenge_field_holder' return if challenge is QR.captcha.nodes.challenge @@ -79,7 +98,7 @@ Captcha.v1 = img.parentNode.hidden = false input.placeholder = 'Verification' QR.captcha.count() - $.off input, 'focus', QR.captcha.setup + $.off input, 'focus click', QR.captcha.cb.focus QR.captcha.nodes.challenge = challenge new MutationObserver(QR.captcha.load.bind QR.captcha).observe challenge, @@ -91,11 +110,13 @@ Captcha.v1 = if QR.nodes.el.getBoundingClientRect().bottom > doc.clientHeight QR.nodes.el.style.top = null QR.nodes.el.style.bottom = '0px' + destroy: -> + return unless @isEnabled $.globalEval 'Recaptcha.destroy()' @beforeSetup() - sync: (captchas) -> + sync: (captchas=[]) -> QR.captcha.captchas = captchas QR.captcha.count() @@ -109,10 +130,6 @@ Captcha.v1 = challenge = @nodes.img.alt if response = @nodes.input.value if Conf['Auto-load captcha'] then @reload() else @destroy() - # Duplicate one-word captchas. - # Don't duplicate street numbers for now (needs testing). - if response and !/\s|^\d$/.test response - response = "#{response} #{response}" {challenge, response} save: -> diff --git a/src/Posting/QR.coffee b/src/Posting/QR.coffee index 6e7f759e7..afc234aba 100644 --- a/src/Posting/QR.coffee +++ b/src/Posting/QR.coffee @@ -10,8 +10,13 @@ QR = return if g.VIEW is 'archive' $.globalEval 'document.documentElement.dataset.jsEnabled = true;' - noscript = Conf['Force Noscript Captcha'] or !doc.dataset.jsEnabled - @captcha = Captcha[if noscript then 'noscript' else 'v2'] + version = if Conf['Force Noscript Captcha'] or !doc.dataset.jsEnabled + 'noscript' + else if Conf['Use Recaptcha v1'] + 'v1' + else + 'v2' + @captcha = Captcha[version] $.on d, '4chanXInitFinished', @initReady @@ -654,7 +659,12 @@ QR = QR.status() cb = (response) -> - extra.form.append 'g-recaptcha-response', response if response? + if response? + if response.challenge? + extra.form.append 'recaptcha_challenge_field', response.challenge + extra.form.append 'recaptcha_response_field', response.response + else + extra.form.append 'g-recaptcha-response', response QR.req = $.ajax "https://sys.4chan.org/#{g.BOARD}/post", options, extra QR.req.progress = '...'