Skip to content

Commit

Permalink
release: v2.0.2 - Part 2 (#16)
Browse files Browse the repository at this point in the history
* refactor: 'addSummaryToPage' & usage of 'isNewDesign'
* fix: Parsing of total videos stat
  • Loading branch information
nrednav authored Nov 14, 2022
1 parent ca498df commit abbad15
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 99 deletions.
43 changes: 23 additions & 20 deletions content.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const start = () => {
configurePage();
setupPlaylistObserver();
setupEventListeners();
const videos = getVideos();
const timestamps = getTimestamps(videos);
const totalDurationInSeconds = timestamps.reduce((a, b) => a + b);
const playlistDuration = formatTimestamp(totalDurationInSeconds);
const playlistSummary = createPlaylistSummary({ videos, playlistDuration });
addSummaryToPage(playlistSummary);
};

// Entry-point
if (document.readyState !== "loading") {
pollPlaylistReady();
} else {
document.addEventListener("DOMContentLoaded", () => {
pollPlaylistReady();
});
}
const start = () => {
configurePage();
setupPlaylistObserver();
setupEventListeners();
const videos = getVideos();
const timestamps = getTimestamps(videos);
const totalDurationInSeconds = timestamps.reduce((a, b) => a + b);
const playlistDuration = formatTimestamp(totalDurationInSeconds);
const playlistSummary = createPlaylistSummary({
timestamps,
playlistDuration,
});
addSummaryToPage(playlistSummary);
};

// Entry-point
if (document.readyState !== "loading") {
pollPlaylistReady();
} else {
document.addEventListener("DOMContentLoaded", () => {
pollPlaylistReady();
});
}
96 changes: 57 additions & 39 deletions library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const config = {
videoElement: "ytd-playlist-video-renderer",
videoElementsContainer: "ytd-playlist-video-list-renderer #contents",
playlistReadyAnchor: "ytd-playlist-video-list-renderer",
timestampContainer: "ytd-thumbnail-overlay-time-status-renderer",
metadataContainer: {
main: ".immersive-header-content .metadata-action-bar",
Expand All @@ -12,25 +11,14 @@ const config = {
main: ".metadata-stats yt-formatted-string",
fallback: "#stats yt-formatted-string",
},
// Design anchor = Element that helps distinguish between old & new layout
designAnchor: {
old: "ytd-playlist-sidebar-renderer",
new: "ytd-playlist-header-renderer",
},
};

// Library
const displayLoader = () => {
const playlistSummary = document.querySelector("#ytpdc-playlist-summary");
if (playlistSummary) {
const loading = document.createElement("div");
loading.style.minHeight = "128px";
loading.style.width = "100%";
loading.style.display = "flex";
loading.style.justifyContent = "center";
loading.style.alignItems = "center";
loading.textContent = "Calculating...";

playlistSummary.innerHTML = "";
playlistSummary.appendChild(loading);
}
};

const pollPlaylistReady = () => {
displayLoader();

Expand All @@ -40,7 +28,7 @@ const pollPlaylistReady = () => {
let playlistPoll = setInterval(() => {
if (pollCount >= maxPollCount) clearInterval(playlistPoll);

if (document.querySelector(config.playlistReadyAnchor)) {
if (document.querySelector(config.timestampContainer)) {
clearInterval(playlistPoll);
start();
}
Expand All @@ -49,6 +37,27 @@ const pollPlaylistReady = () => {
}, 1000);
};

const displayLoader = () => {
const playlistSummary = document.querySelector(
isNewDesign()
? "#ytpdc-playlist-summary-new"
: "#ytpdc-playlist-summary-old"
);

if (playlistSummary) {
const loading = document.createElement("div");
loading.style.minHeight = "128px";
loading.style.width = "100%";
loading.style.display = "flex";
loading.style.justifyContent = "center";
loading.style.alignItems = "center";
loading.textContent = "Calculating...";

playlistSummary.innerHTML = "";
playlistSummary.appendChild(loading);
}
};

const configurePage = () => {
if (window.ytpdc) return;
window.ytpdc = { playlistObserver: false, interPlaylistNavigation: false };
Expand All @@ -58,11 +67,8 @@ const setupPlaylistObserver = () => {
if (window.ytpdc.playlistObserver) return;
window.ytpdc.playlistObserver = true;

const playlistObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0)
pollPlaylistReady();
});
const playlistObserver = new MutationObserver((_) => {
pollPlaylistReady();
});

const targetNode = document.querySelector(config.videoElementsContainer);
Expand Down Expand Up @@ -146,9 +152,8 @@ const createSummaryItem = (label, value, valueColor = "#facc15") => {
return container;
};

const createPlaylistSummary = ({ videos, playlistDuration }) => {
const createPlaylistSummary = ({ timestamps, playlistDuration }) => {
const summaryContainer = document.createElement("div");
summaryContainer.id = "ytpdc-playlist-summary";

// Styles for new design
summaryContainer.style.display = "flex";
Expand Down Expand Up @@ -183,7 +188,7 @@ const createPlaylistSummary = ({ videos, playlistDuration }) => {
// Videos counted
const videosCounted = createSummaryItem(
"Videos counted:",
`${videos.length}`,
`${timestamps.length}`,
"#fdba74"
);
summaryContainer.appendChild(videosCounted);
Expand All @@ -192,13 +197,15 @@ const createPlaylistSummary = ({ videos, playlistDuration }) => {
const totalVideosInPlaylist = countTotalVideosInPlaylist();
const videosNotCounted = createSummaryItem(
"Videos not counted:",
`${totalVideosInPlaylist ? totalVideosInPlaylist - videos.length : "N/A"}`,
`${
totalVideosInPlaylist ? totalVideosInPlaylist - timestamps.length : "N/A"
}`,
"#fca5a5"
);
summaryContainer.appendChild(videosNotCounted);

// Tooltip
if (videos.length >= 100) {
if (timestamps.length >= 100) {
const tooltip = document.createElement("div");
tooltip.style.marginTop = "16px";
tooltip.style.display = "flex";
Expand All @@ -221,24 +228,29 @@ const createPlaylistSummary = ({ videos, playlistDuration }) => {
return summaryContainer;
};

const addSummaryToPage = (summaryContainer) => {
const addSummaryToPage = (summary) => {
const newDesign = isNewDesign();

let metadataSection = document.querySelector(
isNewDesign()
newDesign
? config.metadataContainer.main
: config.metadataContainer.fallback
);
if (!metadataSection) return null;

const playlistSummary = document.querySelector("#ytpdc-playlist-summary");
const previousSummary = document.querySelector(
newDesign ? "#ytpdc-playlist-summary-new" : "#ytpdc-playlist-summary-old"
);

if (playlistSummary) {
playlistSummary.parentNode.removeChild(playlistSummary);
if (previousSummary) {
previousSummary.parentNode.removeChild(previousSummary);
}

metadataSection.parentNode.insertBefore(
summaryContainer,
metadataSection.nextSibling
);
summary.id = newDesign
? "ytpdc-playlist-summary-new"
: "ytpdc-playlist-summary-old";

metadataSection.parentNode.insertBefore(summary, metadataSection.nextSibling);
};

const countTotalVideosInPlaylist = () => {
Expand All @@ -249,7 +261,7 @@ const countTotalVideosInPlaylist = () => {
if (!totalVideosStat) return null;

const totalVideoCount = parseInt(
totalVideosStat.firstChild.innerText.replace(/,/g, "")
totalVideosStat.innerText.replace(/\D/g, "")
);

return totalVideoCount;
Expand All @@ -260,5 +272,11 @@ const isDarkMode = () => {
};

const isNewDesign = () => {
return document.querySelector(config.metadataContainer.main) !== null;
const newDesignAnchor = document.querySelector(config.designAnchor.new);
const oldDesignAnchor = document.querySelector(config.designAnchor.old);

const isNewDesign =
newDesignAnchor && oldDesignAnchor.getAttribute("hidden") !== null;

return isNewDesign;
};
5 changes: 4 additions & 1 deletion ytpdc-firefox/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const start = () => {
const timestamps = getTimestamps(videos);
const totalDurationInSeconds = timestamps.reduce((a, b) => a + b);
const playlistDuration = formatTimestamp(totalDurationInSeconds);
const playlistSummary = createPlaylistSummary({ videos, playlistDuration });
const playlistSummary = createPlaylistSummary({
timestamps,
playlistDuration,
});
addSummaryToPage(playlistSummary);
};

Expand Down
Loading

0 comments on commit abbad15

Please sign in to comment.