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

所属分类: 网络编程 / JavaScript 阅读数: 547
收藏 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闭包

闭包是ECMAScript一个很重要的特征,但是却很难用合适的定义来描述它。虽然闭包很难清晰地描述,但是,却很容易创建,或者说,不小心创建。然而,闭包的存在其实是有一定的潜在问题的。为了避免“不小心”地创建闭包,以及更好地利用闭包的优点,有必要理解闭包的机制。闭包的定义 ... 查看详情
收藏 0 赞 0 分享

Javascript delete 引用类型对象

如下面例子: 复制代码 代码如下:var testVar = {            a : {      ... 查看详情
收藏 0 赞 0 分享

JS实现切换标签页效果实例代码

在网页<head>区添加以下代码复制代码 代码如下:<style type="text/css">  <!--  BODY {  PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-S... 查看详情
收藏 0 赞 0 分享

js将json格式内容转换成对象的方法

复制代码 代码如下:{"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"valu e\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"13\"}]"} <... 查看详情
收藏 0 赞 0 分享

jQuery Mobile 导航栏代码

导航栏是由一组水平排列的链接组成,通常包含在头部或尾部内。 默认情况下,导航栏中的链接将自动变成按钮(不需要 data-role="button")。 使用 data-role="navbar" 属性来定义导航栏:复制代码 代码如下:<div data-role="he... 查看详情
收藏 0 赞 0 分享

基于jquery插件实现常见的幻灯片效果

在网站中使用幻灯片效果的目前很普遍,如何实现,自己经过一番研究,本以为很复杂,想不到却很简单。有现成的jquery插件jquery.KinSlideshow.js。 使用jquery.KinSlideshow.js就可以很轻松的实现幻灯片效果 htm代码: 复制代码 代码如下: ... 查看详情
收藏 0 赞 0 分享

多种方法实现JS动态添加事件

方法一、setAttribute var obj = document.getElementById("obj"); obj.setAttribute("onclick", "javascript:alert('测试');"); 但是IE不支持用 setAttribute 设置某... 查看详情
收藏 0 赞 0 分享

JS实现拖动示例代码

getBoundingClientRect() 来获取页面元素的位置 复制代码 代码如下: document.documentElement.getBoundingClientRect 该方法返回一个对象,从而获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,即分别代... 查看详情
收藏 0 赞 0 分享

JS滚轮事件onmousewheel使用介绍

典型的应用时鼠标滚轮滚动控制图片或者文字的大小,例如此类的转动鼠标滚轮实现缩放等等交互效果中,会用到 Mousewheel 事件。在大多数浏览器(IE6, IE7, IE8, Opera 10+, Safari 5+)中,都提供了 “mousewheel” 事件。滚轮事件的兼容性... 查看详情
收藏 0 赞 0 分享

框架页面高度自动刷新的Javascript脚本

实现原理:加载index.htm时候,每隔1秒钟自动调用脚本刷新框架页面代码 代码优点:只需要设置index.html框架页面中的脚本,调用加载的子页面中不需要设置任何代码。 index.htm代码如下: 复制代码 代码如下: <!DOCTYPE html PUBLIC "... 查看详情
收藏 0 赞 0 分享
查看更多