最近老是忘记打卡,导致考勤异常,于是想来是时候做一个自动化打卡的服务。
能够实现特定时间内自动打卡,不用手动登陆打卡界面进行打卡。
并且服务的配置能够设定该服务在周一到周日的早上哪个时间段打卡,在晚上的哪个时间段打卡,全部过程无需人工干预都是自动化配置。
初步找了下解决方案,找到了一个叫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);
});

由于经常忘记打卡导致考勤异常,本文介绍了如何使用PhantomJS创建一个自动化打卡服务。该服务能在设定的时间段内自动完成早晚打卡,避免人工干预。目前实现了自动登录和打卡功能,下一步计划将其发展为可配置的长期服务,允许用户自定义每周各天的打卡时间。
2407





