Letterboxd TuneFind Link

Adds a button to Letterboxd movie pages that links to the TuneFind page of the movie.

// ==UserScript==
// @name         Letterboxd TuneFind Link
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Adds a button to Letterboxd movie pages that links to the TuneFind page of the movie.
// @author       Megane0103
// @match        https://letterboxd.com/film/*
// @grant        none
// @license MIT; https://github.com/Megane0103/letterboxd-tunefind-link/blob/main/LICENSE
// @copyright 2023, Megane0103 (https://github.com/Megane0103)
// ==/UserScript==

(function() {
    'use strict';

    // Extract movie title from the page
    const movieTitle = document.querySelector('.headline-1').textContent.trim().toLowerCase().replace(/\s+/g, '-').replace(/'/g, '');

    // Check if the URL already contains the year
    const yearFromUrl = window.location.href.match(/-(\d{4})$/);
    let movieYear = yearFromUrl ? yearFromUrl[1] : null;

    // Extract movie year from the page if not found in the URL
    if (!movieYear) {
        const movieYearElement = document.querySelector('a[href^="/films/year/"]');
        movieYear = movieYearElement ? movieYearElement.textContent.trim() : null;
    }

    if (movieTitle && movieYear) {
        // Create the TuneFind link
        const tuneFindLink = `https://www.tunefind.com/movie/${movieTitle}-${movieYear}`;

        // Create the button element
        const tuneFindButton = document.createElement('a');
        tuneFindButton.className = 'micro-button track-event';
        tuneFindButton.href = tuneFindLink;
        tuneFindButton.textContent = 'TuneFind';
        tuneFindButton.target = '_blank';
        tuneFindButton.style.marginLeft = '3px';

        // Find the TMDb button
        const tmdbButton = document.querySelector('a[data-track-action="TMDb"]');
        if (tmdbButton) {
            // Insert the TuneFind button after the TMDb button
            tmdbButton.parentNode.insertBefore(tuneFindButton, tmdbButton.nextSibling);
        }
    }
})();