diff --git a/library.js b/library.js index b647234..3b6980a 100644 --- a/library.js +++ b/library.js @@ -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(); } @@ -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]; }; @@ -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" ); @@ -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; }; diff --git a/manifest.json b/manifest.json index f5ca241..4998826 100755 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/ytpdc-firefox/library.js b/ytpdc-firefox/library.js index b647234..3b6980a 100644 --- a/ytpdc-firefox/library.js +++ b/ytpdc-firefox/library.js @@ -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(); } @@ -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]; }; @@ -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" ); @@ -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; }; diff --git a/ytpdc-firefox/manifest.json b/ytpdc-firefox/manifest.json index 7d34986..a224991 100644 --- a/ytpdc-firefox/manifest.json +++ b/ytpdc-firefox/manifest.json @@ -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",