-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
318 lines (273 loc) · 13.1 KB
/
content.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
310
311
312
313
314
315
316
317
318
let mutationObserver;
let actionsMenuReadyDispatched = false;
let waitAfterFirstElMatch = false;
const actionsMenuReady = new CustomEvent('actionsMenuReady');
function onWatchLaterUrl() {
mutationObserver = new MutationObserver(function (mutations) {
mutations.forEach(async function (mutation, index) {
if (actionsMenuReadyDispatched && !document.querySelector('#delComplWatched'))
actionsMenuReadyDispatched = false;
if (!actionsMenuReadyDispatched) {
let actionMenu = null;
let queriedElement = elementToObserve.querySelector('ytd-menu-popup-renderer > tp-yt-paper-listbox');
if (isWatchLaterUrl) {
if (queriedElement && queriedElement.lastElementChild && queriedElement.lastElementChild.tagName == 'YTD-MENU-NAVIGATION-ITEM-RENDERER') {
actionMenu = queriedElement;
}
} else {
if (queriedElement && queriedElement.lastElementChild && queriedElement.childElementCount == 5) {
actionMenu = queriedElement;
}
}
if (actionMenu) {
document.dispatchEvent(actionsMenuReady);
actionsMenuReadyDispatched = true;
}
}
});
});
document.addEventListener('actionsMenuReady', async () => {
await customizeActionsMenu();
});
function waitUntilActionsMenuIsReady() {
return new Promise((resolve) => {
const handleEvent = () => {
console.log('actionsMenuReady!!!');
document.removeEventListener('actionsMenuReady', handleEvent);
resolve();
};
document.addEventListener('actionsMenuReady', handleEvent);
});
}
async function customizeActionsMenu() {
observeMenuPopupRenderer();
await waitUntilActionsMenuIsReady();
let delComplWatchedAdded = document.querySelector('#delComplWatched') != null;
if (!delComplWatchedAdded) {
let delItem = document.querySelectorAll("ytd-menu-service-item-renderer")[document.querySelectorAll("ytd-menu-service-item-renderer").length - 1];
let delItem2 = document.querySelectorAll("ytd-menu-navigation-item-renderer")[document.querySelectorAll("ytd-menu-navigation-item-renderer").length - 1];
let text = '';
if (delItem)
text = delItem.querySelector('yt-formatted-string').textContent;
let text2 = '';
if (delItem2)
text2 = delItem2.querySelector('yt-formatted-string').textContent;
const item = document.createElement('div');
item.setAttribute('id', 'delComplWatched');
item.classList.add('wvoytr-action-menu-item');
item.style.cursor = 'pointer';
item.style.fontSize = '14px';
item.style.padding = '4px 0';
item.style.overflow = 'hidden';
item.style.alignItems = 'center';
const logo = document.createElement('img');
logo.src = chrome.runtime.getURL("images/icon16.png");
if (delItem) {
delItem.parentElement.appendChild(item);
} else if (delItem2) {
delItem2.parentElement.appendChild(item);
}
let delComplWatchedVideosBtn = document.querySelector("#delComplWatched");
delComplWatchedVideosBtn.style.display = 'flex';
delComplWatchedVideosBtn.style.marginTop = '2px';
const logoDiv = document.createElement('div');
logoDiv.style.marginLeft = '8px';
logoDiv.style.display = 'flex';
//logoDiv.style.justifyContent = 'center';
//logoDiv.style.alignItems = 'center';
logoDiv.appendChild(logo);
const textDiv = document.createElement('div');
textDiv.style.marginLeft = '8px';
textDiv.style.marginRight = '8px';
textDiv.style.whiteSpace = 'nowrap';
textDiv.innerHTML = text === 'Videos hinzufügen' || text === 'Playlist löschen' || text2 === 'Gesehene Videos entfernen' ? 'Vollständig gesehene Videos entfernen' : 'Remove completely watched videos';
delComplWatchedVideosBtn.appendChild(logoDiv);
delComplWatchedVideosBtn.appendChild(textDiv);
// Input field for percentage threshold
const percentageInputDiv = document.createElement('div');
percentageInputDiv.style.display = 'flex';
percentageInputDiv.style.alignItems = 'center';
percentageInputDiv.style.marginLeft = '8px';
percentageInputDiv.style.marginRight = '8px';
percentageInputDiv.style.marginTop = '0';
// Label for the input
const percentageLabel = document.createElement('span');
percentageLabel.innerHTML = 'Threshold: ';
percentageLabel.style.marginRight = '8px';
// Input field for percentage
const percentageInput = document.createElement('input');
percentageInput.type = 'number';
percentageInput.min = '0';
percentageInput.max = '100';
percentageInput.value = '100';
percentageInput.style.width = '50px';
percentageInput.style.padding = '4px';
percentageInput.style.margin = '0';
percentageInput.style.boxSizing = 'border-box';
percentageInput.style.height = 'auto';
// Unit for percentage
const percentageUnit = document.createElement('span');
percentageUnit.innerHTML = '%';
percentageUnit.style.marginLeft = '8px';
percentageUnit.style.marginRight = '4px';
// Append elements
percentageInputDiv.appendChild(percentageLabel);
percentageInputDiv.appendChild(percentageInput);
percentageInputDiv.appendChild(percentageUnit);
// Append the percentage div to the main container
delComplWatchedVideosBtn.appendChild(percentageInputDiv);
// Event listener for removing videos
delComplWatchedVideosBtn.addEventListener('click', async (event) => {
if (event.target === percentageInput || event.target === percentageLabel) {
event.stopPropagation();
return;
}
const userPercentage = parseInt(percentageInput.value, 10) || 100;
console.log(`Removing videos watched at least ${userPercentage}%`);
list = document.querySelector("#contents .ytd-section-list-renderer").querySelector("#contents").querySelector("#contents").querySelectorAll("ytd-playlist-video-renderer");
remove(userPercentage);
document.querySelector("ytd-playlist-header-renderer").click();
});
// Use MutationObserver to remove max-width after the element is rendered
const popupRenderer = document.querySelector("ytd-menu-popup-renderer");
const popupObserver = new MutationObserver(function (mutations) {
mutations.forEach((mutation) => {
if (mutation.attributeName === "style" && popupRenderer.style.maxWidth) {
popupRenderer.style.maxWidth = 'none'; // Remove the max-width
}
});
});
// Observe changes to the styles of the popup renderer
popupObserver.observe(popupRenderer, { attributes: true });
}
}
function observeMenuPopupRenderer() {
const menuPopupRendererObserver = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
const target = mutation.target;
if (target.style.maxHeight !== '') {
target.style.maxHeight = '';
}
}
}
});
const menuPopupRenderer = document.querySelector('ytd-menu-popup-renderer');
if (menuPopupRenderer) {
menuPopupRendererObserver.observe(menuPopupRenderer, {
attributes: true,
attributeFilter: ['style'],
});
}
}
let elementToObserve = document.querySelector('ytd-popup-container');
mutationObserver.observe(elementToObserve, {
attributes: true,
childList: true,
subtree: true,
});
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function remove(userPercentage) {
let list = document.querySelectorAll("ytd-playlist-video-renderer");
let totalItemsToRemove = 0;
for (let el of list) {
let pgBar = el.querySelectorAll("#content")[0].querySelector("#progress");
if (pgBar && parseFloat(pgBar.style.width) >= userPercentage) {
totalItemsToRemove++;
}
}
const progressBarContainer = document.querySelector('#progressBarContainer');
progressBarContainer.style.display = 'block';
let itemsProcessed = 0;
for (let el of list) {
let pgBar = el.querySelectorAll("#content")[0].querySelector("#progress");
if (pgBar && parseFloat(pgBar.style.width) >= userPercentage) {
el.querySelector("#menu").querySelector("#interaction").click();
await wait(500);
if (document.querySelector("ytd-popup-container tp-yt-iron-dropdown").style.display == '') {
if (isWatchLaterUrl) {
document.querySelector("ytd-menu-popup-renderer").querySelector("tp-yt-paper-listbox").children[2].click();
} else {
document.querySelector("ytd-menu-popup-renderer").querySelector("tp-yt-paper-listbox").children[3].click();
}
}
await wait(1000);
itemsProcessed++;
updateProgressBar((itemsProcessed / totalItemsToRemove) * 100);
}
}
document.querySelector("ytd-popup-container tp-yt-iron-dropdown").style.display = 'none';
hideProgressBar();
}
}
const regex = /^https:\/\/www\.youtube\.com\/playlist\?list=.+$/;
let isWatchLaterUrl = false;
function checkAndUpdate() {
isWatchLaterUrl = false;
if (window.location.href === "https://www.youtube.com/playlist?list=WL") {
createProgressBar();
onWatchLaterUrl();
isWatchLaterUrl = true;
} else if (regex.test(window.location.href)) {
createProgressBar();
onWatchLaterUrl();
} else {
if (mutationObserver) {
mutationObserver.disconnect();
}
const delComplWatchedVideosBtn = document.querySelector("#delComplWatched");
if (delComplWatchedVideosBtn) {
delComplWatchedVideosBtn.remove();
}
actionsMenuReadyDispatched = false;
waitAfterFirstElMatch = false;
}
}
checkAndUpdate();
const targetNode = document.querySelector('title');
const config = { childList: true };
const callback = function (mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
checkAndUpdate();
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
function createProgressBar() {
if (document.querySelector('#progressBarContainer')) {
return;
}
const progressBarContainer = document.createElement('div');
progressBarContainer.style.position = 'fixed';
progressBarContainer.style.top = '0';
progressBarContainer.style.left = '0';
progressBarContainer.style.width = '100%';
progressBarContainer.style.height = '5px';
progressBarContainer.style.backgroundColor = '#ddd';
progressBarContainer.style.zIndex = '9999';
progressBarContainer.style.display = 'none';
progressBarContainer.id = 'progressBarContainer';
const progressBar = document.createElement('div');
progressBar.style.height = '5px';
progressBar.style.width = '0%';
progressBar.style.backgroundColor = '#4CAF50';
progressBar.id = 'progressBar';
progressBarContainer.appendChild(progressBar);
document.body.appendChild(progressBarContainer);
}
function updateProgressBar(percentage) {
const progressBar = document.querySelector('#progressBar');
if (progressBar) {
progressBar.style.width = `${percentage}%`;
}
}
function hideProgressBar() {
const progressBarContainer = document.querySelector('#progressBarContainer');
if (progressBarContainer) {
progressBarContainer.style.display = 'none';
}
}