// ==UserScript== // @name Letterboxd Friend Ratings Analyzer // @namespace http://tampermonkey.net/ // @version 3.4 // @description Analyze ratings from friends on Letterboxd, including paginated ratings, and show a histogram below the global one. // @author https://github.com/liam-h // @match https://letterboxd.com/film/* // @grant none // @license GPLv3 // @run-at document-end // @downloadURL none // ==/UserScript== const username = "YOUR_USERNAME_HERE"; const film = window.location.href.split("/").slice(-2, -1)[0]; const fetchRatings = (user, film) => fetch(`/${user}/friends/film/${film}/ratings/rated/.5-5/`) .then((response) => response.text()) .then((html) => Array.from( new DOMParser() .parseFromString(html, "text/html") .querySelectorAll(".film-rating-group"), ).flatMap((section) => { const [, score] = section .querySelector("h2 > .rating") ?.className.match(/rated-large-(\d+)/) || []; const reviewCount = section.querySelectorAll( "ul.avatar-list > li", ).length; return score ? Array(reviewCount).fill(parseFloat(score) / 2) : []; }), ); // Construct the friends' rating histogram with links const constructHistogram = (ratings, user, film) => { const bins = Array(10).fill(0); ratings.forEach((rating) => bins[Math.floor((rating - 0.5) * 2)]++); const maxCount = Math.max(...bins); return `
★★★★★
`; }; // Place histogram below the global one, adding links const placeHistogram = (histogramHtml, averageRating, user, film, count) => { const globalHistogramSection = document.querySelector( ".ratings-histogram-chart", ); if (globalHistogramSection) { const friendsRatingsLink = `/${user}/friends/film/${film}/rated/.5-5/`; const friendsHistogramSection = document.createElement("section"); friendsHistogramSection.classList.add("section", "ratings-histogram-chart"); friendsHistogramSection.innerHTML = `

Ratings from friends ${averageRating}

${histogramHtml} `; globalHistogramSection.parentNode.insertBefore( friendsHistogramSection, globalHistogramSection.nextSibling, ); } }; // Main function to run the script fetchRatings(username, film).then((ratings) => { if (ratings.length) { const averageRating = ( ratings.reduce((sum, rating) => sum + rating, 0) / ratings.length ).toFixed(1); placeHistogram( constructHistogram(ratings, username, film), averageRating, username, film, ratings.length, ); } });