Last active
July 7, 2016 09:05
-
-
Save widyou/c39d2be3d1c6d4af1bd677e24dbf3e42 to your computer and use it in GitHub Desktop.
attendance to inven community with phantomjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var page = require('webpage').create(); | |
function waitFor(testFx, onReady, onFail, timeOutMillis) { | |
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, // Default Max Timout is 3s | |
start = new Date().getTime(), | |
condition = false, | |
interval = setInterval(function() { | |
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { | |
condition = testFx(); | |
if (typeof(condition) !== 'boolean') { | |
console.log(condition); | |
condition = false; | |
} | |
} else { | |
if(!condition) { | |
console.log("'waitFor()' timeout"); | |
phantom.exit(1); | |
//onFail(); | |
} else { | |
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); | |
onReady(); | |
clearInterval(interval); | |
} | |
} | |
}, 500); // repeat check every 500ms | |
} | |
function sleepFor(func, time) { | |
setTimeout(func, time); | |
} | |
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'; | |
page.onUrlChanged = function(targetUrl) { | |
console.log('onUrlChanged: ' + targetUrl); | |
console.log(' page.url: ' + page.url); | |
var url = page.evaluate(function() { | |
return window.location.href; | |
}); | |
console.log(' location.href: ' + url); | |
}; | |
page.onLoadStarted = function() { | |
}; | |
page.onLoadFinished = function(status) { | |
console.log('onLoadFinished:' + page.url + ', ' + page.title); | |
if (page.url.match(/member\.inven\.co\.kr\/user\/scorpio\/clogin/)) { | |
console.log('login processing page found'); | |
page.evaluate(function() { | |
if ($('#btn-ok').length === 1) { | |
$('#btn-ok').trigger('click'); | |
} | |
}); | |
} | |
console.log(page.evaluate(function() { | |
if ($('#comLeftOutloginV2').find('fieldset').length === 1) | |
return $('#comLeftOutloginV2').find('fieldset').text().replace(/\s+/g, ' '); | |
else | |
return ' '; | |
})); | |
}; | |
page.open('http://imart.inven.co.kr/attendance/', function(status) { | |
function login() { | |
console.log('login()'); | |
page.evaluate(function() { | |
$('#comLeftLoginId').val('id'); | |
$('#comLoginPassword').val('password'); | |
$('input.login').trigger('click'); | |
}); | |
} | |
function attendance() { | |
console.log('attendance()'); | |
page.evaluate(function() { | |
if ($('div.attendBttn').find('a').length === 1) { | |
$('div.attendBttn').find('a').trigger('click'); | |
} | |
}); | |
} | |
if (status !== 'success') { | |
console.log('Unable to access network'); | |
phantom.exit(); | |
} else { | |
console.log('a'); | |
var isNotLogin = page.evaluate(function(){ | |
return $('#comLeftLoginId').length === 1; | |
}); | |
if (isNotLogin) { | |
login(); | |
waitFor(function() { | |
return page.evaluate(function() { | |
if (typeof($) !== 'undefined') { | |
return ($('li.character').length === 1); | |
} | |
return false; | |
}) && (page.url === 'http://imart.inven.co.kr/attendance/'); | |
}, function() { | |
console.log('wait complete'); | |
attendance(); | |
setTimeout(function() { | |
phantom.exit(); | |
}, 1000); | |
}, 30000); | |
} else { | |
attendance(); | |
setTimeout(function() { | |
phantom.exit(); | |
}, 1000); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment