【JavaScript】分享一个定时到网站上签到/签退的JS脚本

本文介绍了一种使用JavaScript实现定时签到/签退的方法,涉及分析网页结构,自动打开网站,自动登录以及定时点击签到按钮的步骤。文章适合有一定HTML和JavaScript基础的读者,通过定时器和DOM操作实现自动化的签到流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

  1. 打开网站
  2. 登陆
  3. 点击“签到”按钮

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

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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值