-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
more-accessible-favorite.js
executable file
·220 lines (167 loc) · 5.77 KB
/
more-accessible-favorite.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
import {
getAuthString,
getPageDom,
getUrlParams,
isClickModified,
} from "../libs/utils";
import { getAllComments, createSiblingLoader } from "../libs/dom-utils";
import { paths } from "../libs/paths";
const loaderCustomStyle = `
height: 9px;
margin-left: 5px;
`;
async function defaultButtons() {
// Query all faveButtons that are, by default, present on the page
// and have not been injected by this extension. This is achieved
// by initialising this feature before `more-accessible-favorite`
const faveButtonsList = document.querySelectorAll('a[href^="fave"]');
for (const faveButton of faveButtonsList) {
const auth = getUrlParams("auth", faveButton.href);
const id = getUrlParams("id", faveButton.href);
let unfave = faveButton.innerText === "un-favorite";
let ongoingFavorite = false;
faveButton.addEventListener("click", async (event) => {
if (isClickModified(event)) {
return;
}
event.preventDefault();
if (ongoingFavorite) {
return;
}
ongoingFavorite = true;
const loader = createSiblingLoader(faveButton, loaderCustomStyle);
await fetch(faveButton.href).then(() => loader.remove());
unfave = !unfave;
faveButton.innerHTML = unfave ? "un-favorite" : "favorite";
faveButton.href = `fave?id=${id}&auth=${auth}${unfave ? "&un=t" : ""}`;
ongoingFavorite = false;
});
}
}
async function commentButtons(metadata) {
const items = getAllComments();
for (const item of items) {
const separatorPipe = document.createTextNode("| ");
const faveButton = document.createElement("a");
faveButton.innerText = "favorite";
faveButton.classList.add("__rhn__fave-button");
const headSpan = item.querySelector("span.comhead");
const navsSpan = item.querySelector("span.navs");
headSpan.insertBefore(separatorPipe, navsSpan);
headSpan.insertBefore(faveButton, navsSpan);
}
let alreadyFaveItems = metadata.favorites;
if (!alreadyFaveItems) {
const page = await getPageDom(
"https://news.ycombinator.com/favorites?comments=t&id=" + metadata.user
);
const fetchedPageItems = page.querySelectorAll(
"table.itemlist > tbody > tr.athing"
);
alreadyFaveItems = [...fetchedPageItems].map((comm) => comm.id);
}
for (const item of items) {
const { id } = item;
const faveButton = item.querySelector(".__rhn__fave-button");
faveButton.href = "javascript:void(0)";
let unfave = false;
if (alreadyFaveItems.includes(id)) {
faveButton.innerHTML = "un-favorite";
unfave = true;
} else {
faveButton.innerHTML = "favorite";
}
let ongoingFavorite = false;
faveButton.addEventListener("click", async () => {
if (ongoingFavorite) {
return;
}
ongoingFavorite = true;
const loader = createSiblingLoader(faveButton, loaderCustomStyle);
const auth = await getAuthString(id);
const url = `https://news.ycombinator.com/fave?id=${id}&auth=${auth}${
unfave ? "&un=t" : ""
}`;
await fetch(url).then(() => loader.remove());
unfave = !unfave;
faveButton.innerHTML = unfave ? "un-favorite" : "favorite";
ongoingFavorite = false;
});
}
}
async function storyButtons(metadata) {
const items = document.querySelectorAll("td.subtext span.subline");
for (const item of items) {
const lastAnchorButton = item.lastElementChild;
const separatorPipe = document.createTextNode(" | ");
const faveButton = document.createElement("a");
faveButton.innerText = "favorite";
faveButton.classList.add("__rhn__fave-button");
item.insertBefore(faveButton, lastAnchorButton);
item.insertBefore(separatorPipe, lastAnchorButton);
}
let alreadyFaveItems = metadata.favorites;
if (!alreadyFaveItems) {
const page = await getPageDom(
"https://news.ycombinator.com/favorites?id=" + metadata.user
);
const fetchedPageItems = page.querySelectorAll(
"table.itemlist > tbody > tr.athing"
);
alreadyFaveItems = [...fetchedPageItems].map((story) => story.id);
}
for (const item of items) {
const faveButton = item.querySelector(".__rhn__fave-button");
const authStringElement =
item.querySelector('a[href^="flag"]') ||
item.querySelector('a[href^="hide"]');
if (!authStringElement) {
continue;
}
const authStringUrl = authStringElement.href.replace("?", "&");
const auth = getUrlParams("auth", authStringUrl);
const id = getUrlParams("id", authStringUrl);
let unfave = false;
if (alreadyFaveItems.includes(id)) {
faveButton.href = `fave?id=${id}&auth=${auth}&un=t`;
faveButton.innerHTML = "un-favorite";
unfave = true;
} else {
faveButton.href = `fave?id=${id}&auth=${auth}`;
}
let ongoingFavorite = false;
faveButton.addEventListener("click", async (event) => {
event.preventDefault();
if (ongoingFavorite) {
return;
}
ongoingFavorite = true;
const loader = createSiblingLoader(faveButton, loaderCustomStyle);
await fetch(faveButton.href).then(() => loader.remove());
unfave = !unfave;
faveButton.innerHTML = unfave ? "un-favorite" : "favorite";
faveButton.href = `fave?id=${id}&auth=${auth}${unfave ? "&un=t" : ""}`;
ongoingFavorite = false;
});
}
}
async function init(metadata) {
const { path } = metadata;
await defaultButtons();
if (paths.comments.includes(path)) {
await commentButtons(metadata);
} else if (paths.stories.includes(path)) {
await storyButtons(metadata);
}
return true;
}
const details = {
id: "more-accessible-favorite",
pages: {
include: [...paths.stories, ...paths.comments, "/favorites"],
exclude: ["/jobs"],
},
loginRequired: true,
init,
};
export default details;