This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockScript.js
88 lines (73 loc) · 2.1 KB
/
blockScript.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
// Config:
const instance = "https://lemmy.world"; // the instance you want to block from
const toBlock = []; // the communities you want to block (the "qualified" name, /c/this_is_the_name => this_is_the_name OR the ID of the community IDs are instance specific! )
const delay = 2500; // Keep this at 2500 or higher to avoid rate limiting
const jwt_token = ""; // find it in your browser's local storage (F12 -> Network -> Headers -> Body -> auth)
const should_block = true; // set to false to unblock communities
// Don't touch anything below this line
// ------------------------------------
const apiUrl = instance + "/api/v3/";
let i = 0;
const blockContent = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
const getContent = {
method: "GET",
headers: {
"Content-Type": "application/json",
},
};
function encodeGetParams(p) {
return Object.entries(p)
.filter((kv) => !!kv[1])
.map((kv) => kv.map(encodeURIComponent).join("="))
.join("&");
}
async function getCommunity(name) {
const url =
apiUrl +
"community" +
"?" +
encodeGetParams({ auth: jwt_token, name: name });
return await (await fetch(url, getContent)).json();
}
async function blockCommunity(comm) {
const api = apiUrl + "community/block";
let commId = comm;
if(typeof(comm) !== "number") {
const commun = await getCommunity(comm)
commId = commun.community_view.community.id;
}
const params = blockContent;
params.body = JSON.stringify({
auth: jwt_token,
community_id: commId,
block: should_block,
});
await fetch(api, params);
}
let id = 0;
function blockAll() {
id = setInterval(async () => {
console.log("Blocking: " + toBlock[i]);
try {
await blockCommunity(toBlock[i]);
} catch (exc) {
console.log(`Failed to block ${toBlock[i]}!`);
console.log(exc);
}
console.log(`Blocked ${toBlock[i]} successfully!`);
i++;
if (i >= toBlock.length) {
console.log("Finished blocking all communities! Ending script.");
clearInterval(id);
}
}, delay);
}
blockAll();
function stop() {
clearInterval(id);
}