-
Notifications
You must be signed in to change notification settings - Fork 272
/
index.js
337 lines (311 loc) · 11.7 KB
/
index.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// ==UserScript==
// @name Auto Read
// @namespace http://tampermonkey.net/
// @version 1.4.1
// @description 自动刷linuxdo文章
// @author liuweiqing
// @match https://meta.discourse.org/*
// @match https://linux.do/*
// @match https://meta.appinn.net/*
// @match https://community.openai.com/
// @grant none
// @license MIT
// @icon https://www.google.com/s2/favicons?domain=linux.do
// @downloadURL https://update.greasyfork.org/scripts/489464/Auto%20Read.user.js
// @updateURL https://update.greasyfork.org/scripts/489464/Auto%20Read.meta.js
// ==/UserScript==
(function () {
("use strict");
// 定义可能的基本URL
const possibleBaseURLs = [
"https://linux.do",
"https://meta.discourse.org",
"https://meta.appinn.net",
"https://community.openai.com",
];
const commentLimit = 1000;
const topicListLimit = 100;
const likeLimit = 50;
// 获取当前页面的URL
const currentURL = window.location.href;
// 确定当前页面对应的BASE_URL
let BASE_URL = possibleBaseURLs.find((url) => currentURL.startsWith(url));
console.log("currentURL:", currentURL);
// 环境变量:阅读网址,如果没有找到匹配的URL,则默认为第一个
if (!BASE_URL) {
BASE_URL = possibleBaseURLs[0];
console.log("默认BASE_URL设置为: " + BASE_URL);
} else {
console.log("当前BASE_URL是: " + BASE_URL);
}
console.log("脚本正在运行在: " + BASE_URL);
//1.进入网页 https://linux.do/t/topic/数字(1,2,3,4)
//2.使滚轮均衡的往下移动模拟刷文章
// 检查是否是第一次运行脚本
function checkFirstRun() {
if (localStorage.getItem("isFirstRun") === null) {
console.log("脚本第一次运行,执行初始化操作...");
updateInitialData();
localStorage.setItem("isFirstRun", "false");
} else {
console.log("脚本非第一次运行");
}
}
// 更新初始数据的函数
function updateInitialData() {
localStorage.setItem("read", "false"); // 开始时自动滚动关闭
localStorage.setItem("autoLikeEnabled", "false"); //默认关闭自动点赞
console.log("执行了初始数据更新操作");
}
const delay = 2000; // 滚动检查的间隔(毫秒)
let scrollInterval = null;
let checkScrollTimeout = null;
let autoLikeInterval = null;
function scrollToBottomSlowly(distancePerStep = 20, delayPerStep = 50) {
if (scrollInterval !== null) {
clearInterval(scrollInterval);
}
scrollInterval = setInterval(() => {
window.scrollBy(0, distancePerStep);
}, delayPerStep); // 每50毫秒滚动20像素
}
function getLatestTopic() {
let latestPage = Number(localStorage.getItem("latestPage")) || 0;
let topicList = [];
let isDataSufficient = false;
while (!isDataSufficient) {
latestPage++;
const url = `${BASE_URL}/latest.json?no_definitions=true&page=${latestPage}`;
$.ajax({
url: url,
async: false,
success: function (result) {
if (
result &&
result.topic_list &&
result.topic_list.topics.length > 0
) {
result.topic_list.topics.forEach((topic) => {
// 未读且评论数小于 commentLimit
if (commentLimit > topic.posts_count) {
//其实不需要 !topic.unseen &&
topicList.push(topic);
}
});
// 检查是否已获得足够的 topics
if (topicList.length >= topicListLimit) {
isDataSufficient = true;
}
} else {
isDataSufficient = true; // 没有更多内容时停止请求
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.error(XMLHttpRequest, textStatus, errorThrown);
isDataSufficient = true; // 遇到错误时也停止请求
},
});
}
if (topicList.length > topicListLimit) {
topicList = topicList.slice(0, topicListLimit);
}
// 其实不需要对latestPage操作
// localStorage.setItem("latestPage", latestPage);
localStorage.setItem("topicList", JSON.stringify(topicList));
}
function openNewTopic() {
let topicListStr = localStorage.getItem("topicList");
let topicList = topicListStr ? JSON.parse(topicListStr) : [];
// 如果列表为空,则获取最新文章
if (topicList.length === 0) {
getLatestTopic();
topicListStr = localStorage.getItem("topicList");
topicList = topicListStr ? JSON.parse(topicListStr) : [];
}
// 如果获取到新文章,打开第一个
if (topicList.length > 0) {
const topic = topicList.shift();
localStorage.setItem("topicList", JSON.stringify(topicList));
if (topic.last_read_post_number) {
window.location.href = `${BASE_URL}/t/topic/${topic.id}/${topic.last_read_post_number}`;
} else {
window.location.href = `${BASE_URL}/t/topic/${topic.id}`;
}
}
}
// 检查是否已滚动到底部(不断重复执行),到底部时跳转到下一个话题
function checkScroll() {
if (localStorage.getItem("read")) {
if (
window.innerHeight + window.scrollY >=
document.body.offsetHeight - 100
) {
console.log("已滚动到底部");
openNewTopic();
} else {
scrollToBottomSlowly();
if (checkScrollTimeout !== null) {
clearTimeout(checkScrollTimeout);
}
checkScrollTimeout = setTimeout(checkScroll, delay);
}
}
}
// 入口函数
window.addEventListener("load", () => {
checkFirstRun();
console.log(
"autoRead",
localStorage.getItem("read"),
"autoLikeEnabled",
localStorage.getItem("autoLikeEnabled")
);
if (localStorage.getItem("read") === "true") {
console.log("执行正常的滚动和检查逻辑");
checkScroll();
if (isAutoLikeEnabled()) {
autoLike();
}
}
});
// 获取当前时间戳
const currentTime = Date.now();
// 获取存储的时间戳
const defaultTimestamp = new Date("1999-01-01T00:00:00Z").getTime(); //默认值为1999年
const storedTime = parseInt(
localStorage.getItem("clickCounterTimestamp") ||
defaultTimestamp.toString(),
10
);
// 获取当前的点击计数,如果不存在则初始化为0
let clickCounter = parseInt(localStorage.getItem("clickCounter") || "0", 10);
// 检查是否超过24小时(24小时 = 24 * 60 * 60 * 1000 毫秒)
if (currentTime - storedTime > 24 * 60 * 60 * 1000) {
// 超过24小时,清空点击计数器并更新时间戳
clickCounter = 0;
localStorage.setItem("clickCounter", "0");
localStorage.setItem("clickCounterTimestamp", currentTime.toString());
}
console.log(`Initial clickCounter: ${clickCounter}`);
function triggerClick(button) {
const event = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
});
button.dispatchEvent(event);
}
function autoLike() {
console.log(`Initial clickCounter: ${clickCounter}`);
// 寻找所有的discourse-reactions-reaction-button
const buttons = document.querySelectorAll(
".discourse-reactions-reaction-button"
);
if (buttons.length === 0) {
console.error(
"No buttons found with the selector '.discourse-reactions-reaction-button'"
);
return;
}
console.log(`Found ${buttons.length} buttons.`); // 调试信息
// 逐个点击找到的按钮
buttons.forEach((button, index) => {
if (
(button.title !== "点赞此帖子" && button.title !== "Like this post") ||
clickCounter >= likeLimit
) {
return;
}
// 使用setTimeout来错开每次点击的时间,避免同时触发点击
autoLikeInterval = setTimeout(() => {
// 模拟点击
triggerClick(button); // 使用自定义的触发点击方法
console.log(`Clicked like button ${index + 1}`);
clickCounter++; // 更新点击计数器
// 将新的点击计数存储到localStorage
localStorage.setItem("clickCounter", clickCounter.toString());
// 如果点击次数达到likeLimit次,则设置点赞变量为false
if (clickCounter === likeLimit) {
console.log(
`Reached ${likeLimit} likes, setting the like variable to false.`
);
localStorage.setItem("autoLikeEnabled", "false"); // 使用localStorage存储点赞变量状态
} else {
console.log("clickCounter:", clickCounter);
}
}, index * 3000); // 这里的1000毫秒是两次点击之间的间隔,可以根据需要调整
});
}
const button = document.createElement("button");
// 初始化按钮文本基于当前的阅读状态
button.textContent =
localStorage.getItem("read") === "true" ? "停止阅读" : "开始阅读";
button.style.position = "fixed";
button.style.bottom = "10px"; // 之前是 top
button.style.left = "10px"; // 之前是 right
button.style.zIndex = 1000;
button.style.backgroundColor = "#f0f0f0"; // 浅灰色背景
button.style.color = "#000"; // 黑色文本
button.style.border = "1px solid #ddd"; // 浅灰色边框
button.style.padding = "5px 10px"; // 内边距
button.style.borderRadius = "5px"; // 圆角
document.body.appendChild(button);
button.onclick = function () {
const currentlyReading = localStorage.getItem("read") === "true";
const newReadState = !currentlyReading;
localStorage.setItem("read", newReadState.toString());
button.textContent = newReadState ? "停止阅读" : "开始阅读";
if (!newReadState) {
if (scrollInterval !== null) {
clearInterval(scrollInterval);
scrollInterval = null;
}
if (checkScrollTimeout !== null) {
clearTimeout(checkScrollTimeout);
checkScrollTimeout = null;
}
localStorage.removeItem("navigatingToNextTopic");
} else {
// 如果是Linuxdo,就导航到我的帖子
if (BASE_URL == "https://linux.do") {
window.location.href = "https://linux.do/t/topic/13716/518";
} else {
window.location.href = `${BASE_URL}/t/topic/1`;
}
checkScroll();
}
};
//自动点赞按钮
// 在页面上添加一个控制自动点赞的按钮
const toggleAutoLikeButton = document.createElement("button");
toggleAutoLikeButton.textContent = isAutoLikeEnabled()
? "禁用自动点赞"
: "启用自动点赞";
toggleAutoLikeButton.style.position = "fixed";
toggleAutoLikeButton.style.bottom = "50px"; // 之前是 top,且与另一个按钮错开位置
toggleAutoLikeButton.style.left = "10px"; // 之前是 right
toggleAutoLikeButton.style.zIndex = "1000";
toggleAutoLikeButton.style.backgroundColor = "#f0f0f0"; // 浅灰色背景
toggleAutoLikeButton.style.color = "#000"; // 黑色文本
toggleAutoLikeButton.style.border = "1px solid #ddd"; // 浅灰色边框
toggleAutoLikeButton.style.padding = "5px 10px"; // 内边距
toggleAutoLikeButton.style.borderRadius = "5px"; // 圆角
document.body.appendChild(toggleAutoLikeButton);
// 为按钮添加点击事件处理函数
toggleAutoLikeButton.addEventListener("click", () => {
const isEnabled = !isAutoLikeEnabled();
setAutoLikeEnabled(isEnabled);
toggleAutoLikeButton.textContent = isEnabled
? "禁用自动点赞"
: "启用自动点赞";
});
// 判断是否启用自动点赞
function isAutoLikeEnabled() {
// 从localStorage获取autoLikeEnabled的值,如果未设置,默认为"true"
return localStorage.getItem("autoLikeEnabled") !== "false";
}
// 设置自动点赞的启用状态
function setAutoLikeEnabled(enabled) {
localStorage.setItem("autoLikeEnabled", enabled ? "true" : "false");
}
})();