Letterboxd IMDb Code Extractor

Extract and copy IMDb code on Letterboxd film pages

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

Advertisement:

// ==UserScript==
// @name         Letterboxd IMDb Code Extractor
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Extract and copy IMDb code on Letterboxd film pages
// @author       You
// @match        https://letterboxd.com/film/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create a button that matches the IMDb button style
    function createMatchingButton(imdbCode) {
        const button = document.createElement('button');
        button.textContent = imdbCode;
        // Apply provided styles
        button.style.background = '#14181c'; // Background color as per request
        button.style.border = '2px solid #303840';
        button.style.borderRadius = '2px';
        button.style.color = '#9ab';
        button.style.fontSize = '.76923077rem';
        button.style.letterSpacing = '.075em';
        button.style.padding = '4px 5px 3px';
        button.style.textTransform = 'uppercase';
        button.style.cursor = 'pointer';
        button.style.marginLeft = '10px'; // Space from the existing TMDB button
        button.onclick = function() {
            navigator.clipboard.writeText(imdbCode).then(function() {
                console.log('IMDb code copied to clipboard');
            }, function(err) {
                console.error('Could not copy IMDb code: ', err);
            });
        };
        return button;
    }

    // Wait for the TMDB button to load on the page
    const waitForTmdbButton = setInterval(function() {
        const tmdbButton = document.querySelector('a[href*="themoviedb.org/movie/"]'); // The TMDB button element
        if (tmdbButton) {
            clearInterval(waitForTmdbButton);

            // Extract the IMDb code from the page
            const imdbLinkElement = document.querySelector('a[href*="imdb.com/title/tt"]');
            if (imdbLinkElement) {
                const imdbUrl = imdbLinkElement.href;
                const imdbCodeMatch = imdbUrl.match(/title\/(tt\d+)/);
                if (imdbCodeMatch && imdbCodeMatch[1]) {
                    const imdbCode = imdbCodeMatch[1];
                    const matchingButton = createMatchingButton(imdbCode);
                    tmdbButton.parentNode.insertBefore(matchingButton, tmdbButton.nextSibling); // Insert the button after the TMDB button
                }
            }
        }
    }, 500); // Check every 500ms if the TMDB button has loaded
})();