Greasy Fork

trakt.tv sudo-flix, Braflix, Binged integration

Adds sudo-flix, Braflix, and Binged buttons to the overview page of shows and movies

目前为 2024-09-20 提交的版本。查看 最新版本

// ==UserScript==
// @name        trakt.tv sudo-flix, Braflix, Binged integration
// @match       https://trakt.tv/movies/*
// @match       https://trakt.tv/shows/*
// @grant       none
// @version     1.2.0
// @license     GPLv3
// @icon        https://icons.duckduckgo.com/ip2/trakt.tv.ico
// @description Adds sudo-flix, Braflix, and Binged buttons to the overview page of shows and movies
// @run-at      document-end
// @namespace   https://greasyfork.org/users/1257939
// ==/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 linkElementBinged = createNewLinkElement(
                'binged',
                `watch/${showMovie}/${tmdbId}${showMovie === 'tv' ? `?season=${season}&ep=${episode}` : ''}`,
                'https://binged.to',
                'Binged'
            );

            // Append the created links to the buttonsPath
            if (linkElementMovieWeb) {
                buttonsPath.appendChild(linkElementMovieWeb);
            }
            if (linkElementBraflix) {
                buttonsPath.appendChild(linkElementBraflix);
            }
            if (linkElementBinged) {
                buttonsPath.appendChild(linkElementBinged);
            }
        } catch (error) {
            console.error("Failed to create external links:", error);
        }
    };

    // Initialize the function when the page loads
    window.addEventListener('load', () => {
        createExternalLink();
    });
})();