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

之前介绍过使用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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e1cc2257c244f935a9afdb30d9a4390e.html