JS腳本實(shí)現(xiàn)定時(shí)到網(wǎng)站上簽到/簽退功能
之前介紹過(guò)使用temperMonkey屏蔽CSDN廣告的方法,主要就是要針對(duì)性地分析網(wǎng)站結(jié)構(gòu),然后用代碼去改變或者操作DOM。今天也一樣,我們需要觀察網(wǎng)頁(yè)結(jié)構(gòu),找到我們要操作的按鈕,觸發(fā)他的click事件就可以了。下面以公司打卡簽到的網(wǎng)站為例,做一些壞壞的事情。本文讀者最好有一定的HTML和JavaScript基礎(chǔ)。
首先,想象一下你去簽到需要做什么:
打開(kāi)網(wǎng)站 登陸 點(diǎn)擊“簽到”按鈕然后每一步我們都可以讓代碼幫我們?nèi)プ觥?/p>
0. 如何定時(shí)
這段代碼是核心,就是根據(jù)當(dāng)前時(shí)間和設(shè)定的時(shí)間做差值,來(lái)確定定時(shí)器的值看懂這段代碼,后面的就都容易了。都是在利用定時(shí)器觸發(fā)callback。
// user settingconst SIGN_IN_TIME = '09:30:00'; // 簽到時(shí)間const SIGN_OUT_TIME = '20:00:00'; // 簽退時(shí)間// code implementationlogTime('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 outif (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. 定時(shí)自動(dòng)打開(kāi)網(wǎng)站
因?yàn)榇蠖鄶?shù)網(wǎng)站都有,“長(zhǎng)時(shí)間未操作-自動(dòng)退出”的設(shè)置。所以我們要在需要打卡的時(shí)候再打開(kāi)網(wǎng)站。在電腦本地跑一個(gè)程序,使用定時(shí)器。這里跑一個(gè)node程序:
const open = require(’open’);logTime('Start Runing');// user settingconst SIGN_IN_TIME = '09:30:00';const SIGN_OUT_TIME = '20:20:00';// code implementationconst 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 outif (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. 自動(dòng)登錄
這個(gè)重點(diǎn)還是在于找到需要填入內(nèi)容的DOM元素
(function() { ’use strict’; // login document.querySelector('#loginid').value = '用戶名'; document.querySelector('#userpassword').value = '密碼'; document.querySelector('#login').click();})();
3. 定時(shí)點(diǎn)擊按鈕
這一步最重要的是要準(zhǔn)確找到按鈕,檢查元素慢慢查找吧。其次,設(shè)定好簽到和簽退的時(shí)間,只有固定時(shí)間才會(huì)自動(dòng)簽到,防止每次登陸進(jìn)來(lái)自動(dòng)簽到或簽退,這樣太頻繁被發(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!`); // 重點(diǎn)就在這兒了,找到網(wǎng)站的簽到按鈕#signInButton,并觸發(fā)他的點(diǎn)擊事件 document.querySelector('#signInButton').click(); } function logTime(str, time=new Date()){ console.log(`${str} -> ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`); }})();
4. 結(jié)束
一套操作,打完收工。每天下班的時(shí)候,不管是提前溜還是晚點(diǎn)到。記得本地開(kāi)一下程序:
node timer.js
到此這篇關(guān)于JS腳本實(shí)現(xiàn)定時(shí)到網(wǎng)站上簽到/簽退功能的文章就介紹到這了,更多相關(guān)js 定時(shí)簽到簽退內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Struts2獲取參數(shù)的三種方法總結(jié)2. JSP中Servlet的Request與Response的用法與區(qū)別3. IntelliJ IDEA刪除類的方法步驟4. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理5. Django視圖類型總結(jié)6. vue cli4下環(huán)境變量和模式示例詳解7. Intellij IDEA 關(guān)閉和開(kāi)啟自動(dòng)更新的提示?8. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)9. Ajax引擎 ajax請(qǐng)求步驟詳細(xì)代碼10. 關(guān)于JavaScript對(duì)象類型之Array及Object
