forked from 14790897/auto-read-liunxdo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_likeUser_activity.js
395 lines (364 loc) · 14.2 KB
/
index_likeUser_activity.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// ==UserScript==
// @name Auto Like Specific User base on activity
// @namespace http://tampermonkey.net/
// @version 1.1.2
// @description 自动点赞特定用户,适用于discourse
// @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
// @run-at document-end
// ==/UserScript==
(function () {
("use strict");
// 定义可能的基本URL
const possibleBaseURLs = [
"https://meta.discourse.org",
"https://linux.do",
"https://meta.appinn.net",
"https://community.openai.com",
];
const commentLimit = 1000;
const specificUserPostListLimit = 100;
const currentURL = window.location.href;
let specificUser = localStorage.getItem("specificUser") || "14790897";
let likeLimit = parseInt(localStorage.getItem("likeLimit") || 200, 10);
let BASE_URL = possibleBaseURLs.find((url) => currentURL.startsWith(url));
// 环境变量:阅读网址,如果没有找到匹配的URL,则默认为第一个
if (!BASE_URL) {
BASE_URL = possibleBaseURLs[0];
console.log("当前BASE_URL设置为(默认): " + BASE_URL);
} else {
console.log("当前BASE_URL是: " + BASE_URL);
}
// 获取当前时间戳
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);
// 检查是否超过12小时(12小时 = 12 * 60 * 60 * 1000 毫秒)
if (currentTime - storedTime > 12 * 60 * 60 * 1000) {
// 超过24小时,清空点击计数器并更新时间戳
clickCounter = 0;
localStorage.setItem("clickCounter", "0");
localStorage.setItem("clickCounterTimestamp", currentTime.toString());
}
console.log(`Initial clickCounter: ${clickCounter}`);
// 入口函数
window.addEventListener("load", () => {
console.log("autoRead", localStorage.getItem("read"));
checkFirstRun();
if (localStorage.getItem("read") === "true") {
console.log("点赞开始");
setTimeout(() => {
likeSpecificPost();
}, 2000);
setTimeout(() => {
openSpecificUserPost();
}, 4000);
}
});
function checkFirstRun() {
if (localStorage.getItem("isFirstRun") === null) {
console.log("脚本第一次运行,执行初始化操作...");
updateInitialData();
localStorage.setItem("isFirstRun", "false");
} else {
console.log("脚本非第一次运行");
}
}
function updateInitialData() {
localStorage.setItem("read", "false"); // 开始时自动滚动关闭
console.log("执行了初始数据更新操作");
}
function getLatestTopic() {
let lastOffset = Number(localStorage.getItem("lastOffset")) || 0;
let specificUserPostList = [];
let isDataSufficient = false;
while (!isDataSufficient) {
lastOffset += 20;
// lastOffset += 1; //对于page来说
// 举例:https://linux.do/user_actions.json?offset=0&username=14790897&filter=5
const url = `${BASE_URL}/user_actions.json?offset=${lastOffset}&username=${specificUser}&filter=5`;
//举例:https://linux.do/search?q=%4014790897%20in%3Aunseen
// const url = `${BASE_URL}/search?q=%40${specificUser}%20in%3Aunseen`; //&page=${lastOffset}
$.ajax({
url: url,
async: false,
headers: {
Accept: "application/json",
},
success: function (result) {
if (result && result.user_actions && result.user_actions.length > 0) {
result.user_actions.forEach((action) => {
// if (result && result.posts && result.posts.length > 0) {
// result.posts.forEach((action) => {
const topicId = action.topic_id;
// const postId = action.post_id;
const postNumber = action.post_number;
specificUserPostList.push({
topic_id: topicId,
// post_id: postId,
post_number: postNumber,
});
});
// 检查是否已获得足够的 Posts
if (specificUserPostList.length >= specificUserPostListLimit) {
isDataSufficient = true;
}
} else {
isDataSufficient = true; // 没有更多内容时停止请求
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.error(XMLHttpRequest, textStatus, errorThrown);
isDataSufficient = true; // 遇到错误时也停止请求
},
});
}
// 如果列表超出限制,则截断
if (specificUserPostList.length > specificUserPostListLimit) {
specificUserPostList = specificUserPostList.slice(
0,
specificUserPostListLimit
);
}
// 存储 lastOffset 和 specificUserPostList 到 localStorage
localStorage.setItem("lastOffset", lastOffset);
localStorage.setItem(
"specificUserPostList",
JSON.stringify(specificUserPostList)
);
}
function openSpecificUserPost() {
let specificUserPostListStr = localStorage.getItem("specificUserPostList");
let specificUserPostList = specificUserPostListStr
? JSON.parse(specificUserPostListStr)
: [];
// 如果列表为空,则获取最新文章
if (specificUserPostList.length === 0) {
getLatestTopic();
specificUserPostListStr = localStorage.getItem("specificUserPostList");
specificUserPostList = specificUserPostListStr
? JSON.parse(specificUserPostListStr)
: [];
}
// 如果获取到新文章,打开第一个
if (specificUserPostList.length > 0) {
const post = specificUserPostList.shift(); // 获取列表中的第一个对象
localStorage.setItem(
"specificUserPostList",
JSON.stringify(specificUserPostList)
);
window.location.href = `${BASE_URL}/t/topic/${post.topic_id}/${post.post_number}`;
} else {
console.error("未能获取到新的帖子数据。");
}
}
// 检查是否点赞
// const postId = data.post_id;
// const targetId = `discourse-reactions-counter-${postId}-right`;
// const element = document.getElementById(targetId);
function likeSpecificPost() {
const urlParts = window.location.pathname.split("/");
const lastPart = urlParts[urlParts.length - 1]; // 获取最后一部分
let buttons, reactionButton;
console.log("post number:", lastPart);
if (lastPart < 10000) {
buttons = document.querySelectorAll(
"button[aria-label]" //[class*='reply']
);
let targetButton = null;
buttons.forEach((button) => {
const ariaLabel = button.getAttribute("aria-label");
if (ariaLabel && ariaLabel.includes(`#${lastPart}`)) {
targetButton = button;
console.log("找到post number按钮:", targetButton);
return;
}
});
if (targetButton) {
// 找到按钮后,获取其父级元素
const parentElement = targetButton.parentElement;
console.log("父级元素:", parentElement);
reactionButton = parentElement.querySelector(
".discourse-reactions-reaction-button"
);
} else {
console.log(`未找到包含 #${lastPart} 的按钮`);
}
} else {
//大于10000说明是主题帖,选择第一个
reactionButton = document.querySelectorAll(
".discourse-reactions-reaction-button"
)[0];
}
if (
reactionButton.title !== "点赞此帖子" &&
reactionButton.title !== "Like this post"
) {
console.log("已经点赞过");
return "already liked";
} else if (clickCounter >= likeLimit) {
console.log("已经达到点赞上限");
localStorage.setItem("read", false);
return;
}
triggerClick(reactionButton);
clickCounter++;
console.log(
`Clicked like button ${clickCounter},已点赞用户${specificUser}`
);
localStorage.setItem("clickCounter", clickCounter.toString());
// 如果点击次数达到likeLimit次,则设置点赞变量为false
if (clickCounter === likeLimit) {
console.log(
`Reached ${likeLimit} likes, setting the like variable to false.`
);
localStorage.setItem("read", false);
} else {
console.log("clickCounter:", clickCounter);
}
}
function triggerClick(button) {
const event = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
});
button.dispatchEvent(event);
}
const button = document.createElement("button");
button.textContent =
localStorage.getItem("read") === "true" ? "停止阅读" : "开始阅读";
button.style.position = "fixed";
button.style.bottom = "20px";
button.style.left = "20px";
button.style.zIndex = 1000;
button.style.backgroundColor = "#e0e0e0";
button.style.color = "#333";
button.style.border = "1px solid #aaa";
button.style.padding = "8px 16px";
button.style.borderRadius = "8px";
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 (BASE_URL == "https://linux.do") {
const maxPostNumber = 600;
const randomPostNumber = Math.floor(Math.random() * maxPostNumber) + 1;
const newUrl = `https://linux.do/t/topic/13716/${randomPostNumber}`;
window.location.href = newUrl;
} else {
window.location.href = `${BASE_URL}/t/topic/1`;
}
}
};
const userInput = document.createElement("input");
userInput.type = "text";
userInput.placeholder = "输入要点赞的用户ID";
userInput.style.position = "fixed";
userInput.style.bottom = "90px";
userInput.style.left = "20px";
userInput.style.zIndex = "1000";
userInput.style.padding = "6px";
userInput.style.border = "1px solid #aaa";
userInput.style.borderRadius = "8px";
userInput.style.backgroundColor = "#e0e0e0";
userInput.style.width = "100px";
userInput.value = localStorage.getItem("specificUser") || "14790897";
document.body.appendChild(userInput);
const saveUserButton = document.createElement("button");
saveUserButton.textContent = "保存用户ID";
saveUserButton.style.position = "fixed";
saveUserButton.style.bottom = "60px";
saveUserButton.style.left = "20px";
saveUserButton.style.zIndex = "1000";
saveUserButton.style.backgroundColor = "#e0e0e0";
saveUserButton.style.color = "#333";
saveUserButton.style.border = "1px solid #aaa";
saveUserButton.style.padding = "8px 16px";
saveUserButton.style.borderRadius = "8px";
document.body.appendChild(saveUserButton);
saveUserButton.onclick = function () {
const newSpecificUser = userInput.value.trim();
if (newSpecificUser) {
localStorage.setItem("specificUser", newSpecificUser);
localStorage.removeItem("specificUserPostList");
localStorage.removeItem("lastOffset");
specificUser = newSpecificUser;
console.log(
`新的specificUser已保存: ${specificUser},specificUserPostList已重置`
);
}
};
const likeLimitInput = document.createElement("input");
likeLimitInput.type = "number";
likeLimitInput.placeholder = "输入点赞数量";
likeLimitInput.style.position = "fixed";
likeLimitInput.style.bottom = "180px";
likeLimitInput.style.left = "20px";
likeLimitInput.style.zIndex = "1000";
likeLimitInput.style.padding = "6px";
likeLimitInput.style.border = "1px solid #aaa";
likeLimitInput.style.borderRadius = "8px";
likeLimitInput.style.backgroundColor = "#e0e0e0";
likeLimitInput.style.width = "100px";
likeLimitInput.value = localStorage.getItem("likeLimit") || 200;
document.body.appendChild(likeLimitInput);
const saveLikeLimitButton = document.createElement("button");
saveLikeLimitButton.textContent = "保存点赞数量";
saveLikeLimitButton.style.position = "fixed";
saveLikeLimitButton.style.bottom = "140px";
saveLikeLimitButton.style.left = "20px";
saveLikeLimitButton.style.zIndex = "1000";
saveLikeLimitButton.style.backgroundColor = "#e0e0e0";
saveLikeLimitButton.style.color = "#333";
saveLikeLimitButton.style.border = "1px solid #aaa";
saveLikeLimitButton.style.padding = "8px 16px";
saveLikeLimitButton.style.borderRadius = "8px";
document.body.appendChild(saveLikeLimitButton);
saveLikeLimitButton.onclick = function () {
const newLikeLimit = parseInt(likeLimitInput.value.trim(), 10);
if (newLikeLimit && newLikeLimit > 0) {
localStorage.setItem("likeLimit", newLikeLimit);
likeLimit = newLikeLimit;
console.log(`新的likeLimit已保存: ${likeLimit}`);
}
};
// 增加清除数据的按钮
const clearDataButton = document.createElement("button");
clearDataButton.textContent = "清除所有数据";
clearDataButton.style.position = "fixed";
clearDataButton.style.bottom = "20px";
clearDataButton.style.left = "140px";
clearDataButton.style.zIndex = "1000";
clearDataButton.style.backgroundColor = "#ff6666"; // 红色背景,提示删除操作
clearDataButton.style.color = "#fff"; // 白色文本
clearDataButton.style.border = "1px solid #ff3333"; // 深红色边框
clearDataButton.style.padding = "8px 16px";
clearDataButton.style.borderRadius = "8px";
document.body.appendChild(clearDataButton);
clearDataButton.onclick = function () {
localStorage.removeItem("lastOffset");
localStorage.removeItem("clickCounter");
localStorage.removeItem("clickCounterTimestamp");
localStorage.removeItem("specificUserPostList");
console.log("所有数据已清除,除了 specificUser 和 specificUserPostList");
};
})();