Revert "Fix captchas, again. #1531"
This reverts commit fd703d42bad855d9e103ecbdb2628fcef94b9651. Conflicts: CHANGELOG.md
This commit is contained in:
parent
9c820f123d
commit
a122981aa9
@ -1,7 +1,8 @@
|
|||||||
QR.captcha =
|
QR.captcha =
|
||||||
init: ->
|
init: ->
|
||||||
return if d.cookie.indexOf('pass_enabled=1') >= 0
|
return if d.cookie.indexOf('pass_enabled=1') >= 0
|
||||||
return unless @isEnabled = !!$.id 'captchaContainer'
|
container = $.id 'captchaContainer'
|
||||||
|
return unless @isEnabled = !!container
|
||||||
|
|
||||||
$.globalEval 'loadRecaptcha()' if Conf['Auto-load captcha']
|
$.globalEval 'loadRecaptcha()' if Conf['Auto-load captcha']
|
||||||
|
|
||||||
@ -9,9 +10,11 @@ QR.captcha =
|
|||||||
className: 'captcha-img'
|
className: 'captcha-img'
|
||||||
title: 'Reload reCAPTCHA'
|
title: 'Reload reCAPTCHA'
|
||||||
innerHTML: '<img>'
|
innerHTML: '<img>'
|
||||||
|
hidden: true
|
||||||
input = $.el 'input',
|
input = $.el 'input',
|
||||||
className: 'captcha-input field'
|
className: 'captcha-input field'
|
||||||
title: 'Verification'
|
title: 'Verification'
|
||||||
|
placeholder: 'Focus to load reCAPTCHA'
|
||||||
autocomplete: 'off'
|
autocomplete: 'off'
|
||||||
spellcheck: false
|
spellcheck: false
|
||||||
tabIndex: 45
|
tabIndex: 45
|
||||||
@ -19,22 +22,17 @@ QR.captcha =
|
|||||||
img: imgContainer.firstChild
|
img: imgContainer.firstChild
|
||||||
input: input
|
input: input
|
||||||
|
|
||||||
|
$.on input, 'focus', @setup
|
||||||
|
|
||||||
$.on input, 'blur', QR.focusout
|
$.on input, 'blur', QR.focusout
|
||||||
$.on input, 'focus', QR.focusin
|
$.on input, 'focus', QR.focusin
|
||||||
|
|
||||||
$.addClass QR.nodes.el, 'has-captcha'
|
$.addClass QR.nodes.el, 'has-captcha'
|
||||||
$.after QR.nodes.com.parentNode, [imgContainer, input]
|
$.after QR.nodes.com.parentNode, [imgContainer, input]
|
||||||
|
|
||||||
@beforeSetup()
|
|
||||||
@afterSetup() # reCAPTCHA might have loaded before the QR.
|
|
||||||
beforeSetup: ->
|
|
||||||
{img, input} = @nodes
|
|
||||||
img.parentNode.hidden = true
|
|
||||||
input.value = ''
|
|
||||||
input.placeholder = 'Focus to load reCAPTCHA'
|
|
||||||
$.on input, 'focus', @setup
|
|
||||||
@setupObserver = new MutationObserver @afterSetup
|
@setupObserver = new MutationObserver @afterSetup
|
||||||
@setupObserver.observe $.id('captchaContainer'), childList: true
|
@setupObserver.observe container, childList: true
|
||||||
|
@afterSetup() # reCAPTCHA might have loaded before the QR.
|
||||||
setup: ->
|
setup: ->
|
||||||
$.globalEval 'loadRecaptcha()'
|
$.globalEval 'loadRecaptcha()'
|
||||||
afterSetup: ->
|
afterSetup: ->
|
||||||
@ -42,37 +40,89 @@ QR.captcha =
|
|||||||
QR.captcha.setupObserver.disconnect()
|
QR.captcha.setupObserver.disconnect()
|
||||||
delete QR.captcha.setupObserver
|
delete QR.captcha.setupObserver
|
||||||
|
|
||||||
|
setLifetime = (e) -> QR.captcha.lifetime = e.detail
|
||||||
|
$.on window, 'captcha:timeout', setLifetime
|
||||||
|
$.globalEval 'window.dispatchEvent(new CustomEvent("captcha:timeout", {detail: RecaptchaState.timeout}))'
|
||||||
|
$.off window, 'captcha:timeout', setLifetime
|
||||||
|
|
||||||
{img, input} = QR.captcha.nodes
|
{img, input} = QR.captcha.nodes
|
||||||
img.parentNode.hidden = false
|
img.parentNode.hidden = false
|
||||||
input.placeholder = 'Verification'
|
|
||||||
$.off input, 'focus', QR.captcha.setup
|
$.off input, 'focus', QR.captcha.setup
|
||||||
$.on input, 'keydown', QR.captcha.keydown.bind QR.captcha
|
$.on input, 'keydown', QR.captcha.keydown.bind QR.captcha
|
||||||
$.on img.parentNode, 'click', QR.captcha.reload.bind QR.captcha
|
$.on img.parentNode, 'click', QR.captcha.reload.bind QR.captcha
|
||||||
|
|
||||||
|
$.get 'captchas', [], ({captchas}) ->
|
||||||
|
QR.captcha.sync captchas
|
||||||
|
$.sync 'captchas', QR.captcha.sync
|
||||||
|
|
||||||
QR.captcha.nodes.challenge = challenge
|
QR.captcha.nodes.challenge = challenge
|
||||||
new MutationObserver(QR.captcha.load.bind QR.captcha).observe challenge,
|
new MutationObserver(QR.captcha.load.bind QR.captcha).observe challenge,
|
||||||
childList: true
|
childList: true
|
||||||
subtree: true
|
subtree: true
|
||||||
attributes: true
|
attributes: true
|
||||||
QR.captcha.load()
|
QR.captcha.load()
|
||||||
destroy: ->
|
|
||||||
$.globalEval 'Recaptcha.destroy()'
|
sync: (captchas) ->
|
||||||
@beforeSetup()
|
QR.captcha.captchas = captchas
|
||||||
|
QR.captcha.count()
|
||||||
|
|
||||||
getOne: ->
|
getOne: ->
|
||||||
challenge = @nodes.img.alt
|
@clear()
|
||||||
response = @nodes.input.value.trim()
|
if captcha = @captchas.shift()
|
||||||
if response and !/\s/.test response
|
{challenge, response} = captcha
|
||||||
|
@count()
|
||||||
|
$.set 'captchas', @captchas
|
||||||
|
else
|
||||||
|
challenge = @nodes.img.alt
|
||||||
|
if response = @nodes.input.value then @reload()
|
||||||
|
if response
|
||||||
|
response = response.trim()
|
||||||
# one-word-captcha:
|
# one-word-captcha:
|
||||||
# If there's only one word, duplicate it.
|
# If there's only one word, duplicate it.
|
||||||
response = "#{response} #{response}"
|
response = "#{response} #{response}" unless /\s/.test response
|
||||||
{challenge, response}
|
{challenge, response}
|
||||||
|
|
||||||
|
save: ->
|
||||||
|
return unless response = @nodes.input.value.trim()
|
||||||
|
@captchas.push
|
||||||
|
challenge: @nodes.img.alt
|
||||||
|
response: response
|
||||||
|
timeout: @timeout
|
||||||
|
@count()
|
||||||
|
@reload()
|
||||||
|
$.set 'captchas', @captchas
|
||||||
|
|
||||||
|
clear: ->
|
||||||
|
return unless @captchas.length
|
||||||
|
now = Date.now()
|
||||||
|
for captcha, i in @captchas
|
||||||
|
break if captcha.timeout > now
|
||||||
|
return unless i
|
||||||
|
@captchas = @captchas[i..]
|
||||||
|
@count()
|
||||||
|
$.set 'captchas', @captchas
|
||||||
|
|
||||||
load: ->
|
load: ->
|
||||||
return unless @nodes.challenge.firstChild
|
return unless @nodes.challenge.firstChild
|
||||||
# -1 minute to give upload some time.
|
# -1 minute to give upload some time.
|
||||||
|
@timeout = Date.now() + @lifetime * $.SECOND - $.MINUTE
|
||||||
challenge = @nodes.challenge.firstChild.value
|
challenge = @nodes.challenge.firstChild.value
|
||||||
@nodes.img.alt = challenge
|
@nodes.img.alt = challenge
|
||||||
@nodes.img.src = "//www.google.com/recaptcha/api/image?c=#{challenge}"
|
@nodes.img.src = "//www.google.com/recaptcha/api/image?c=#{challenge}"
|
||||||
@nodes.input.value = null
|
@nodes.input.value = null
|
||||||
|
@clear()
|
||||||
|
|
||||||
|
count: ->
|
||||||
|
count = if @captchas then @captchas.length else 0
|
||||||
|
@nodes.input.placeholder = switch count
|
||||||
|
when 0
|
||||||
|
'Verification (Shift + Enter to cache)'
|
||||||
|
when 1
|
||||||
|
'Verification (1 cached captcha)'
|
||||||
|
else
|
||||||
|
"Verification (#{count} cached captchas)"
|
||||||
|
@nodes.input.alt = count # For XTRM RICE.
|
||||||
|
|
||||||
reload: (focus) ->
|
reload: (focus) ->
|
||||||
# the 't' argument prevents the input from being focused
|
# the 't' argument prevents the input from being focused
|
||||||
$.globalEval 'Recaptcha.reload("t")'
|
$.globalEval 'Recaptcha.reload("t")'
|
||||||
@ -82,6 +132,8 @@ QR.captcha =
|
|||||||
keydown: (e) ->
|
keydown: (e) ->
|
||||||
if e.keyCode is 8 and not @nodes.input.value
|
if e.keyCode is 8 and not @nodes.input.value
|
||||||
@reload()
|
@reload()
|
||||||
|
else if e.keyCode is 13 and e.shiftKey
|
||||||
|
@save()
|
||||||
else
|
else
|
||||||
return
|
return
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|||||||
@ -668,9 +668,6 @@ QR =
|
|||||||
onerror: (err, url, line) ->
|
onerror: (err, url, line) ->
|
||||||
# Connection error, or www.4chan.org/banned
|
# Connection error, or www.4chan.org/banned
|
||||||
delete QR.req
|
delete QR.req
|
||||||
if QR.captcha.isEnabled
|
|
||||||
QR.captcha.destroy()
|
|
||||||
QR.captcha.setup()
|
|
||||||
post.unlock()
|
post.unlock()
|
||||||
QR.cooldown.auto = false
|
QR.cooldown.auto = false
|
||||||
QR.status()
|
QR.status()
|
||||||
@ -707,7 +704,6 @@ QR =
|
|||||||
{req} = QR
|
{req} = QR
|
||||||
delete QR.req
|
delete QR.req
|
||||||
|
|
||||||
QR.captcha.destroy() if QR.captcha.isEnabled
|
|
||||||
post = QR.posts[0]
|
post = QR.posts[0]
|
||||||
post.unlock()
|
post.unlock()
|
||||||
|
|
||||||
@ -739,12 +735,23 @@ QR =
|
|||||||
err = 'You seem to have mistyped the CAPTCHA.'
|
err = 'You seem to have mistyped the CAPTCHA.'
|
||||||
else if /expired/i.test err.textContent
|
else if /expired/i.test err.textContent
|
||||||
err = 'This CAPTCHA is no longer valid because it has expired.'
|
err = 'This CAPTCHA is no longer valid because it has expired.'
|
||||||
QR.cooldown.auto = false
|
# Enable auto-post if we have some cached captchas.
|
||||||
|
QR.cooldown.auto = if QR.captcha.isEnabled
|
||||||
|
!!QR.captcha.captchas.length
|
||||||
|
else if err is 'Connection error with sys.4chan.org.'
|
||||||
|
true
|
||||||
|
else
|
||||||
|
# Something must've gone terribly wrong if you get captcha errors without captchas.
|
||||||
|
# Don't auto-post indefinitely in that case.
|
||||||
|
false
|
||||||
# Too many frequent mistyped captchas will auto-ban you!
|
# Too many frequent mistyped captchas will auto-ban you!
|
||||||
# On connection error, the post most likely didn't go through.
|
# On connection error, the post most likely didn't go through.
|
||||||
QR.cooldown.set delay: 2
|
QR.cooldown.set delay: 2
|
||||||
else if err.textContent and m = err.textContent.match /wait\s+(\d+)\s+second/i
|
else if err.textContent and m = err.textContent.match /wait\s+(\d+)\s+second/i
|
||||||
QR.cooldown.auto = !QR.captcha.isEnabled
|
QR.cooldown.auto = if QR.captcha.isEnabled
|
||||||
|
!!QR.captcha.captchas.length
|
||||||
|
else
|
||||||
|
true
|
||||||
QR.cooldown.set delay: m[1]
|
QR.cooldown.set delay: m[1]
|
||||||
else # stop auto-posting
|
else # stop auto-posting
|
||||||
QR.cooldown.auto = false
|
QR.cooldown.auto = false
|
||||||
@ -784,6 +791,18 @@ QR =
|
|||||||
# Enable auto-posting if we have stuff left to post, disable it otherwise.
|
# Enable auto-posting if we have stuff left to post, disable it otherwise.
|
||||||
postsCount = QR.posts.length - 1
|
postsCount = QR.posts.length - 1
|
||||||
QR.cooldown.auto = postsCount and isReply
|
QR.cooldown.auto = postsCount and isReply
|
||||||
|
if QR.cooldown.auto and QR.captcha.isEnabled and (captchasCount = QR.captcha.captchas.length) < 3 and captchasCount < postsCount
|
||||||
|
notif = new Notification 'Quick reply warning',
|
||||||
|
body: "You are running low on cached captchas. Cache count: #{captchasCount}."
|
||||||
|
icon: Favicon.logo
|
||||||
|
notif.onclick = ->
|
||||||
|
QR.open()
|
||||||
|
QR.captcha.nodes.input.focus()
|
||||||
|
window.focus()
|
||||||
|
notif.onshow = ->
|
||||||
|
setTimeout ->
|
||||||
|
notif.close()
|
||||||
|
, 7 * $.SECOND
|
||||||
|
|
||||||
unless Conf['Persistent QR'] or QR.cooldown.auto
|
unless Conf['Persistent QR'] or QR.cooldown.auto
|
||||||
QR.close()
|
QR.close()
|
||||||
|
|||||||
@ -91,8 +91,6 @@ QR.post = class
|
|||||||
return unless @ is QR.selected
|
return unless @ is QR.selected
|
||||||
for name in ['thread', 'name', 'email', 'sub', 'com', 'fileButton', 'filename', 'spoiler', 'flag'] when node = QR.nodes[name]
|
for name in ['thread', 'name', 'email', 'sub', 'com', 'fileButton', 'filename', 'spoiler', 'flag'] when node = QR.nodes[name]
|
||||||
node.disabled = lock
|
node.disabled = lock
|
||||||
if QR.captcha.isEnabled
|
|
||||||
QR.captcha.nodes.input.disabled = lock
|
|
||||||
@nodes.rm.style.visibility = if lock then 'hidden' else ''
|
@nodes.rm.style.visibility = if lock then 'hidden' else ''
|
||||||
(if lock then $.off else $.on) QR.nodes.filename.previousElementSibling, 'click', QR.openFileInput
|
(if lock then $.off else $.on) QR.nodes.filename.previousElementSibling, 'click', QR.openFileInput
|
||||||
@nodes.spoiler.disabled = lock
|
@nodes.spoiler.disabled = lock
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user