前言
最近突發奇想,寫了一個js插件,用來防止上班摸魚,插件僅知乎頁面有效,別做的太絕,,,
啟動該插件后,打開知乎頁面,標題會改成 “摸魚中,,,” ,背景被替換成咸魚,每隔一分鐘,背景就會變的透明一下,
10分鐘后,頁面提示同時發送釘釘機器人報警,20分鐘后,頁面報警同時發送機器人報警并主動關閉知乎頁面,同時2小時內禁止再次訪問知乎頁面,效果和代碼如下,



// ==UserScript==
// @name 上班防摸魚插件
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 上班防摸魚,自動關閉知乎頁面,發送釘釘機器人報警,
// @author 大話家
// @include *://*.zhihu.com/*
// @require https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js
// @require https://res.layui.com/layui/release/layer/dist/layer.js?v=3111
// @require http://pv.sohu.com/cityjson?ie=utf-8
// ==/UserScript==
(function() {
'use strict';
// 背景透明值
var opacityNum = 1.0;
// 頁面打開的時間
var startTime = dateFormat("YYYY年MM月dd日 HH時mm分ss秒", new Date());
// 主機IP
var ip = returnCitySN.cip;
// 員工唯一標識
var userId = 360945;
var flag = localStorage.getItem("flag");
if(flag == "" || flag == undefined){
flag = 0;
localStorage.setItem("flag",1);
}
if(flag == 2){
var banTime = localStorage.getItem("banTime");
// 2小時后可解封
if(Date.now() - banTime > 2*60*60*1000){
localStorage.setItem("flag",0);
}else{
// 關閉頁面
window.opener = null;
window.open('','_self');
window.close();
}
}
// 設定摸魚壁紙
document.body.style.background = "url('https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1272729702,676992708&fm=26&gp=0.jpg')";
// 設定摸魚標題
setTimeout(function(){
document.title = '摸魚中,,,';
}, 3*1000);
// 摸魚程序中,每一分鐘背景逐漸透明
setInterval(function(){
$("body").css({ opacity: opacityNum });
opacityNum = opacityNum -0.04;
}, 60*1000);
// 10分鐘內,頁面警告;20分鐘內強制退出!
setInterval(function(){
if(flag == 0){
layer.msg("你已摸魚10分鐘,請注意用時!\n上班要專心!", { icon: 7, time: 10000 });
localStorage.setItem("flag", 1);
var dingMsg1 = "提示:"+userId+"("+ip+")用戶從"+startTime+"開始摸魚10分鐘,上班要認真!";
$.ajax({
url:'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx',
type:"POST",
async: false,
beforeSend:function (xhr){
xhr.setRequestHeader('Content-Type', 'application/json,application/x-www-form-urlencoded');
},
data:JSON.stringify({"msgtype": "text","text": {"content": dingMsg1}}),
success:function (res){
console.log(res);
},
error:function (err){
console.log(err);
}
});
// 強制退出
}else{
layer.msg("你已摸魚20分鐘,您將禁止訪問知乎頁面2小時!", { icon: 7, time: 10000 });
localStorage.setItem("flag", 2);
var dingMsg2 = "提示:"+userId+"("+ip+")用戶從"+startTime+"開始摸魚20分鐘!插件將屏蔽該用戶訪問知乎頁面2小時,";
$.ajax({
url:'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxx',
type:"POST",
async: false,
beforeSend:function (xhr){
xhr.setRequestHeader('Content-Type', 'application/json,application/x-www-form-urlencoded');
},
data:JSON.stringify({"msgtype": "text","text": {"content": dingMsg2}}),
success:function (res){
console.log(res);
},
error:function (err){
console.log(err);
}
});
localStorage.setItem("banTime", Date.now());
setTimeout(function(){
window.opener = null;
window.open('','_self');
window.close();
}, 12*1000);
}
}, 10*60*1000);
// 獲取格式化時間
function dateFormat(fmt, date) {
let ret;
const opt = {
"Y+": date.getFullYear().toString(), // 年
"M+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 時
"m+": date.getMinutes().toString(), // 分
"s+": date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以繼續添加,必須轉化成字串
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
}
})();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/189029.html
標籤:java
