Greasy Fork

Greasy Fork is available in English.

Letterboxd Titles

Force titles to be displayed above movie posters (on some pages only)

当前为 2020-12-10 提交的版本,查看 最新版本

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Letterboxd Titles
// @namespace    https://letterboxd.com/
// @version      0.1
// @description  Force titles to be displayed above movie posters (on some pages only)
// @author       n00bCod3r (https://github.com/n00bCod3rr)
// @match        https://letterboxd.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    function addTitles() {
        const maxTitleLength = 35;
        let movieInfo = "";
        //ul that will contain all the movie posters
        const posterNodeListContainer = document.querySelector('ul.poster-list');
        posterNodeListContainer.style.alignItems = "flex-end";
        const posterNodeList = document.querySelectorAll('li>div[class*="film-poster-"]');
        //for every movie
        for (const elem of posterNodeList) {
            //since the function will be called on scroll event, I add a class to every element that is tweaked so that the div is added only once
            const elemClasses = elem.className;
            if (!elemClasses.includes('tweaked')) {
                elem.className += ' tweaked';
                const movieTitle = elem.dataset.filmName;
                //if the movie title is too long, trim it (change maxTitleLeght as you please)
                if (movieTitle.length > maxTitleLength) {
                    const trimmedMovieTitle = `${movieTitle.substring(0,maxTitleLength-3)}...`;
                    movieInfo = `${trimmedMovieTitle}\n(${elem.dataset.filmReleaseYear})`;
                }
                else {
                    movieInfo = `${elem.dataset.filmName} (${elem.dataset.filmReleaseYear})`;
                }
                //Title (yyyy)
                const width = elem.offsetWidth; //this will become the width of the divNode
                const divNode = document.createElement("div");
                divNode.innerHTML = movieInfo; //writes Movietitle <br/> (year) inside the div
                //set width, text-align, overflow-wrap CSS properties for the div
                divNode.style.width = `${width}px`;
                divNode.style.textAlign = "center";
                divNode.style.overflowWrap = "break-word"; //long words will go in a new line
                //targets the DOM node father of the current div node, and insert the new divNode as its first child (to show the div above the poster image)
                const nodeElemParent = elem.parentNode;
                nodeElemParent.insertBefore(divNode, elem);
            }
        }
    }
    //wait for page to load
    window.addEventListener('load', function() {
        addTitles();
        const currentURL = window.location.href;
        console.log(currentURL);
        if (currentURL.includes('/list/') || currentURL.includes('/watchlist/')) {
            document.addEventListener('scroll', addTitles, false);
        };
    }, false);

})();