forked from OsaSoft/youtube-better-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subs-ui.js
309 lines (249 loc) · 10.5 KB
/
subs-ui.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const HIDE_WATCHED_TOGGLE = PREFIX + "hide-watched-toggle";
const HIDE_WATCHED_LABEL = PREFIX + "hide-watched-toggle-label";
const MARK_ALL_WATCHED_BTN = PREFIX + "subs-grid-menu-mark-all";
const SETTINGS_BTN = PREFIX + "subs-grid-menu-settings";
const MARK_WATCHED_BTN = PREFIX + "mark-watched";
const MARK_UNWATCHED_BTN = PREFIX + "mark-unwatched";
const METADATA_LINE = PREFIX + "metadata-line";
const COLLAPSE_SECTION_CHECKBOX = PREFIX + "collapse-section";
const HIDDEN_CLASS = PREFIX + "hidden";
const COLLAPSE_CLASS = PREFIX + "collapse-section";
let addedElems = [];
function hideItem(item) {
hidden.push(item);
item.style.display = 'none';
item.classList.add(HIDDEN_CLASS);
}
function changeMarkWatchedToMarkUnwatched(item) {
// find Mark as watched button and change it to Unmark as watched
let metaDataLine = item.querySelector("#" + METADATA_LINE);
if (metaDataLine != null) {
let dismissibleDiv = metaDataLine.parentNode;
dismissibleDiv.removeChild(metaDataLine);
let markUnwatchedBtn = buildMarkWatchedButton(dismissibleDiv, item, getVideoId(item), false);
dismissibleDiv.appendChild(markUnwatchedBtn);
}
}
function showWatched() {
log("Showing watched videos");
for (let item of hidden) {
item.style.display = '';
item.classList.remove(HIDDEN_CLASS);
}
hidden = [];
processSections();
}
function buildUI() {
log("Building subs UI");
addHideWatchedCheckbox();
addHideAllMenuButton();
addSettingsButton();
if (settings["settings.hide.watched.ui.stick.right"])
addedElems[0].after(...addedElems)
}
function buildMenuButtonContainer() {
let menuButtonContainer;
if (isPolymer) { //is new layout?
menuButtonContainer = document.createElement("h2");
menuButtonContainer.classList.add("yt-simple-endpoint");
menuButtonContainer.classList.add("style-scope");
menuButtonContainer.classList.add("ytd-compact-link-renderer");
} else {
menuButtonContainer = document.createElement("span");
menuButtonContainer.classList.add("yt-uix-clickcard");
}
menuButtonContainer.classList.add("subs-grid-menu-item");
return menuButtonContainer;
}
function addSettingsButton() {
let settingsButton = buildMenuButtonContainer();
settingsButton.classList.add("subs-btn-settings");
settingsButton.setAttribute("id", SETTINGS_BTN);
addElementToMenuUI(settingsButton);
let messenger = document.getElementById(SETTINGS_BTN);
messenger.addEventListener("click", () => brwsr.runtime.sendMessage({"action": "openOptionsPage"}));
}
function addHideAllMenuButton() {
if (settings["settings.hide.watched.all.label"]) {
let hideAllButtonContainer = buildMenuButtonContainer();
hideAllButtonContainer.classList.add("subs-grid-menu-mark-all");
hideAllButtonContainer.setAttribute("id", MARK_ALL_WATCHED_BTN);
hideAllButtonContainer.appendChild(document.createTextNode("Mark all as watched"));
addElementToMenuUI(hideAllButtonContainer);
let messenger = document.getElementById(MARK_ALL_WATCHED_BTN);
messenger.addEventListener("click", markAllAsWatched);
}
}
function addHideWatchedCheckbox() {
if (settings["settings.hide.watched.label"]) {
let hideWatchedLabel = buildMenuButtonContainer();
hideWatchedLabel.setAttribute("id", HIDE_WATCHED_LABEL);
hideWatchedLabel.appendChild(document.createTextNode("Hide watched")); //TODO: translations
addElementToMenuUI(hideWatchedLabel);
let messenger = document.getElementById(HIDE_WATCHED_LABEL);
messenger.addEventListener("click", hideWatchedChanged);
}
let toggleContainer = document.createElement("div");
toggleContainer.setAttribute("id", HIDE_WATCHED_TOGGLE);
toggleContainer.classList.add("toggle-container", "style-scope", "paper-toggle-button");
if (hideWatched) {
toggleContainer.classList.add("subs-btn-hide-watched-checked");
} else {
toggleContainer.classList.add("subs-btn-hide-watched-unchecked");
}
let toggleBar = document.createElement("div");
toggleBar.classList.add("toggle-bar", "style-scope", "paper-toggle-button");
let toggleButton = document.createElement("div");
toggleButton.classList.add("toggle-button", "style-scope", "paper-toggle-button");
toggleContainer.appendChild(toggleBar);
toggleContainer.appendChild(toggleButton);
addElementToMenuUI(toggleContainer);
let messenger = document.getElementById(HIDE_WATCHED_TOGGLE);
messenger.addEventListener("click", hideWatchedChanged);
}
function addElementToMenuUI(element) {
log("Adding element to menu UI");
if (isPolymer) { //is new layout?
let topMenuEnd = document.getElementById("end");
if (topMenuEnd != null) { //just in case...
if (settings["settings.hide.watched.ui.stick.right"])
topMenuEnd.prepend(element);
else
topMenuEnd.parentNode.insertBefore(element, topMenuEnd);
}
} else {
let uiContainer = document.getElementById("yt-masthead-user");
if (uiContainer != null) { //just in case...
uiContainer.insertBefore(element, uiContainer.children[0]);
}
}
addedElems.push(element);
}
function buildMarkWatchedButton(dismissibleDiv, item, videoId, isMarkWatchedBtn = true) {
let enclosingDiv = document.createElement("div");
enclosingDiv.setAttribute("id", METADATA_LINE);
enclosingDiv.classList.add("style-scope", "ytd-thumbnail-overlay-toggle-button-renderer");
let button = document.createElement("button");
button.setAttribute("id", isMarkWatchedBtn ? MARK_WATCHED_BTN : MARK_UNWATCHED_BTN);
button.classList.add(isMarkWatchedBtn ? "subs-btn-mark-watched" : "subs-btn-mark-unwatched");
button.setAttribute("role", "button");
if (isMarkWatchedBtn) {
button.onclick = () => {
markWatched(item, videoId);
};
} else {
button.onclick = () => {
markUnwatched(videoId);
let metaDataElem = item.querySelector("#" + METADATA_LINE);
let container = metaDataElem.parentNode;
container.removeChild(metaDataElem);
container.appendChild(buildMarkWatchedButton(dismissibleDiv, item, videoId));
}
}
enclosingDiv.appendChild(button);
if (isMarkWatchedBtn)
dismissibleDiv.classList.remove("semitransparent");
else
dismissibleDiv.classList.add("semitransparent");
return enclosingDiv;
}
let collapsibleIdNum = 0;
function addCollapsibleBtnToSection(sectionHeader) {
try {
// only add if doesnt have it already
if (sectionHeader.parentNode.querySelector("." + COLLAPSE_CLASS) == null) {
let collapsibleId = COLLAPSE_SECTION_CHECKBOX + collapsibleIdNum++;
let collapseCheckbox = document.createElement("input");
collapseCheckbox.setAttribute("id", collapsibleId);
collapseCheckbox.setAttribute("type", "checkbox");
collapseCheckbox.checked = true;
collapseCheckbox.classList.add(COLLAPSE_CLASS);
sectionHeader.parentNode.appendChild(collapseCheckbox);
let messenger = document.getElementById(collapsibleId);
messenger.addEventListener("change", collapseSectionChanged);
}
} catch (e) {
logError(e);
}
}
function processSections() {
log("Processing sections");
let sections = document.querySelectorAll(sectionsQuery());
log("Found " + sections.length + " sections.");
for (let section of sections) {
let sectionHeader = section.querySelector(sectionTitleQuery());
// Temporary fix for PAGES.channel TODO: refactor this (when more pages added)
if (!sectionHeader) break;
let sectionTitle = sectionHeader.textContent;
// add collapse button to sections
addCollapsibleBtnToSection(sectionHeader);
// hide or show sections
if (section.querySelector(vidQuery()) == null) {
// section has no videos that arent hidden, so hide it
if (!section.classList.contains(HIDDEN_CLASS)) {
log("Hiding section '" + sectionTitle + "'");
section.style.display = 'none';
section.classList.add(HIDDEN_CLASS);
}
} else {
// section has some videos that arent hidden, in case we hid it before, show it now
if (section.classList.contains(HIDDEN_CLASS)) {
log("Showing section '" + sectionTitle + "'");
section.style.display = '';
section.classList.remove(HIDDEN_CLASS);
}
}
}
log("Processing sections... Done");
}
function removeWatchedAndAddButton() {
log("Removing watched from feed and adding overlay");
let els = document.querySelectorAll(vidQuery());
let hiddenCount = 0;
for (let item of els) {
let videoId = getVideoId(item);
let stored = videoId in storage;
let dismissableDiv = item.firstElementChild;
let button = stored ? MARK_UNWATCHED_BTN : MARK_WATCHED_BTN;
if (!stored && isYouTubeWatched(item)) {
markWatched(item, videoId);
stored = true;
} else if (stored && hideWatched) {
hideItem(item);
hiddenCount++;
}
// does it already have any button?
if (dismissableDiv.querySelector("#" + button) != null) {
continue;
} else {
dismissableDiv = dismissableDiv.firstChild;
if (!isPolymer) {
dismissableDiv = dismissableDiv.firstChild;
}
}
// stored = false - build "Mark as watched"
// stored = true - build "Mark as unwatched"
let markButton = buildMarkWatchedButton(dismissableDiv, item, getVideoId(item), !stored);
dismissableDiv.appendChild(markButton);
}
log("Removing watched from feed and adding overlay... Done");
// if we hid any videos, see if sections need changing, or videos loading
if (hiddenCount > 0) {
processSections();
loadMoreVideos();
}
}
function removeUI() {
addedElems.forEach((elem) => {
elem.parentNode.removeChild(elem);
});
addedElems = [];
// delete built buttons
document.querySelectorAll("#" + METADATA_LINE).forEach(e => e.remove());
// make hidden videos visible
for (let item of hidden) {
item.style.display = '';
item.classList.remove(HIDDEN_CLASS);
}
hidden = [];
}