-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsubs-ui.js
More file actions
338 lines (271 loc) · 11.6 KB
/
subs-ui.js
File metadata and controls
338 lines (271 loc) · 11.6 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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 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;
menuButtonContainer = document.createElement("h2");
menuButtonContainer.classList.add("yt-simple-endpoint");
menuButtonContainer.classList.add("style-scope");
menuButtonContainer.classList.add("ytd-compact-link-renderer");
menuButtonContainer.classList.add("subs-grid-menu-item");
return menuButtonContainer;
}
function deleteOldButton(ID) {
const oldButton = document.querySelector(`#${ID}`);
if (oldButton) {
oldButton.remove();
}
}
function addSettingsButton() {
deleteOldButton(SETTINGS_BTN);
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"]) {
deleteOldButton(MARK_ALL_WATCHED_BTN);
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"]) {
deleteOldButton(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);
}
deleteOldButton(HIDE_WATCHED_TOGGLE);
let toggleContainer = document.createElement("div");
toggleContainer.setAttribute("id", HIDE_WATCHED_TOGGLE);
toggleContainer.classList.add("toggle-container", "style-scope", "tp-yt-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", "tp-yt-paper-toggle-button");
let toggleButton = document.createElement("div");
toggleButton.classList.add("toggle-button", "style-scope", "tp-yt-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");
// Try multiple possible insertion points for different YouTube layouts
let insertionPoint = document.getElementById("end") || // Old layout
document.querySelector("ytd-feed-filter-chip-bar-renderer") || // New layout chip bar
document.querySelector("#primary ytd-rich-grid-renderer") || // Grid container
document.querySelector("ytd-browse[page-subtype='subscriptions']"); // Page container
if (insertionPoint != null) {
if (insertionPoint.id === "end") {
// Old layout behavior
if (settings["settings.hide.watched.ui.stick.right"])
insertionPoint.prepend(element);
else
insertionPoint.parentNode.insertBefore(element, insertionPoint);
} else {
// New layout behavior - respect stick-right setting
if (settings["settings.hide.watched.ui.stick.right"]) {
insertionPoint.appendChild(element);
} else {
insertionPoint.insertBefore(element, insertionPoint.firstChild);
}
}
addedElems.push(element);
} else {
logError({"message": "Could not find UI insertion point", "stack": "subs-ui.js:addElementToMenuUI"});
}
}
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");
let vid = new SubscriptionVideo(item);
if (isMarkWatchedBtn) {
button.onclick = () => {
vid.markWatched();
};
} else {
button.onclick = () => {
vid.markUnwatched();
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;
// Ignore for list view
if (section.classList.contains("ytd-section-list-renderer")) 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)) {
logDebug("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)) {
logDebug("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 vid = new SubscriptionVideo(item);
if (!vid.isStored && isYouTubeWatched(item)) {
vid.markWatched();
} else if (vid.shouldHide()) {
vid.hide();
hiddenCount++;
}
// does it already have any button?
if (!vid.hasButton()) {
vid.addButton();
}
}
const gridElement = document.querySelector('ytd-two-column-browse-results-renderer ytd-rich-grid-renderer #contents');
// hide "Most Relevant" shelf if setting enabled
if (hideMostRelevant && gridElement) {
[...gridElement.querySelectorAll(':scope > ytd-rich-section-renderer')].forEach(section => {
const richShelf = section.querySelector(':scope > #content > ytd-rich-shelf-renderer:not([is-shorts])');
if (richShelf) {
section.style.display = 'none';
}
});
}
// if shorts shelf is empty, hide it
if (gridElement && isRendered(gridElement)) {
[...gridElement.querySelectorAll(':scope > ytd-rich-section-renderer')].forEach(richSectionElement => {
const contents = richSectionElement.querySelector(':scope > #content > ytd-rich-shelf-renderer > #dismissible #contents');
if (!contents) {
return;
}
if (![...contents.children].some(child => isRendered(child))) {
richSectionElement.style.display = 'none';
}
});
}
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.remove(); // Safe - doesn't throw if already removed from DOM
});
addedElems = [];
// delete built buttons and strip containers
document.querySelectorAll(".subs-btn-container").forEach(e => e.remove());
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 = [];
}
function rebuildUI() {
if (isSubscriptionsPage()) {
try {
removeUI();
buildUI();
} catch (e) {
logError(e);
}
}
}