From af7b158846a9bf452bfc9249791b4316816303b0 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 6 Feb 2017 15:07:54 -0800 Subject: [PATCH] Improve conditions for loading v2 captcha when more captchas are needed. --- src/Posting/Captcha.v1.coffee | 4 +--- src/Posting/Captcha.v2.coffee | 23 ++++++++++++----------- src/Posting/QR.post.coffee | 8 +++----- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index c49891312..d230fc748 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -101,9 +101,7 @@ Captcha.v1 = 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: -> + moreNeeded: -> setup: (focus, force) -> return unless @isEnabled and (force or @needed()) diff --git a/src/Posting/Captcha.v2.coffee b/src/Posting/Captcha.v2.coffee index 54848ff51..3a977f4ad 100644 --- a/src/Posting/Captcha.v2.coffee +++ b/src/Posting/Captcha.v2.coffee @@ -34,7 +34,7 @@ Captcha.v2 = $.queueTask => @save false timeouts: {} - postsCount: 0 + prevNeeded: 0 noscriptURL: -> url = 'https://www.google.com/recaptcha/api/fallback?k=<%= meta.recaptchaKey %>' @@ -45,16 +45,17 @@ Captcha.v2 = 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 + 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: -> - @setup() - - onPostChange: -> - @setup() if @postsCount is 0 - @postsCount = 0 if QR.posts.length is 1 and !Conf['Auto-load captcha'] and !QR.posts[0].com and !QR.posts[0].file + moreNeeded: -> + # Post count temporarily off by 1 when called from QR.post.rm, QR.close, or QR.submit + $.queueTask => + needed = @needed() + if needed and not @prevNeeded + @setup(QR.cooldown.auto and d.activeElement is QR.nodes.status) + @prevNeeded = needed toggle: -> if @nodes.container and !@timeouts.destroy @@ -215,10 +216,10 @@ Captcha.v2 = @captchas = @captchas[i..] @count() $.set 'captchas', @captchas - @setup(d.activeElement is QR.nodes.status) count: -> @nodes.counter.textContent = "Captchas: #{@captchas.length}" + @moreNeeded() clearTimeout @timeouts.clear if @captchas.length @timeouts.clear = setTimeout @clear.bind(@), @captchas[0].timeout - Date.now() diff --git a/src/Posting/QR.post.coffee b/src/Posting/QR.post.coffee index 8b1bb1375..42030124d 100644 --- a/src/Posting/QR.post.coffee +++ b/src/Posting/QR.post.coffee @@ -57,8 +57,7 @@ QR.post = class (@load() if QR.selected is @) # load persona @select() if select @unlock() - # Post count temporarily off by 1 when called from QR.post.rm or QR.close - $.queueTask -> QR.captcha.onNewPost() + QR.captcha.moreNeeded() rm: -> @delete() @@ -161,8 +160,7 @@ QR.post = class if @ is QR.selected QR.characterCount() @nodes.span.textContent = @com - # Post count temporarily off by 1 when called from QR.post.rm or QR.close - $.queueTask -> QR.captcha.onPostChange() + QR.captcha.moreNeeded() @rmErrored: (e) -> e.stopPropagation() @@ -203,7 +201,7 @@ QR.post = class @filesize = $.bytesToString @file.size @checkSize() $.addClass @nodes.el, 'has-file' - $.queueTask -> QR.captcha.onPostChange() + QR.captcha.moreNeeded() URL.revokeObjectURL @URL @saveFilename() if @ is QR.selected