Implement new APIs: LoadCaptcha, RequestCaptcha, and AbortCaptcha.

This commit is contained in:
ccd0 2019-07-03 15:50:03 -07:00
parent 63910fafba
commit bb4acb6411
3 changed files with 47 additions and 7 deletions

View File

@ -1,7 +1,9 @@
Captcha.cache = Captcha.cache =
init: -> init: ->
$.on d, 'SaveCaptcha', (e) => $.on d, 'SaveCaptcha', (e) =>
@save e.detail @saveAPI e.detail
$.on d, 'NoCaptcha', (e) =>
@noCaptcha e.detail
captchas: [] captchas: []
@ -10,11 +12,16 @@ Captcha.cache =
needed: -> needed: ->
not ( 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 ( ) and (
QR.posts.length > 1 or Conf['Auto-load captcha'] or QR.posts[0].com or QR.posts[0].file 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) -> getOne: (isReply) ->
@clear() @clear()
i = @captchas.findIndex((x) -> isReply or !x.challenge?) i = @captchas.findIndex((x) -> isReply or !x.challenge?)
@ -25,7 +32,35 @@ Captcha.cache =
else else
null 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) -> save: (captcha) ->
if (cb = @submitCB)
@abort()
cb captcha
return
@captchas.push captcha @captchas.push captcha
@captchas.sort (a, b) -> a.timeout - b.timeout @captchas.sort (a, b) -> a.timeout - b.timeout
@count() @count()

View File

@ -673,7 +673,7 @@ QR =
err or= 'Original comment required.' err or= 'Original comment required.'
if QR.captcha.isEnabled and !(/\b_ct=/.test(d.cookie) and threadID) and !err 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 unless captcha
err = 'No valid captcha.' err = 'No valid captcha.'
QR.captcha.setup(!QR.cooldown.auto or d.activeElement is QR.nodes.status) 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. # Wait for captcha to be verified before submitting post.
QR.req = QR.req =
progress: '...' progress: '...'
abort: -> cb = null abort: ->
Captcha.cache.abort()
cb = null
captcha (response) -> captcha (response) ->
if response if Captcha.cache.haveCookie()
cb?()
Captcha.cache.save response if response
else if response
cb? response cb? response
else else
delete QR.req delete QR.req

View File

@ -340,7 +340,7 @@ $.event = (event, detail, root=d) ->
if detail? and typeof cloneInto is 'function' if detail? and typeof cloneInto is 'function'
detail = cloneInto detail, d.defaultView 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') { %> <% if (type === 'userscript') { %>
# XXX Make $.event work in Pale Moon with GM 3.x (no cloneInto function). # XXX Make $.event work in Pale Moon with GM 3.x (no cloneInto function).
@ -361,7 +361,7 @@ do ->
else else
obj obj
$.event = (event, detail, root=d) -> $.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) -> $.modifiedClick = (e) ->