IMDb Search - Filter by Date (IA)

Filter movies by year on IMDB search results

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
  });
})();