IMDb Search - Filter by Date (IA)

Filter movies by year on IMDB search results

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         IMDb Search - Filter by Date (IA)
// @namespace    http://tampermonkey.net/
// @version      2.0.0
// @description  Filter movies by year on IMDB search results
// @author       You
// @match        https://*.imdb.com/find/?q=*
// @match        https://www.imdb.com/fr*/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-results-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-results-section-title"] ul.ipc-metadata-list.ipc-metadata-list--dividers-after li.ipc-metadata-list-summary-item.find-result-item.find-title-result');
    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);
  });
})();