-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
auto-refresh.js
executable file
·140 lines (109 loc) · 3.51 KB
/
auto-refresh.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
import OptionsSync from "webext-options-sync";
import { initialiseSome } from "../libs/initialise";
import { getPageDom, optionsBarEnabledOptions } from "../libs/utils";
import { createOptionsBar } from "../libs/dom-utils";
import { paths } from "../libs/paths";
import { hideStories } from "./hide-read-stories";
import { sortStories } from "./sort-stories";
function handleInterval(input, options, enabledOptions) {
if (input.disabled) {
return;
}
if (window.refreshInterval) {
clearInterval(window.refreshInterval);
}
const duration = Number(input.value) * 1000; // Milliseconds
if (duration <= 0) {
return;
}
window.refreshInterval = setInterval(() => {
if (input.disabled) {
clearInterval(window.refreshInterval);
return;
}
refresh(options, enabledOptions);
}, duration);
}
async function refresh(options, enabledOptions) {
const loader = document.querySelector("form#autoRefreshForm img");
loader.classList.remove("__rhn__no-display");
const page = await getPageDom(window.location);
if (!page) {
return false;
}
const newStories = page.querySelector("table.itemlist");
document.querySelector("table.itemlist").innerHTML = newStories.innerHTML;
initialiseSome(
"more-accessible-favorite",
"show-user-info-on-hover",
"open-story-links-in-new-tab",
"click-rank-to-vote-unvote"
);
loader.classList.add("__rhn__no-display");
if (enabledOptions.includes("hide-read-stories")) {
hideStories(options);
}
if (enabledOptions.includes("sort-stories")) {
sortStories();
}
}
function init(metadata) {
const { options } = metadata;
const optionsBar = createOptionsBar(options.optionsBarPosition);
const form = document.createElement("form");
const check = document.createElement("input");
const label = document.createElement("label");
const input = document.createElement("input");
const loader = document.createElement("img");
check.type = "checkbox";
check.id = "auto-refresh-check";
check.style.marginLeft = "0px";
check.name = "autoRefreshEnabled";
check.checked = options.autoRefreshEnabled;
label.innerHTML = "auto refresh every ";
label.setAttribute("for", "auto-refresh-check");
input.type = "number";
input.id = "auto-refresh-input";
input.name = "autoRefreshValue";
input.value = options.autoRefreshValue;
loader.src = browser.extension.getURL("loader.gif");
loader.classList.add("__rhn__no-display");
const enabledOptions = optionsBarEnabledOptions(metadata);
if (
enabledOptions.includes("hide-read-stories") ||
enabledOptions.includes("sort-stories")
) {
check.style.marginLeft = "8px";
form.append("|");
form.style.marginLeft = "10px";
}
form.id = "autoRefreshForm";
form.append(check, label, input, "seconds", loader);
optionsBar.append(form);
input.disabled = !check.checked;
handleInterval(input, metadata.options, enabledOptions);
input.addEventListener("input", () => {
input.style.width = input.value.length + 4 + "ch";
});
form.addEventListener("change", () => {
const isChecked = check.checked;
input.disabled = !isChecked;
if (!isChecked) {
loader.classList.add("__rhn__no-display");
}
handleInterval(input, metadata.options, enabledOptions);
});
new OptionsSync({ logging: false }).syncForm("#autoRefreshForm");
return true;
}
const details = {
id: "auto-refresh",
pages: {
include: paths.stories,
exclude: ["/front"],
},
loginRequired: false,
awaitInit: true,
init,
};
export default details;