Filter movies by year on IMDB search r###lts
// ==UserScript== // @name IMDb Search - Filter by Date (IA) // @namespace http://tampermonkey.net/ // @version 0.1.0 // @description Filter movies by year on IMDB search r###lts // @author You // @match https://*.imdb.com/find/?q=* // @icon https://icons.duckduckgo.com/ip2/imdb.com.ico // @grant none // ==/UserScript== (function() { 'use strict'; // Create the filter button const filterButton = document.createElement('button'); filterButton.textContent = 'Filter by date'; // Add up and down arrows to the button const upArrow = document.createElement('span'); upArrow.textContent = '▲'; upArrow.style.borderRadius = '100%'; upArrow.style.border = '1px solid #ccc'; upArrow.style.padding = '2px'; upArrow.style.margin = '0 10px 0 10px'; upArrow.style.cursor = 'pointer'; const downArrow = document.createElement('span'); downArrow.textContent = '▼'; downArrow.style.borderRadius = '100%'; downArrow.style.border = '1px solid #ccc'; downArrow.style.padding = '2px'; downArrow.style.margin = '0 10px 0 10px'; downArrow.style.cursor = 'pointer'; filterButton.appendChild(upArrow); filterButton.appendChild(downArrow); // Add the filter button to the page const listContainer = document.querySelector('section[data-testid="find-r###lts-section-title"] ul.ipc-metadata-list.ipc-metadata-list--dividers-after'); listContainer.parentNode.insertBefore(filterButton, listContainer); // Function to extract the year from a movie item function extractYear(movieItem) { const yearSpan = movieItem.querySelector('.ipc-metadata-list-summary-item__tc a + ul > li:first-of-type > span'); if (yearSpan) { const yearText = yearSpan.textContent; if (yearText.includes('–')) { const years = yearText.split('–').map(year => parseInt(year)); return { start: years[0], end: years[1] }; } else { return parseInt(yearText); } } else { return null; } } // Function to reorder movies by year function reorderMovies(ascending) { const movieItems = document.querySelectorAll('section[data-testid="find-r###lts-section-title"] ul.ipc-metadata-list.ipc-metadata-list--dividers-after li.ipc-metadata-list-summary-item.find-r###lt-item.find-title-r###lt'); const sortedMovieItems = Array.from(movieItems).sort((a, b) => { const yearA = extractYear(a); const yearB = extractYear(b); if (yearA && yearB) { if (typeof yearA === 'object' && typeof yearB === 'object') { return (yearA.start - yearB.start) * (ascending ? 1 : -1); } else if (typeof yearA === 'object') { return (yearA.start - yearB) * (ascending ? 1 : -1); } else if (typeof yearB === 'object') { return (yearA - yearB.start) * (ascending ? 1 : -1); } else { return (yearA - yearB) * (ascending ? 1 : -1); } } else if (yearA) { return -1; } else if (yearB) { return 1; } else { return 0; } }); sortedMovieItems.forEach(movieItem => { listContainer.appendChild(movieItem); }); } // Add event listeners to the filter button and arrows let ascending = true; filterButton.addEventListener('click', () => { reorderMovies(ascending); }); upArrow.addEventListener('click', () => { ascending = true; upArrow.style.background = 'green'; upArrow.style.color = 'white'; downArrow.style.background = ''; downArrow.style.color = ''; reorderMovies(ascending); }); downArrow.addEventListener('click', () => { ascending = false; downArrow.style.background = 'green'; downArrow.style.color = 'white'; upArrow.style.background = ''; upArrow.style.color = ''; reorderMovies(ascending); }); })();