IMDB to 豆瓣

Adds a link to jump to Douban page using IMDb ID and automatically clicks on the first result

目前為 2024-06-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name        IMDB to 豆瓣
// @namespace   Violentmonkey Scripts
// @match       *://*.imdb.com/title/*
// @match       *://movie.douban.com/subject_search?search_text=*
// @author      int-xu
// @license     MIT
// @grant       none
// @version     1.3
// @description Adds a link to jump to Douban page using IMDb ID and automatically clicks on the first result
// ==/UserScript==

(function() {
  'use strict';

  // Function to create and insert the Douban button on IMDb
  function addDoubanButton() {
    // Extract the IMDb ID from the URL
    const imdbIdMatch = window.location.pathname.match(/title\/(tt\d+)/);
    if (imdbIdMatch && imdbIdMatch[1]) {
      const imdbId = imdbIdMatch[1];

      // Construct the Douban URL using the IMDb ID
      const doubanUrl = `https://movie.douban.com/subject_search?search_text=${imdbId}&cat=1002`;

      // Create a new button element
      const doubanButton = document.createElement('a');
      doubanButton.href = doubanUrl;
      doubanButton.textContent = 'Douban';
      doubanButton.target = '_blank';
      doubanButton.style = 'padding: 2px 6px; background-color: #00a680; color: white; border-radius: 3px; text-decoration: none; font-size: 14px; margin-left: 10px;';

      // Find the element to insert the button next to (e.g., next to the title)
      const titleElement = document.querySelector('h1');

      // Insert the button into the page
      if (titleElement) {
        titleElement.parentElement.appendChild(doubanButton);
      }
    }
  }

  // Function to click the first search result on Douban
  function clickFirstResult() {
    // Wait for the DOM to fully load
    window.addEventListener('load', function() {
      // Select the first search result link
      const firstResult = document.querySelector('.title a');

      if (firstResult) {
        // Navigate to the first result link
        window.location.href = firstResult.href;
      }
    });
  }

  // Determine if we are on IMDb or Douban
  if (window.location.hostname.includes('imdb.com')) {
    // Wait for the DOM to fully load before running the script
    window.addEventListener('load', addDoubanButton);
  } else if (window.location.hostname.includes('douban.com') && window.location.search.includes('search_text=tt')) {
    // Only execute on Douban when search_text includes an IMDb ID pattern
    clickFirstResult();
  }
})();