-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
preview-and-set-top-bar-color.js
executable file
·88 lines (70 loc) · 2.25 KB
/
preview-and-set-top-bar-color.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
import { getPageDom } from "../libs/utils";
import { elementInScrollView, createSiblingLoader } from "../libs/dom-utils";
function loopAndCreate(colorRows, form) {
const topbar = document.querySelector("table#hnmain > tbody > tr > td");
const topColorInput = form.querySelector('input[name="topc"]');
const loaderCustomStyle = `
height: 10px;
margin-top: 5px;
`;
for (const row of colorRows) {
if (!elementInScrollView(row) && row.getBoundingClientRect().top >= 0) {
break;
}
const color = row.innerText.trim();
row.innerHTML +=
'<td><a href="javascript:void(0)">preview</td><td><a href="javascript:void(0)">set</td>';
const [preview, set] = row.querySelectorAll("a");
preview.addEventListener("click", () => {
topbar.bgColor = color;
});
set.addEventListener("click", async () => {
topbar.bgColor = color;
topColorInput.value = color;
const loaderSibling = row.querySelector("td:last-child");
const loader = createSiblingLoader(loaderSibling, loaderCustomStyle);
await fetch("/xuser", {
method: "POST",
body: new URLSearchParams(new FormData(form)),
});
loader.remove();
});
row.classList.add("__rhn__is-set");
}
}
async function init(metadata) {
const userPage = await getPageDom(
`https://news.ycombinator.com/user?id=${metadata.user}`
);
const form = userPage.querySelector("form.profileform");
form.classList.add("__rhn__no-display");
form.target = "_blank";
document.body.append(form);
const topColorInput = form.querySelector('input[name="topc"]');
if (!topColorInput) {
return false;
}
const colorRowTable = document
.querySelectorAll("table#hnmain > tbody > tr")[2]
.querySelector("table");
let colorRows = colorRowTable.querySelectorAll("tbody > tr");
colorRowTable.classList.add("__rhn__top-colors");
loopAndCreate(colorRows, form);
window.addEventListener("scroll", () => {
colorRows = colorRowTable.querySelectorAll(
"tbody > tr:not(.__rhn__is-set)"
);
loopAndCreate(colorRows, form);
});
return true;
}
const details = {
id: "quickly-set-top-bar-color",
pages: {
include: ["/topcolors"],
exclude: [],
},
loginRequired: true,
init,
};
export default details;