JS腳本實現(xiàn)定時到網(wǎng)站上簽到/簽退功能
之前介紹過使用temperMonkey屏蔽CSDN廣告的方法,主要就是要針對性地分析網(wǎng)站結構,然后用代碼去改變或者操作DOM。今天也一樣,我們需要觀察網(wǎng)頁結構,找到我們要操作的按鈕,觸發(fā)他的
click事件就可以了。下面以公司打卡簽到的網(wǎng)站為例,做一些壞壞的事情。本文讀者最好有一定的HTML和JavaScript基礎。
首先,想象一下你去簽到需要做什么:
- 打開網(wǎng)站
- 登陸
- 點擊“簽到”按鈕
然后每一步我們都可以讓代碼幫我們?nèi)プ觥?/p>
0. 如何定時
這段代碼是核心,就是根據(jù)當前時間和設定的時間做差值,來確定定時器的值
看懂這段代碼,后面的就都容易了。都是在利用定時器觸發(fā)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. 定時自動打開網(wǎng)站
因為大多數(shù)網(wǎng)站都有,“長時間未操作-自動退出”的設置。所以我們要在需要打卡的時候再打開網(wǎng)站。
在電腦本地跑一個程序,使用定時器。這里跑一個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. 自動登錄
這個重點還是在于找到需要填入內(nèi)容的DOM元素
(function() {
'use strict';
// login
document.querySelector("#loginid").value = "用戶名";
document.querySelector("#userpassword").value = "密碼";
document.querySelector("#login").click();
})();
3. 定時點擊按鈕
這一步最重要的是要準確找到按鈕,檢查元素慢慢查找吧。
其次,設定好簽到和簽退的時間,只有固定時間才會自動簽到,防止每次登陸進來自動簽到或簽退,這樣太頻繁被發(fā)現(xiàn)。
(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!`);
// 重點就在這兒了,找到網(wǎng)站的簽到按鈕#signInButton,并觸發(fā)他的點擊事件
document.querySelector("#signInButton").click();
}
function logTime(str, time=new Date()){
console.log(`${str} -> ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`);
}
})();
4. 結束
一套操作,打完收工。每天下班的時候,不管是提前溜還是晚點到。記得本地開一下程序:
node timer.js
到此這篇關于JS腳本實現(xiàn)定時到網(wǎng)站上簽到/簽退功能的文章就介紹到這了,更多相關js 定時簽到簽退內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
跟我學習javascript的函數(shù)調(diào)用和構造函數(shù)調(diào)用
跟我學習javascript的函數(shù)和構造函數(shù)調(diào)用,主要包括三方面內(nèi)容函數(shù)調(diào)用、方法調(diào)用以及構造函數(shù)調(diào)用,想要了解這些內(nèi)容的朋友千萬不要錯過下面的內(nèi)容。2015-11-11
javascript實現(xiàn)自動輸出文本(打字特效)
文字如何實現(xiàn)打字的效果呢?在瀏覽網(wǎng)頁的時候也經(jīng)常能看到這種效果。本文給大家匯總介紹了幾種打字效果的文字特效,文字一個一個地打印在頁面上。2015-08-08
js實現(xiàn)調(diào)用網(wǎng)絡攝像頭及常見錯誤處理
這篇文章主要介紹了js實現(xiàn)調(diào)用網(wǎng)絡攝像頭及常見錯誤處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

