Created
November 13, 2023 10:58
-
-
Save cemerson/5bfe365d80157662cef8408f8699a89e to your computer and use it in GitHub Desktop.
Rumble.com: Bookmarklet sort video search results by duration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Rumble.com: Sort video search results page by duration (low to high) */ javascript:(function() {%0A %2F%2F Get all the <li> elements with the class 'video-listing-entry'%0A const videoEntries %3D document.querySelectorAll('li.video-listing-entry')%3B%0A%0A %2F%2F Convert the NodeList to an array for easier sorting%0A const videoEntriesArray %3D Array.from(videoEntries)%3B%0A%0A %2F%2F Custom sorting function to sort by video duration%0A function compareDurations(a%2C b) %7B%0A %2F%2F Get the duration values from the data-value attribute of <span> elements%0A const durationA %3D a.querySelector('.video-item--duration').getAttribute('data-value')%3B%0A const durationB %3D b.querySelector('.video-item--duration').getAttribute('data-value')%3B%0A%0A %2F%2F Convert the duration strings to time in seconds for comparison%0A const timeA %3D convertDurationToSeconds(durationA)%3B%0A const timeB %3D convertDurationToSeconds(durationB)%3B%0A%0A %2F%2F Compare and return the result%0A return timeA - timeB%3B%0A %7D%0A%0A %2F%2F Function to convert duration strings to time in seconds%0A function convertDurationToSeconds(duration) %7B%0A %2F%2F Split the duration into parts (hours%2C minutes%2C seconds)%0A const parts %3D duration.split('%3A').map(Number)%3B%0A %0A if (parts.length %3D%3D%3D 3) %7B%0A %2F%2F If there are three parts (hh%3Amm%3Ass)%2C convert to seconds%0A return parts%5B0%5D * 3600 %2B parts%5B1%5D * 60 %2B parts%5B2%5D%3B%0A %7D else if (parts.length %3D%3D%3D 2) %7B%0A %2F%2F If there are two parts (mm%3Ass)%2C consider it as mm minutes and ss seconds%0A return parts%5B0%5D * 60 %2B parts%5B1%5D%3B%0A %7D else %7B%0A %2F%2F Handle invalid duration format%0A return 0%3B %2F%2F You may want to handle this differently%0A %7D%0A%7D%0A%0A %2F%2F Create a container for the sorted list or find the existing container%0A let videoListContainer %3D document.querySelector('.video-list')%3B%0A%0A %2F%2F Check if the container element was found or create one%0A if (!videoListContainer) %7B%0A videoListContainer %3D document.createElement('ul')%3B%0A videoListContainer.className %3D 'video-list'%3B %2F%2F Add the class to the created container%0A %7D%0A%0A %2F%2F Sort the array of <li> elements based on video duration%0A videoEntriesArray.sort(compareDurations)%3B%0A%0A %2F%2F Clear the current list and append the sorted list%0A videoListContainer.innerHTML %3D ''%3B%0A%0A videoEntriesArray.forEach((entry) %3D> {%0A videoListContainer.appendChild(entry)%3B %2F%2F Append the sorted entries%0A })%3B%0A%0A %2F%2F Append the container to the parent element%0A const parentElement %3D document.querySelector('.main-and-sidebar ol')%3B%0A if (parentElement) {%0A parentElement.appendChild(videoListContainer)%3B%0A %7D else {%0A console.error('Parent element not found.')%3B%0A %7D%0A} )()%3B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May break eventually but still working as of 11/2024