最近老是忘記打卡,導致考勤異常,於是想來是時候做一個自動化打卡的服務。
能夠實現特定時間內自動打卡,不用手動登陸打卡界面進行打卡。
並且服務的配置能夠設定該服務在週一到週日的早上哪個時間段打卡,在晚上的哪個時間段打卡,全部過程無需人工干預都是自動化配置。
初步找了下解決方案,找到了一個叫phantomJS的東西,一種高度模擬瀏覽器的js語言。具體介紹見:http://phantomjs.org/
目前的進度是,已經能夠實現自動登陸和自動打卡,下一步打算吧這個過程做成長期的,並且是可配置的服務(比如設置週一什麼時候打卡,週二什麼時候打卡等等)。
下面把自動登陸和自動打卡的核心邏輯貼一下:
var page = require('webpage').create();
var usr = 'your Username';// bug!!!: phantom 's defect : evaluation is a sandbox is indenpendent , it do not accept js objects or vars
var psd = 'your Password';
page.onConsoleMessage = function (msg) {
console.log('enter in ' + msg);
};
page.open('****login.html', function(status){
page.evaluate(function(usr,psd) {
document.querySelector('input[name=Name]').value = usr;
document.querySelector('input[name=Password]').value = psd;
document.querySelector('input[name=Logon]').click();
},usr,psd);
page.render('login.png');
window.setTimeout(function(){
page.open('******personnal.html',function(status) {
page.render('logon.png');
page.evaluate(function() {
//document.querySelector('a[name=1|1]').click();
console.log(document.title);
});
window.setTimeout(function() {
page.open('*******daka.com',function(status){//bug!!!: it may need to enter ***daka.com multiple times
page.render('punch.png');//get the punch page now !!!
page.evaluate(function() {
console.log(document.title);//!!! bug : if it may be log on the erp incurrent with kinds of reason(such as network status)
//punch the clock
//create clock event
var clickEvent = document.createEvent("HTMLEvents");
clickEvent.initEvent("click",false,true);
document.getElementById('clockIn').dispatchEvent(clickEvent);
});
window.setTimeout(function() {
console.log('punch completely , exiting');
phantom.exit();
},3000);
});
},3000);
});
},3000);
});