JS脚本实现定时到网站上签到/签退功能

所属分类: 网络编程 / JavaScript 阅读数: 869
收藏 0 赞 0 分享

之前介绍过使用temperMonkey屏蔽CSDN广告的方法,主要就是要针对性地分析网站结构,然后用代码去改变或者操作DOM。今天也一样,我们需要观察网页结构,找到我们要操作的按钮,触发他的click事件就可以了。下面以公司打卡签到的网站为例,做一些坏坏的事情。本文读者最好有一定的HTML和JavaScript基础。

首先,想象一下你去签到需要做什么:

  • 打开网站
  • 登陆
  • 点击“签到”按钮

然后每一步我们都可以让代码帮我们去做。

0. 如何定时

这段代码是核心,就是根据当前时间和设定的时间做差值,来确定定时器的值
看懂这段代码,后面的就都容易了。都是在利用定时器触发callback。

// user setting
const SIGN_IN_TIME = "09:30:00"; // 签到时间
const SIGN_OUT_TIME = "20:00:00"; // 签退时间

// code implementation
logTime("code start running");
const now = new Date();
const today = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
var signInTime = +new Date(`${today} ${SIGN_IN_TIME}`);
logTime("signInTime", new Date(signInTime));
var signOutTime = +new Date(`${today} ${SIGN_OUT_TIME}`);
logTime("signOutTime", new Date(signOutTime));
// diff in or out
if (now > signInTime && now < signOutTime) {
 // ready to sign out for today
 console.log("Seconds to sign out for today: " + (signOutTime - now) / 1000);
 setTimeout(callback, signOutTime - now);
} else {
 // ready to sign in for tomorrow
 signInTime = +signInTime + 60 * 60 * 24 * 1000;
 console.log("Seconds to sign in for tomorrow: " + (signInTime - now) / 1000);
 setTimeout(callback, signInTime - now);
}

function logTime(str, time = new Date()) {
 console.log(`${str} -> ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`);
}

1. 定时自动打开网站

因为大多数网站都有,“长时间未操作-自动退出”的设置。所以我们要在需要打卡的时候再打开网站。
在电脑本地跑一个程序,使用定时器。这里跑一个node程序:

const open = require('open');
logTime("Start Runing");

// user setting
const SIGN_IN_TIME = "09:30:00";
const SIGN_OUT_TIME = "20:20:00";

// code implementation
const openBrowser = async () => {
 await open('http://172.10.80.42');
};
logTime("code start running");
const now = new Date();
const today = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
var signInTime = +new Date(`${today} ${SIGN_IN_TIME}`);
logTime("signInTime", new Date(signInTime));
var signOutTime = +new Date(`${today} ${SIGN_OUT_TIME}`);
logTime("signOutTime", new Date(signOutTime));
// diff in or out
if (now > signInTime && now < signOutTime) {
 // ready to sign out for today
 console.log("Seconds to sign out for today: " + (signOutTime - now) / 1000);
 setTimeout(openBrowser, signOutTime - now);
} else {
 // ready to sign in for tomorrow
 signInTime = +signInTime + 60 * 60 * 24 * 1000;
 console.log("Seconds to sign in for tomorrow: " + (signInTime - now) / 1000);
 setTimeout(openBrowser, signInTime - now);
}

function logTime(str, time = new Date()) {
 console.log(`${str} -> ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`);
}

2. 自动登录

这个重点还是在于找到需要填入内容的DOM元素

(function() {
 'use strict';
 // login
 document.querySelector("#loginid").value = "用户名";
 document.querySelector("#userpassword").value = "密码";
 document.querySelector("#login").click();
})();

3. 定时点击按钮

这一步最重要的是要准确找到按钮,检查元素慢慢查找吧。
其次,设定好签到和签退的时间,只有固定时间才会自动签到,防止每次登陆进来自动签到或签退,这样太频繁被发现。

(function() {
 'use strict';

 // user setting
 const SIGN_IN_TIME = "09:00:00";
 const SIGN_OUT_TIME = "21:00:00";

 // code implementation
 logTime("code start running");
 const now = new Date();
 const today = now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
 var signInTime = +new Date(`${today} ${SIGN_IN_TIME}`);
 logTime("signInTime", new Date(signInTime));
 var signOutTime = +new Date(`${today} ${SIGN_OUT_TIME}`);
 logTime("signOutTime", new Date(signOutTime));
 // diff in or out
 if(now > signInTime && now < signOutTime) {
 // ready to sign out for today
 console.log("Seconds to sign out for today: " + (signOutTime - now)/1000);
 setTimeout(signInorSignOut, signOutTime - now);
 } else {
 // ready to sign in for tomorrow
 signInTime = +signInTime + 60 * 60 * 24 * 1000;
 console.log("Seconds to sign in for tomorrow: " + (signInTime - now)/1000);
 setTimeout(signInorSignOut, signInTime - now);
 }

 // signInorSignOut
 function signInorSignOut(){
 logTime(`signInButton clicked!`);
 // 重点就在这儿了,找到网站的签到按钮#signInButton,并触发他的点击事件
 document.querySelector("#signInButton").click();
 }

 function logTime(str, time=new Date()){
 console.log(`${str} -> ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`);
 }
})();

4. 结束

一套操作,打完收工。每天下班的时候,不管是提前溜还是晚点到。记得本地开一下程序:

node timer.js
更多精彩内容其他人还在看

详解javascript 变量提升(Hoisting)

这篇文章主要介绍了详解javascript 变量提升(Hoisting),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

javascript中数组的常用算法深入分析

这篇文章主要给大家介绍了关于javascript中数组的常用算法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用javascript具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

详解微信小程序框架wepy踩坑记录(与vue对比)

这篇文章主要介绍了详解微信小程序框架wepy踩坑记录(与vue对比),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用Webpack与Babel配置ES6开发环境

这篇文章主要介绍了详解用Webpack与Babel配置ES6开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何使用pm2快速将项目部署到远程服务器

这篇文章主要介绍了如何使用pm2快速将项目部署到远程服务器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Jquery的autocomplete插件用法及参数讲解

今天小编就为大家分享一篇关于Jquery的autocomplete插件用法及参数讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

通过JS运行机制的角度说说作用域

这篇文章主要给大家介绍了如何通过JS运行机制的角度说说作用域的相关资料,文中通过图文介绍的非常详细,对大家的学习或者使用JS作用域具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Vue项目引发的「过滤器」使用教程

这篇文章主要给大家介绍了关于Vue项目引发的「过滤器」使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

利用Vconsole和Fillder进行移动端抓包调试方法

这篇文章主要介绍了利用Vconsole和Fillder进行移动端抓包调试,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

移动端(微信等使用vConsole调试console的方法

这篇文章主要介绍了移动端(微信等使用vConsole调试console的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多