发布于  更新于 

知乎定时屏蔽 userscript

项目

知乎每天限制使用时间, 防止一颓就是几个小时. 特判不限制来自搜索引擎的回答页面, 以便有需要的时候查找资料.

安装链接: GreasyFork

主要自用, 如果有其他功能需求请自己动手丰衣足食.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ==UserScript==
// @name Zhihu Smart Block
// @namespace https://duanyll.com/
// @version 0.1
// @description 按每天使用时间屏蔽知乎, 不屏蔽从百度谷歌必应打开的知乎页面以便查找资料
// @author Duanyll
// @match http://*.zhihu.com/*
// @match https://*.zhihu.com/*
// @grant none
// ==/UserScript==

(function () {
'use strict';
if (localStorage.getItem("timeLimit") === null) {
var timeLimit = "";
timeLimit = prompt("请设置限制时间(秒), 请自觉保证它是一个整数")
localStorage.setItem("timeLimit", timeLimit);
console.log(`限制时间设置为 ${timeLimit} 秒`);
}

if ((document.referrer.includes("baidu.com") || document.referrer.includes("google.com") || document.referrer.includes("bing.cn"))
&& window.location.href.includes("question")) {
console.log("不限制从搜索引擎打开的知乎页面");
return;
}

var isBlocked = false;

function checkTime() {
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
var lastDate = localStorage.getItem("lastDate");
if (lastDate === getNowFormatDate()) {
var timeUsed = Number.parseInt(localStorage.getItem("timeUsed"));
if (isNaN(timeUsed)) timeUsed = 0;
timeUsed += secondBetweenCheck;
var timeLimit = Number.parseInt(localStorage.getItem("timeLimit"));
if (timeUsed > timeLimit) {
if (!isBlocked) {
alert('今天的知乎使用时间已到, 按确定关闭知乎.');
document.body.innerHTML = "<h1>知乎已屏蔽</h1>";
isBlocked = true;
}
} else {
localStorage.setItem("timeUsed", timeUsed.toString());
}
} else {
localStorage.setItem("lastDate", getNowFormatDate());
localStorage.setItem("timeUsed", "0");
console.log("这是今天第一次打开知乎");
}
}

var secondBetweenCheck = 2;
var intervalOutput = setInterval(checkTime, secondBetweenCheck * 1000);
window.onblur = () => clearInterval(intervalOutput);
window.onfocus = () => {
intervalOutput = setInterval(checkTime, secondBetweenCheck * 1000);
}
console.log("已加载知乎限制脚本.");
})();