-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
more-accessible-flag.js
executable file
·71 lines (54 loc) · 1.69 KB
/
more-accessible-flag.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
import { getAuthString } from "../libs/utils";
import { getAllComments, createSiblingLoader } from "../libs/dom-utils";
import { paths } from "../libs/paths";
const loaderCustomStyle = `
height: 9px;
margin-right: 5px;
`;
function init() {
const items = getAllComments();
for (const item of items) {
const separatorPipe = document.createTextNode("| ");
const flagButton = document.createElement("a");
flagButton.innerText = "flag";
flagButton.classList.add("__rhn__flag-button");
flagButton.style.marginRight = "4px";
const headSpan = item.querySelector("span.comhead");
const navsSpan = item.querySelector("span.navs");
headSpan.insertBefore(separatorPipe, navsSpan);
headSpan.insertBefore(flagButton, navsSpan);
}
for (const item of items) {
const { id } = item;
const flagButton = item.querySelector(".__rhn__flag-button");
flagButton.href = "javascript:void(0)";
let ongoingFlag = false;
let flagged = false;
flagButton.addEventListener("click", async () => {
if (ongoingFlag) {
return;
}
ongoingFlag = true;
const loader = createSiblingLoader(flagButton, loaderCustomStyle);
const auth = await getAuthString(id);
const url = `https://news.ycombinator.com/flag?id=${id}&auth=${auth}${
flagged ? "&un=t" : ""
}`;
await fetch(url).then(() => loader.remove());
flagged = !flagged;
flagButton.innerHTML = flagged ? "unflag" : "flag";
ongoingFlag = false;
});
}
return true;
}
const details = {
id: "more-accessible-flag",
pages: {
include: paths.comments,
exclude: ["/jobs"],
},
loginRequired: true,
init,
};
export default details;