From bb4acb641120e5ca6c8c68ac8c9f6d4ef40b65ed Mon Sep 17 00:00:00 2001 From: ccd0 Date: Wed, 3 Jul 2019 15:50:03 -0700 Subject: [PATCH] Implement new APIs: LoadCaptcha, RequestCaptcha, and AbortCaptcha. --- src/Posting/Captcha.cache.coffee | 39 ++++++++++++++++++++++++++++++-- src/Posting/QR.coffee | 11 ++++++--- src/platform/$.coffee | 4 ++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/Posting/Captcha.cache.coffee b/src/Posting/Captcha.cache.coffee index 2c0ea4b7b..8d245e8f5 100644 --- a/src/Posting/Captcha.cache.coffee +++ b/src/Posting/Captcha.cache.coffee @@ -1,7 +1,9 @@ Captcha.cache = init: -> $.on d, 'SaveCaptcha', (e) => - @save e.detail + @saveAPI e.detail + $.on d, 'NoCaptcha', (e) => + @noCaptcha e.detail captchas: [] @@ -10,11 +12,16 @@ Captcha.cache = needed: -> not ( - (/\b_ct=/.test(d.cookie) and QR.posts[0].thread isnt 'new') or @captchas.length or QR.req + @haveCookie() or @captchas.length or QR.req ) and ( QR.posts.length > 1 or Conf['Auto-load captcha'] or QR.posts[0].com or QR.posts[0].file + ) and ( + @submitCB or $.event('LoadCaptcha') ) + haveCookie: -> + /\b_ct=/.test(d.cookie) and QR.posts[0].thread isnt 'new' + getOne: (isReply) -> @clear() i = @captchas.findIndex((x) -> isReply or !x.challenge?) @@ -25,7 +32,35 @@ Captcha.cache = else null + request: (isReply) -> + return if $.event('RequestCaptcha', {isReply}) + (cb) => @submitCB = cb + + abort: -> + if @submitCB + delete @submitCB + $.event 'AbortCaptcha' + + saveAPI: (captcha) -> + if (cb = @submitCB) + delete @submitCB + cb captcha + else + @save captcha + + noCaptcha: (detail) -> + if (cb = @submitCB) + if !@haveCookie() or detail?.error + QR.error(detail?.error or 'Failed to retrieve captcha.') + QR.captcha.setup(d.activeElement is QR.nodes.status) + delete @submitCB + cb() + save: (captcha) -> + if (cb = @submitCB) + @abort() + cb captcha + return @captchas.push captcha @captchas.sort (a, b) -> a.timeout - b.timeout @count() diff --git a/src/Posting/QR.coffee b/src/Posting/QR.coffee index 20a340de8..825629171 100644 --- a/src/Posting/QR.coffee +++ b/src/Posting/QR.coffee @@ -673,7 +673,7 @@ QR = err or= 'Original comment required.' if QR.captcha.isEnabled and !(/\b_ct=/.test(d.cookie) and threadID) and !err - captcha = QR.captcha.getOne(!!threadID) + captcha = QR.captcha.getOne(!!threadID) or Captcha.cache.request(!!threadID) unless captcha err = 'No valid captcha.' QR.captcha.setup(!QR.cooldown.auto or d.activeElement is QR.nodes.status) @@ -736,9 +736,14 @@ QR = # Wait for captcha to be verified before submitting post. QR.req = progress: '...' - abort: -> cb = null + abort: -> + Captcha.cache.abort() + cb = null captcha (response) -> - if response + if Captcha.cache.haveCookie() + cb?() + Captcha.cache.save response if response + else if response cb? response else delete QR.req diff --git a/src/platform/$.coffee b/src/platform/$.coffee index 1a4f13037..28d031819 100644 --- a/src/platform/$.coffee +++ b/src/platform/$.coffee @@ -340,7 +340,7 @@ $.event = (event, detail, root=d) -> if detail? and typeof cloneInto is 'function' detail = cloneInto detail, d.defaultView <% } %> - root.dispatchEvent new CustomEvent event, {bubbles: true, detail} + root.dispatchEvent new CustomEvent event, {bubbles: true, cancelable: true, detail} <% if (type === 'userscript') { %> # XXX Make $.event work in Pale Moon with GM 3.x (no cloneInto function). @@ -361,7 +361,7 @@ do -> else obj $.event = (event, detail, root=d) -> - root.dispatchEvent new CustomEvent event, {bubbles: true, detail: clone detail} + root.dispatchEvent new CustomEvent event, {bubbles: true, cancelable: true, detail: clone detail} <% } %> $.modifiedClick = (e) ->