-
-
Save RafalJDev/dcdc5d9f455d84addcdf0e116982aa58 to your computer and use it in GitHub Desktop.
//You need to run this in javascript console inside chrome (althought should work the same in other browsers) | |
//Assumptions: | |
//1. Will count only "expanded" videos on page, you may first need to run script to scroll to last video or do it manually | |
//2. Tested on chrome, ubuntu, 2019 | |
//3. Time format: hh:mm:ss | |
var array = document.getElementsByClassName("style-scope ytd-thumbnail-overlay-time-status-renderer"); | |
var arrLength = array.length; | |
var allHours = 0; | |
var allMinutes = 0; | |
var allSeconds = 0; | |
for (var i=0; i<arrLength; i++) { | |
var content = array[i].textContent; | |
// console.log("content: ", content); | |
var splitedTime = content.split(":"); | |
if (splitedTime.length == 1) { | |
allSeconds += +splitedTime[0]; | |
} else if (splitedTime.length == 2) { | |
allMinutes += +splitedTime[0]; | |
allSeconds += +splitedTime[1]; | |
} else if (splitedTime.length == 3) { | |
allHours += +splitedTime[0]; | |
allMinutes += +splitedTime[1]; | |
allSeconds += +splitedTime[2]; | |
} else { | |
console.log("WTF error, current content:", content); | |
} | |
} | |
var seconds = allSeconds % 60; | |
var minutes = (allMinutes % 60 + allSeconds / 60) % 60; | |
var hours = allHours + allMinutes / 60 + allSeconds / 3600; | |
// console.log("allHours:", allHours); | |
// console.log("allMinutes:", allMinutes); | |
// console.log("allSeconds:", allSeconds); | |
console.log("Hours:", hours); | |
console.log("Minutes:", minutes); | |
console.log("Seconds:", seconds); | |
//comments left for future fast debugging in case of errors, I know, bad practice | |
//Example page: https://www.youtube.com/user/DNewsChannel/videos |
The above script provides in-accurate timing, modified for the latest working
// Assuming you have your domlists as an array-like object
const domlists = document.querySelectorAll("ytd-rich-item-renderer");
var allHours = 0;
var allMinutes = 0;
var allSeconds = 0;
// Loop through each element in domlists
domlists.forEach(element => {
// Find the child element with <span> tag and the specified className
const childElement = element.querySelector("span.style-scope.ytd-thumbnail-overlay-time-status-renderer");
var splitedTime = childElement.textContent.split(":");
if (splitedTime.length == 1) {
allSeconds += +splitedTime[0];
} else if (splitedTime.length == 2) {
allMinutes += +splitedTime[0];
allSeconds += +splitedTime[1];
} else if (splitedTime.length == 3) {
allHours += +splitedTime[0];
allMinutes += +splitedTime[1];
allSeconds += +splitedTime[2];
} else {
console.log("WTF error, current content:", content);
}
});
var seconds = allSeconds % 60;
var minutes = (allMinutes % 60 + allSeconds / 60) % 60;
var hours = allHours + allMinutes / 60 + allSeconds / 3600;
console.log("Hours:", hours);
console.log("Minutes:", minutes);
console.log("Seconds:", seconds);
thanks exactly what i needed today. thx
thanks!
Cool script. I saw that it leaves decimals in minutes and hours values because of an issue in lines 38 and 39. My fix is in my fork if you want to pull it.
I just realized that the getElementsByClassName that is used to collect data into the array is triggered 8 times for every video in the videos tab of a channel. Four times the content is empty and four times it returns the time of the video. This means that the total time result is four times the actual value.
@itizarsa your solution is nice and clean. I used it in my fork.
Forked this script and added some debugging output to include video title and duration in a table to the console so I could see which videos were being counted, and how the total time was accumulating. Really helpful script, thanks for sharing it! (code's a bit messy
and has inefficiencies, I'll see if I can get to fixing it) For now though, this'll do 😄
Side note:
If you've got 2 days, 5 hours, 47 minutes, and 22 seconds free, you can watch all the long form videos from Vsauce as of today.
great...