Skip to content

Instantly share code, notes, and snippets.

@Ghost---Shadow
Last active July 28, 2021 08:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ghost---Shadow/23a484a3fe69b20aee6e7b5edbf40284 to your computer and use it in GitHub Desktop.
Save Ghost---Shadow/23a484a3fe69b20aee6e7b5edbf40284 to your computer and use it in GitHub Desktop.
Sorts the disable_polymer=true Youtube Watch Later playlist by video length
/**
USAGE
1. Open the playlist you want
2. Right click > Inspect element > Console
3. Copy this and paste. Hit enter.
4. For watch later make sure to click the Edit button
EDIT: Not all playlists have hours.
*/
const table = document.getElementById("pl-video-table")
const rows = Array.from(table.rows)
const toSeconds = s => s.split(':').map(v => parseInt(v)).reverse().reduce((acc,e,i) => acc + e * Math.pow(60,i))
const getTimestamp = row => toSeconds(row.children[6].children[0].children[0].innerText)
const sortedRows = rows.sort((row1, row2) => getTimestamp(row1) > getTimestamp(row2))
sortedRows.forEach(row => table.appendChild(row))
// For also printing the total playlist length
let seconds = rows.reduce((acc,e) => acc + getTimestamp(e),0)
let mins = parseInt(seconds / 60)
seconds %= 60
let hours = parseInt(mins / 60)
mins %= 60
console.log(hours+":"+mins+":"+seconds)
@Ghost---Shadow
Copy link
Author

// TODO: Handle NaN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment