Skip to content

Commit

Permalink
fix: ensure all timestamps are available before processing (#24)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
nrednav and Californ1a authored Oct 12, 2023
1 parent 820c4c6 commit 2eb483c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
43 changes: 39 additions & 4 deletions library.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const pollPlaylistReady = () => {
let playlistPoll = setInterval(() => {
if (pollCount >= maxPollCount) clearInterval(playlistPoll);

if (document.querySelector(config.timestampContainer)) {
if (
document.querySelector(config.timestampContainer) &&
countUnavailableTimestamps() === countUnavailableVideos()
) {
clearInterval(playlistPoll);
start();
}
Expand Down Expand Up @@ -89,7 +92,12 @@ const setupEventListeners = () => {
};

const getVideos = () => {
const videos = document.getElementsByTagName(config.videoElement);
const videoElementsContainer = document.querySelector(
config.videoElementsContainer
);
const videos = videoElementsContainer.getElementsByTagName(
config.videoElement
);
return [...videos];
};

Expand Down Expand Up @@ -203,8 +211,7 @@ const createPlaylistSummary = ({ timestamps, playlistDuration }) => {
const totalVideosInPlaylist = countTotalVideosInPlaylist();
const videosNotCounted = createSummaryItem(
"Videos not counted:",
`${
totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
`${totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
}`,
"#fca5a5"
);
Expand Down Expand Up @@ -273,6 +280,34 @@ const countTotalVideosInPlaylist = () => {
return totalVideoCount;
};

const countUnavailableVideos = () => {
const unavailableVideoTitles = [
"[Private video]",
"[Deleted video]",
"[Unavailable]",
"[Video unavailable]",
"[Restricted video]",
"[Age restricted]",
];

const videoTitles = document.querySelectorAll("a#video-title");

let unavailableVideosCount = 0;

videoTitles.forEach((videoTitle) => {
if (unavailableVideoTitles.includes(videoTitle.title)) {
unavailableVideosCount++;
}
});

return unavailableVideosCount;
};

const countUnavailableTimestamps = () => {
const timestamps = getTimestamps(getVideos());
return timestamps.filter((timestamp) => timestamp === null).length;
};

const isDarkMode = () => {
return document.documentElement.getAttribute("dark") !== null;
};
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Youtube Playlist Duration Calculator",
"short_name": "YTPD Calculator",
"description": "An extension to calculate & display the total duration of a youtube playlist.",
"version": "2.0.3",
"version": "2.0.4",
"icons": {
"128": "icon128.png",
"48": "icon48.png",
Expand Down
43 changes: 39 additions & 4 deletions ytpdc-firefox/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const pollPlaylistReady = () => {
let playlistPoll = setInterval(() => {
if (pollCount >= maxPollCount) clearInterval(playlistPoll);

if (document.querySelector(config.timestampContainer)) {
if (
document.querySelector(config.timestampContainer) &&
countUnavailableTimestamps() === countUnavailableVideos()
) {
clearInterval(playlistPoll);
start();
}
Expand Down Expand Up @@ -89,7 +92,12 @@ const setupEventListeners = () => {
};

const getVideos = () => {
const videos = document.getElementsByTagName(config.videoElement);
const videoElementsContainer = document.querySelector(
config.videoElementsContainer
);
const videos = videoElementsContainer.getElementsByTagName(
config.videoElement
);
return [...videos];
};

Expand Down Expand Up @@ -203,8 +211,7 @@ const createPlaylistSummary = ({ timestamps, playlistDuration }) => {
const totalVideosInPlaylist = countTotalVideosInPlaylist();
const videosNotCounted = createSummaryItem(
"Videos not counted:",
`${
totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
`${totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
}`,
"#fca5a5"
);
Expand Down Expand Up @@ -273,6 +280,34 @@ const countTotalVideosInPlaylist = () => {
return totalVideoCount;
};

const countUnavailableVideos = () => {
const unavailableVideoTitles = [
"[Private video]",
"[Deleted video]",
"[Unavailable]",
"[Video unavailable]",
"[Restricted video]",
"[Age restricted]",
];

const videoTitles = document.querySelectorAll("a#video-title");

let unavailableVideosCount = 0;

videoTitles.forEach((videoTitle) => {
if (unavailableVideoTitles.includes(videoTitle.title)) {
unavailableVideosCount++;
}
});

return unavailableVideosCount;
};

const countUnavailableTimestamps = () => {
const timestamps = getTimestamps(getVideos());
return timestamps.filter((timestamp) => timestamp === null).length;
};

const isDarkMode = () => {
return document.documentElement.getAttribute("dark") !== null;
};
Expand Down
2 changes: 1 addition & 1 deletion ytpdc-firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Youtube Playlist Duration Calculator",
"short_name": "YTPD Calculator",
"description": "An extension to calculate & display the total duration of a youtube playlist.",
"version": "2.0.3",
"version": "2.0.4",
"icons": {
"128": "icon128.png",
"48": "icon48.png",
Expand Down

0 comments on commit 2eb483c

Please sign in to comment.