Integrate v1 captcha into current QR code.

This commit is contained in:
ccd0 2015-04-18 21:27:37 -07:00
parent 60f0458ec3
commit 1892ad1973
3 changed files with 43 additions and 11 deletions

View File

@ -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.'

View File

@ -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: ->

View File

@ -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 = '...'