Discussies » Ontwikkeling

Please help with script that replaces IMDB synopsis with Wikipedia one

§
Geplaatst: 16-09-2024

Code works in some pages but not in others and i'm struggling to identify the problem. Example page where it does not work, despite there being a valid wikipedia entry for the IMDB title: https://www.imdb.com/title/tt8550732/


// ==UserScript==
// @name IMDb Synopsis Replacer with Wikipedia
// @namespace http://tampermonkey.net/
// @version 1.8
// @description Replace IMDb movie synopsis with Wikipedia's synopsis, using a simplified method to tag as either 'TV series' or 'Movie' based on media type
// @author You
// @match https://www.imdb.com/title/*
// @grant none
// ==/UserScript==

(function() {
'use strict';

// Function to fetch Wikipedia page directly
function fetchWikipediaPage(title, mediaType) {
// Construct Wikipedia URL with title and media type in parentheses
const wikipediaUrl = `https://en.wikipedia.org/wiki/${encodeURIComponent(title)}_(${encodeURIComponent(mediaType)})`;

return fetch(wikipediaUrl)
.then(response => response.text())
.then(pageContent => {
// Check if it's a disambiguation page
if (pageContent.includes("may refer to:")) {
return "Disambiguation page found. Could not fetch the synopsis.";
}

// Try to locate the synopsis or plot section
const synopsisMatch = pageContent.match(/(.+?)<\/span>[\s\S]*?

(.+?)<\/p>/);
return synopsisMatch ? synopsisMatch[3] : "Synopsis not found on Wikipedia.";
})
.catch(error => {
console.error("Error fetching Wikipedia page:", error);
return "Failed to fetch Wikipedia synopsis.";
});
}

// Function to get media type from IMDb and determine if it's TV series or movie
function getMediaType() {
// IMDb lists the media type in


  • const mediaTypeElement = document.querySelectorAll('li[role="presentation"].ipc-inline-list__item');
    let mediaType = "Movie"; // Default to Movie

    // Loop through all the list items to check for "tv"
    mediaTypeElement.forEach(item => {
    const text = item.innerText.toLowerCase();
    if (text.includes("tv")) {
    mediaType = "TV series"; // Change to TV series if 'tv' is found
    }
    });

    return mediaType;
    }

    // Function to replace IMDb synopsis
    function replaceIMDBSynopsis() {
    // IMDb movie title (assuming it's in the

    tag)
    const movieTitle = document.querySelector('h1').innerText.trim();

    // Get media type (e.g., TV series or Movie)
    const mediaType = getMediaType();

    // Target the element with 'data-testid="plot-xl"' and 'role="presentation"'
    const synopsisElement = document.querySelector('span[data-testid="plot-xl"][role="presentation"]');

    if (synopsisElement) {
    // Fetch the Wikipedia page and replace IMDb's synopsis
    fetchWikipediaPage(movieTitle, mediaType).then(synopsis => {
    synopsisElement.innerHTML = synopsis;
    });
    }
    }

    // Run the script after the page loads
    window.addEventListener('load', replaceIMDBSynopsis);
    })();

  • Reactie plaatsen

    Log in om antwoord te geven.