coffee script + phantomjs 实现网页登录+打卡

 

page = require('webpage').create()
system = require('system')

# 用于处理page.evaluate中js脚本的console,使其输出在命令行可见.
page.onConsoleMessage = (msg) ->
    console.log 'CONSOLE: ' , msg

# 用于处理异常
page.onError = (msg) ->
    console.log 'ERROR: ', msg
    phantom.exit()

# 获取调用时的输入参数
# 调用命令格式(设当前脚本文件名称为xxx.coffee): phantomjs xxx.coffee 1 name pass
args = system.args
if args.length == 4
    signType = args[1]
    uName = args[2]
    uPass = args[3]
else
    console.log ' execute params are error !'
    phantom.exit()

# signType 打卡类型: 1.早上上班 2.早上下班 3.下午上班 4.下午下班
# uName/uPass 登陆帐号/密码
console.log "--> signType = #{ signType }, uName = #{ uName }, uPass = #{ uPass } "
# 预设的打卡时间点
signTime = [83559, 120000, 140559, 180000]
now = new Date()
nowTime = now.getHours() * 10000 + now.getMinutes() * 100 + now.getSeconds()
console.log 'nowTime -> ', nowTime
result = switch parseInt(signType, 10)
    when 1 
        if nowTime < signTime[0] then '打卡' else '迟到'
    when 2 
        if nowTime > signTime[1] then '打卡' else '早退'
    when 3 
        if nowTime < signTime[2] then '打卡' else '迟到'
    when 4 
        if nowTime > signTime[3] then '打卡' else '早退'
    else 
        console.log 'signType is error!'
        phantom.exit()
console.log result

# 登陆页面.
LOGIN_URL =  'http://-1'
# 当天的打卡日志列表.
SIGN_RESULT_URL =  'http://-2'
# 打卡. SIGNTYPE参数用于标识打卡类型
SIGN_URL = 'http://-3?SIGNTYPE=' 

console.log 'wait for loginning...'
# 打开登陆页面
page.open LOGIN_URL, ->
    console.log 'opened page --> login page'
    # 将参数:帐号,密码 传入js脚本并执行 !!注意phantomjs与js脚本间的传参方式
    page.evaluate (uName, uPass) ->
        document.getElementsByName('UNAME')[0].value = uName
        document.getElementsByName('PASSWORD')[0].value = uPass
        document.form1.submit()
    , uName, uPass
    # 截图: 登陆页面
    page.render 'login.png'
    console.log 'wait for getting sign result...'
    # 在表单提交后等待3秒
    setTimeout ->
        # 打开当天的打卡记录列表
        page.open SIGN_RESULT_URL, ->
            console.log 'opened page --> sign page'
            # 在当前页面加载后, 等待2秒(因为是ajax处理的缘故)
            setTimeout ->
                # 截图: 今天的打卡日志
                page.render 'sign0.png'
                # 使用JS脚本分析页面上的打卡日志table, 并获取请求的打卡类型SIGNTYPE的当前处理情况
                isSigned = page.evaluate (signType)->
                    doc = window.frames['frame1'].children[1].contentDocument
                    table = doc.querySelector '.TableList'
                    trs = table.getElementsByTagName 'tr'
                    tr = trs[signType]
                    tds = tr.getElementsByTagName 'td'
                    res = [tds[3].innerText, tds[4].innerText]
                    console.log res 
                    return res 
                ,signType
                # 根据打卡情况判断是否进行打卡操作
                if isSigned[0] == '未登记' and isSigned[1] != '不在登记时间段'
                    console.log 'execute signing... ', SIGN_URL + signType
                    # 进行打卡操作
                    page.open SIGN_URL + signType, ->
                        console.log 'wait for signing...'
                        # 等待2秒
                        setTimeout ->
                            # 截图: 此次打卡结果
                            page.render 'sign1.png'
                            phantom.exit()
                        , 2000
                else
                    console.log 'COULD NOT SIGN'
                    phantom.exit()
            , 2000
    , 3000

 

 

你可能感兴趣的:(script)