Skip to content

Commit 2eb483c

Browse files
nrednavCaliforn1a
andauthored
fix: ensure all timestamps are available before processing (#24)
* fix: update logic that checks whether playlist is ready to be processed * update extension version to 2.0.4 --------- Co-authored-by: Californ1a <[email protected]>
1 parent 820c4c6 commit 2eb483c

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

library.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const pollPlaylistReady = () => {
2828
let playlistPoll = setInterval(() => {
2929
if (pollCount >= maxPollCount) clearInterval(playlistPoll);
3030

31-
if (document.querySelector(config.timestampContainer)) {
31+
if (
32+
document.querySelector(config.timestampContainer) &&
33+
countUnavailableTimestamps() === countUnavailableVideos()
34+
) {
3235
clearInterval(playlistPoll);
3336
start();
3437
}
@@ -89,7 +92,12 @@ const setupEventListeners = () => {
8992
};
9093

9194
const getVideos = () => {
92-
const videos = document.getElementsByTagName(config.videoElement);
95+
const videoElementsContainer = document.querySelector(
96+
config.videoElementsContainer
97+
);
98+
const videos = videoElementsContainer.getElementsByTagName(
99+
config.videoElement
100+
);
93101
return [...videos];
94102
};
95103

@@ -203,8 +211,7 @@ const createPlaylistSummary = ({ timestamps, playlistDuration }) => {
203211
const totalVideosInPlaylist = countTotalVideosInPlaylist();
204212
const videosNotCounted = createSummaryItem(
205213
"Videos not counted:",
206-
`${
207-
totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
214+
`${totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
208215
}`,
209216
"#fca5a5"
210217
);
@@ -273,6 +280,34 @@ const countTotalVideosInPlaylist = () => {
273280
return totalVideoCount;
274281
};
275282

283+
const countUnavailableVideos = () => {
284+
const unavailableVideoTitles = [
285+
"[Private video]",
286+
"[Deleted video]",
287+
"[Unavailable]",
288+
"[Video unavailable]",
289+
"[Restricted video]",
290+
"[Age restricted]",
291+
];
292+
293+
const videoTitles = document.querySelectorAll("a#video-title");
294+
295+
let unavailableVideosCount = 0;
296+
297+
videoTitles.forEach((videoTitle) => {
298+
if (unavailableVideoTitles.includes(videoTitle.title)) {
299+
unavailableVideosCount++;
300+
}
301+
});
302+
303+
return unavailableVideosCount;
304+
};
305+
306+
const countUnavailableTimestamps = () => {
307+
const timestamps = getTimestamps(getVideos());
308+
return timestamps.filter((timestamp) => timestamp === null).length;
309+
};
310+
276311
const isDarkMode = () => {
277312
return document.documentElement.getAttribute("dark") !== null;
278313
};

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Youtube Playlist Duration Calculator",
44
"short_name": "YTPD Calculator",
55
"description": "An extension to calculate & display the total duration of a youtube playlist.",
6-
"version": "2.0.3",
6+
"version": "2.0.4",
77
"icons": {
88
"128": "icon128.png",
99
"48": "icon48.png",

ytpdc-firefox/library.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const pollPlaylistReady = () => {
2828
let playlistPoll = setInterval(() => {
2929
if (pollCount >= maxPollCount) clearInterval(playlistPoll);
3030

31-
if (document.querySelector(config.timestampContainer)) {
31+
if (
32+
document.querySelector(config.timestampContainer) &&
33+
countUnavailableTimestamps() === countUnavailableVideos()
34+
) {
3235
clearInterval(playlistPoll);
3336
start();
3437
}
@@ -89,7 +92,12 @@ const setupEventListeners = () => {
8992
};
9093

9194
const getVideos = () => {
92-
const videos = document.getElementsByTagName(config.videoElement);
95+
const videoElementsContainer = document.querySelector(
96+
config.videoElementsContainer
97+
);
98+
const videos = videoElementsContainer.getElementsByTagName(
99+
config.videoElement
100+
);
93101
return [...videos];
94102
};
95103

@@ -203,8 +211,7 @@ const createPlaylistSummary = ({ timestamps, playlistDuration }) => {
203211
const totalVideosInPlaylist = countTotalVideosInPlaylist();
204212
const videosNotCounted = createSummaryItem(
205213
"Videos not counted:",
206-
`${
207-
totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
214+
`${totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
208215
}`,
209216
"#fca5a5"
210217
);
@@ -273,6 +280,34 @@ const countTotalVideosInPlaylist = () => {
273280
return totalVideoCount;
274281
};
275282

283+
const countUnavailableVideos = () => {
284+
const unavailableVideoTitles = [
285+
"[Private video]",
286+
"[Deleted video]",
287+
"[Unavailable]",
288+
"[Video unavailable]",
289+
"[Restricted video]",
290+
"[Age restricted]",
291+
];
292+
293+
const videoTitles = document.querySelectorAll("a#video-title");
294+
295+
let unavailableVideosCount = 0;
296+
297+
videoTitles.forEach((videoTitle) => {
298+
if (unavailableVideoTitles.includes(videoTitle.title)) {
299+
unavailableVideosCount++;
300+
}
301+
});
302+
303+
return unavailableVideosCount;
304+
};
305+
306+
const countUnavailableTimestamps = () => {
307+
const timestamps = getTimestamps(getVideos());
308+
return timestamps.filter((timestamp) => timestamp === null).length;
309+
};
310+
276311
const isDarkMode = () => {
277312
return document.documentElement.getAttribute("dark") !== null;
278313
};

ytpdc-firefox/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Youtube Playlist Duration Calculator",
44
"short_name": "YTPD Calculator",
55
"description": "An extension to calculate & display the total duration of a youtube playlist.",
6-
"version": "2.0.3",
6+
"version": "2.0.4",
77
"icons": {
88
"128": "icon128.png",
99
"48": "icon48.png",

0 commit comments

Comments
 (0)