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

2022-10-07,,,,

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

到此这篇关于js脚本实现定时到网站上签到/签退功能的文章就介绍到这了,更多相关js 定时签到签退内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

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

下载本文的Word格式文档,以方便收藏与打印。