summaryrefslogtreecommitdiff
path: root/util.coffee
diff options
context:
space:
mode:
authorYaohan Chen <yaohan.chen@gmail.com>2014-04-01 11:22:54 -0400
committerYaohan Chen <yaohan.chen@gmail.com>2014-04-01 11:34:13 -0400
commitc921fc9cd3c97a40f1e267720309da3d1c0fd338 (patch)
tree20828658c6ead46fa36dba14525ab7e9e08daf73 /util.coffee
parent5a03ceb35424e10119b42c9d4e80b72dd81d0815 (diff)
downloadhib-dlagent-c921fc9cd3c97a40f1e267720309da3d1c0fd338.tar.gz
hib-dlagent-c921fc9cd3c97a40f1e267720309da3d1c0fd338.zip
Refactor login and captcha handling code
Diffstat (limited to 'util.coffee')
-rw-r--r--util.coffee20
1 files changed, 11 insertions, 9 deletions
diff --git a/util.coffee b/util.coffee
index bd6403d..8b11814 100644
--- a/util.coffee
+++ b/util.coffee
@@ -25,11 +25,8 @@ exports.display_screenshot = display_screenshot = ->
page.render screenshot
child_process.spawn 'display', [screenshot]
-# handles login/captcha boxes, and calls the passed action() when logged in
-exports.handle_login_captcha = handle_login_captcha = (action, username, password) ->
- need_to_submit = false
-
- # complete a login form if there is one
+# checks if there is a login form, and if so, completes it and returns true
+exports.handle_login = handle_login = (username, password) ->
if page.evaluate(-> document.querySelector 'input[name="username"]')
log 'Entering login information...'
page.evaluate (username, password) ->
@@ -39,10 +36,11 @@ exports.handle_login_captcha = handle_login_captcha = (action, username, passwor
username_box.value = username
if password_box
password_box.value = password
+ (username_box or password_box)?
, username, password
- need_to_submit = true
- # handle a captcha box if there is one
+# checks if there is captcha, and if so, handles it and returns true
+exports.handle_captcha = handle_captcha = ->
if page.evaluate(-> document.querySelector '#recaptcha_response_field')
log 'Humble Bundle wants you to solve a captcha. Displaying screenshot...'
display_process = display_screenshot()
@@ -56,13 +54,17 @@ exports.handle_login_captcha = handle_login_captcha = (action, username, passwor
captcha_box = document.querySelector '#recaptcha_response_field'
captcha_box.value = input
, input
- need_to_submit = true
- if need_to_submit
+# handles login/captcha, repeating if necessary, and performs the action
+exports.handle_login_captcha = handle_login_captcha = (action, username, password) ->
+ entered_login = handle_login(username, password)
+ entered_captcha = handle_captcha()
+ if entered_login or entered_captcha
# Entered information, submit and check for captcha/login again after load finishes
log 'Submitting login information and/or captcha response...'
page.onLoadFinished = -> handle_login_captcha action, username, password
page.evaluate ->
+ # FIXME make sure we're submitting the right form
form = document.querySelector('form')
if form
form.submit()