// ==UserScript== // @name trakt.tv sudo-flix, Braflix, Freek integration // @match https://trakt.tv/movies/* // @match https://trakt.tv/shows/* // @grant none // @version 1.3.0 // @license GPLv3 // @icon https://icons.duckduckgo.com/ip2/trakt.tv.ico // @description Adds sudo-flix, Braflix, and Freek buttons to the overview page of shows and movies // @run-at document-end // @namespace https://greasyfork.org/users/1257939 // @downloadURL none // ==/UserScript== (function() { // Extract and sanitize URL details const extractDetailsFromUrl = (url) => { try { const parts = url.split("/"); return { tmdbId: parts[4], season: parts[6] || 1, // Default to first season on show overview page episode: parts[8] || 1, // Default to first episode on season overview page }; } catch (error) { console.error("Failed to extract URL details:", error); return null; } }; // Sanitize the title by removing year suffix if present const sanitizeTitle = (title) => { try { return title.replace(/-\d{4}$/, ''); } catch (error) { console.error("Failed to sanitize title:", error); return title; } }; // Create a new link element with appropriate attributes const createNewLinkElement = (service, suffix, targetUrl, textContent) => { try { const linkElement = document.createElement('a'); linkElement.target = "_blank"; linkElement.id = `external-link-${service}`; linkElement.href = `${targetUrl}/${suffix}`; linkElement.dataset.originalTitle = ""; linkElement.title = ""; linkElement.textContent = textContent; return linkElement; } catch (error) { console.error("Failed to create new link element:", error); return null; } }; // Create external links based on the extracted details const createExternalLink = () => { try { const tmdbLink = document.getElementById("external-link-tmdb"); if (!tmdbLink) { console.error("TMDB link doesn’t exist."); return; } const { tmdbId, season, episode } = extractDetailsFromUrl(tmdbLink.href); const title = sanitizeTitle(location.pathname.split("/")[2]); // Extract title from location const showMovie = location.pathname.split("/")[1] === "movies" ? "movie" : "tv"; const buttonsPath = document.querySelector('.external > li'); if (!buttonsPath) { console.error("Path for buttons doesn’t exist."); return; } const linkElementMovieWeb = createNewLinkElement( 'sudo-flix', `media/tmdb-${showMovie}-${tmdbId}-${title}`, 'https://sudo-flix.rip', 'sudo-flix' ); const linkElementBraflix = createNewLinkElement( 'braflix', `${showMovie}/${tmdbId}${showMovie === 'tv' ? `/${season}/${episode}?play=true` : ''}`, 'https://www.braflix.st', 'Braflix' ); const linkElementFreek = createNewLinkElement( 'freek', `watch/${showMovie}/${tmdbId}${showMovie === 'tv' ? `?season=${season}&ep=${episode}` : ''}`, 'https://freek.to', 'Freek' ); // Append the created links to the buttonsPath if (linkElementMovieWeb) { buttonsPath.appendChild(linkElementMovieWeb); } if (linkElementBraflix) { buttonsPath.appendChild(linkElementBraflix); } if (linkElementFreek) { buttonsPath.appendChild(linkElementFreek); } } catch (error) { console.error("Failed to create external links:", error); } }; // Initialize the function when the page loads window.addEventListener('load', () => { createExternalLink(); }); })();