Move captcha fixes to its own module.

This commit is contained in:
ccd0 2015-04-17 21:10:46 -07:00
parent c72d5b4c97
commit 09acfaf9a2
3 changed files with 40 additions and 32 deletions

View File

@ -1,10 +1,12 @@
Main =
init: ->
if location.hostname is 'www.google.com'
type = if location.pathname is '/recaptcha/api/fallback' then 'noscript' else 'v2'
return $.ready ->
doc = d.documentElement
Captcha[type].initFrame()
if location.pathname is '/recaptcha/api/fallback'
Captcha.noscript.initFrame()
else
Captcha.fixes.init()
g.threads = new SimpleDict()
g.posts = new SimpleDict()

View File

@ -0,0 +1,36 @@
Captcha.fixes =
selectors:
image: '.rc-imageselect-target > .rc-imageselect-tile > img'
init: ->
switch location.pathname.split('/')[3]
when 'anchor' then @initMain()
when 'frame' then @initPopup()
initMain: ->
$.onExists d.body, '#recaptcha-anchor', true, (checkbox) ->
focus = ->
if d.hasFocus() and d.activeElement isnt checkbox
checkbox.focus()
focus()
$.on window, 'focus', ->
$.queueTask focus
initPopup: ->
$.addStyle "#{@selectors.image}:focus {outline: 2px solid #4a90e2;}"
@fixImages()
new MutationObserver(=> @fixImages()).observe d.body, {childList: true, subtree: true}
fixImages: ->
return unless (images = $$ @selectors.image).length
focus = images[0].tabIndex isnt 0
for img in images
img.tabIndex = 0
@focusImage images[0] if focus
focusImage: (img) ->
# XXX Image is not focusable at first in Firefox; to be refactored when I figure out why.
$.asap ->
img.focus()
d.activeElement is img or not doc.contains img
, ->

View File

@ -25,36 +25,6 @@ Captcha.v2 =
# XXX Greasemonkey 1.x workaround to gain access to GM_* functions.
$.queueTask => @save false
initFrame: ->
@initFrame2[location.pathname.split('/')[3]]()
initFrame2:
anchor: ->
$.onExists d.body, '#recaptcha-anchor', true, (checkbox) ->
focus = ->
if d.hasFocus() and d.activeElement isnt checkbox
checkbox.focus()
focus()
$.on window, 'focus', ->
$.queueTask focus
frame: ->
selector = '.rc-imageselect-target > .rc-imageselect-tile > img'
$.addStyle "#{selector}:focus {outline: 2px solid #4a90e2;}"
fixImages = ->
return unless (images = $$ selector).length
focus = images[0].tabIndex isnt 0
for img in images
img.tabIndex = 0
if focus
# XXX Image is not focusable at first in Firefox; to be refactored when I figure out why.
$.asap ->
images[0].focus()
d.activeElement is images[0] or not doc.contains images[0]
, ->
fixImages()
new MutationObserver(fixImages).observe d.body, {childList: true, subtree: true}
shouldFocus: false
timeouts: {}
postsCount: 0