From f0d76d8ef70be694e26d5022931b6e2b25a2c2e5 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 17 Nov 2015 17:36:18 -0800 Subject: [PATCH 1/5] Replace shouldFocus flag with focus on counter. --- src/Posting/Captcha.v2.coffee | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Posting/Captcha.v2.coffee b/src/Posting/Captcha.v2.coffee index 9c8102978..70c486bf6 100644 --- a/src/Posting/Captcha.v2.coffee +++ b/src/Posting/Captcha.v2.coffee @@ -42,7 +42,6 @@ Captcha.v2 = conn = new Connection window.parent, "#{location.protocol}//boards.4chan.org" conn.send {token} - shouldFocus: false timeouts: {} postsCount: 0 @@ -74,17 +73,16 @@ Captcha.v2 = setup: (focus, force) -> return unless @isEnabled and (@needed() or force) - @shouldFocus = true if focus and not QR.inBubble() + @nodes.counter.focus() if focus and not QR.inBubble() if @timeouts.destroy clearTimeout @timeouts.destroy delete @timeouts.destroy return @reload() if @nodes.container - if @shouldFocus and iframe = $ 'iframe', @nodes.container + if d.activeElement is @nodes.counter and (iframe = $ 'iframe', @nodes.container) iframe.focus() QR.focus() # Event handler not fired in Firefox - delete @shouldFocus return @nodes.container = $.el 'div', className: 'captcha-container' @@ -143,8 +141,7 @@ Captcha.v2 = if QR.nodes.el.getBoundingClientRect().bottom > doc.clientHeight QR.nodes.el.style.top = null QR.nodes.el.style.bottom = '0px' - iframe.focus() if @shouldFocus - @shouldFocus = false + iframe.focus() if d.activeElement is @nodes.counter # XXX Stop Recaptcha from changing focus from iframe -> body -> iframe on submit. $.global -> f = document.querySelector('#qr iframe') @@ -193,7 +190,7 @@ Captcha.v2 = if @needed() if focus if QR.cooldown.auto or Conf['Post on Captcha Completion'] - @shouldFocus = true + @nodes.counter.focus() else QR.nodes.status.focus() @reload() From d6e16c8eac3915294547b84b5f84d0c2dcc77d21 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 17 Nov 2015 20:10:19 -0800 Subject: [PATCH 2/5] Captcha should steal focus from submit button or body only. --- src/Posting/Captcha.v1.coffee | 2 +- src/Posting/Captcha.v2.coffee | 4 ++-- src/Posting/QR.coffee | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index 904b0f5d0..c371bef7e 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -95,7 +95,7 @@ Captcha.v1 = setup: (focus, force) -> return unless @isEnabled and (force or @needed()) @create() - @nodes.input.focus() if focus + @nodes.input.focus() if focus and d.activeElement in [QR.nodes.status, d.body] afterSetup: -> return unless challenge = $.id 'recaptcha_challenge_field_holder' diff --git a/src/Posting/Captcha.v2.coffee b/src/Posting/Captcha.v2.coffee index 70c486bf6..ddb7de804 100644 --- a/src/Posting/Captcha.v2.coffee +++ b/src/Posting/Captcha.v2.coffee @@ -73,7 +73,7 @@ Captcha.v2 = setup: (focus, force) -> return unless @isEnabled and (@needed() or force) - @nodes.counter.focus() if focus and not QR.inBubble() + @nodes.counter.focus() if focus and d.activeElement in [QR.nodes.status, d.body] if @timeouts.destroy clearTimeout @timeouts.destroy delete @timeouts.destroy @@ -213,7 +213,7 @@ Captcha.v2 = @captchas = @captchas[i..] @count() $.set 'captchas', @captchas - @setup(d.activeElement is QR.nodes.status) + @setup true count: -> @nodes.counter.textContent = "Captchas: #{@captchas.length}" diff --git a/src/Posting/QR.coffee b/src/Posting/QR.coffee index a707e3499..f26bc5e93 100644 --- a/src/Posting/QR.coffee +++ b/src/Posting/QR.coffee @@ -773,7 +773,7 @@ QR = QR.cooldown.addMute seconds else QR.cooldown.addDelay post, seconds - QR.captcha.setup (d.activeElement is QR.nodes.status) + QR.captcha.setup true else # stop auto-posting QR.cooldown.auto = false QR.status() @@ -815,7 +815,7 @@ QR = QR.close() else post.rm() - QR.captcha.setup(d.activeElement is QR.nodes.status) + QR.captcha.setup true QR.cooldown.add threadID, postID From 712a15981691855ed090778d1d4b4208abf45c3e Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 17 Nov 2015 21:07:01 -0800 Subject: [PATCH 3/5] Update captcha count when captchas expire. --- src/Posting/Captcha.v1.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index c371bef7e..ea1fd3ade 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -196,6 +196,9 @@ Captcha.v1 = " (#{count} cached captchas)" @nodes.input.placeholder = placeholder @nodes.input.alt = count # For XTRM RICE. + clearTimeout @timer + if count + @timer = setTimeout @clear.bind(@), @captchas[0].timeout - Date.now() reload: (focus) -> # Recaptcha.should_focus = false: Hack to prevent the input from being focused From bb32e6eaa5406c0ee9e26f37f4cae36ed2998f85 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 17 Nov 2015 21:20:37 -0800 Subject: [PATCH 4/5] All posting errors should reload the captcha if we need more. --- src/Posting/QR.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Posting/QR.coffee b/src/Posting/QR.coffee index f26bc5e93..99f3d29b6 100644 --- a/src/Posting/QR.coffee +++ b/src/Posting/QR.coffee @@ -222,8 +222,6 @@ QR = else el = err el.removeAttribute 'style' - if QR.captcha.isEnabled and /captcha|verification/i.test el.textContent - QR.captcha.setup true notice = new Notice 'warning', el QR.notifications.push notice unless Header.areNotificationsEnabled @@ -633,7 +631,9 @@ QR = if QR.captcha.isEnabled and !err captcha = QR.captcha.getOne() - err = 'No valid captcha.' unless captcha + unless captcha + err = 'No valid captcha.' + QR.captcha.setup true QR.cleanNotifications() if err @@ -773,11 +773,11 @@ QR = QR.cooldown.addMute seconds else QR.cooldown.addDelay post, seconds - QR.captcha.setup true else # stop auto-posting QR.cooldown.auto = false QR.status() QR.error err + QR.captcha.setup true return h1 = $ 'h1', resDoc From 8f88c58a99061699508658f811cb49769509fd6c Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 17 Nov 2015 21:33:33 -0800 Subject: [PATCH 5/5] Use captchas closer to expiring first. #618 --- src/Posting/Captcha.v1.coffee | 1 + src/Posting/Captcha.v2.coffee | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index ea1fd3ade..9a438d0a0 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -154,6 +154,7 @@ Captcha.v1 = challenge: @nodes.img.alt response: response timeout: @timeout + @captchas.sort (a, b) -> a.timeout - b.timeout @count() @destroy() @setup false, true diff --git a/src/Posting/Captcha.v2.coffee b/src/Posting/Captcha.v2.coffee index ddb7de804..9313f16af 100644 --- a/src/Posting/Captcha.v2.coffee +++ b/src/Posting/Captcha.v2.coffee @@ -183,6 +183,7 @@ Captcha.v2 = @captchas.push response: token or $('textarea', @nodes.container).value timeout: Date.now() + @lifetime + @captchas.sort (a, b) -> a.timeout - b.timeout $.set 'captchas', @captchas @count()