// ==UserScript==
// @name         Specific m3u8 Link Detector and Copier
// @version      1.2
// @description  Detect specific m3u8 links, modify them, and display with copy and hide buttons. Link text is displayed only on hover.
// @author       Nimendra
// @icon         https://img.icons8.com/?size=100&id=roUdNSN10VmV&format=png&color=000000
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const detectedLinks = [];

    // Function to check if the URL matches the pattern and ends with .m3u8
    function isSpecificM3U8Link(url) {
        return url.includes("_144.m3u8");
    }

    // Function to modify the m3u8 link
    function modifyM3U8Link(url) {
        return url.replace("_144.m3u8", "_360.m3u8");
    }

    // Function to create and show the floating element with copy and hide buttons
    function showFloatingElement(url) {
        if (detectedLinks.includes(url)) return;

        const modifiedUrl = modifyM3U8Link(url);
        detectedLinks.push(url);

        const floatDiv = document.createElement("div");
        floatDiv.style.position = "fixed";
        floatDiv.style.bottom = "20px";
        floatDiv.style.right = "20px";
        floatDiv.style.backgroundColor = "transparent";
        floatDiv.style.color = "white";
        floatDiv.style.padding = "10px";
        floatDiv.style.borderRadius = "5px";
        floatDiv.style.zIndex = "9999999999";
        floatDiv.style.maxWidth = "300px";
        floatDiv.style.wordWrap = "break-word";
        floatDiv.style.display = "flex";
        floatDiv.style.flexDirection = "column";
        floatDiv.style.alignItems = "center";

        const linkText = document.createElement("div");
        linkText.innerText = modifiedUrl;
        linkText.style.marginBottom = "10px";
        linkText.style.wordBreak = "break-all";
        linkText.style.opacity = "0"; // Initially hide link text
        linkText.style.transition = "opacity 0.3s"; // Add transition effect
        floatDiv.appendChild(linkText);

        // Show link text on hover
        floatDiv.addEventListener("mouseenter", () => {
            linkText.style.opacity = "1";
        });
        floatDiv.addEventListener("mouseleave", () => {
            linkText.style.opacity = "0";
        });

        const copyButton = document.createElement("button");
        copyButton.innerText = "Copy Link";
        copyButton.style.marginBottom = "5px";
        copyButton.style.padding = "5px 10px";
        copyButton.style.border = "none";
        copyButton.style.borderRadius = "3px";
        copyButton.style.backgroundColor = "white";
        copyButton.style.color = "black";
        copyButton.style.cursor = "pointer";
        copyButton.addEventListener("click", () => {
            copyTextToClipboard(modifiedUrl);
            copyButton.innerText = "Copied";
            setTimeout(() => {
                copyButton.innerText = "Copy Link";
            }, 2000);
        });
        floatDiv.appendChild(copyButton);

        const hideButton = document.createElement("button");
        hideButton.innerText = "Hide";
        hideButton.style.padding = "5px 10px";
        hideButton.style.border = "none";
        hideButton.style.borderRadius = "3px";
        hideButton.style.backgroundColor = "white";
        hideButton.style.color = "black";
        hideButton.style.cursor = "pointer";
        hideButton.addEventListener("click", () => {
            floatDiv.style.display = "none";
        });
        floatDiv.appendChild(hideButton);

        document.body.appendChild(floatDiv);
    }

    // Function to copy text to the clipboard
    function copyTextToClipboard(text) {
        const textArea = document.createElement("textarea");
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand('copy');
        document.body.removeChild(textArea);
    }

    // Hook into fetch responses
    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        return originalFetch.apply(this, args).then(response => {
            const clonedResponse = response.clone();
            const url = response.url;
            if (isSpecificM3U8Link(url)) {
                showFloatingElement(url);
            }
            return response;
        });
    };

    // Hook into XMLHttpRequest responses
    const originalOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(...args) {
        this.addEventListener('load', function() {
            const url = args[1];
            if (isSpecificM3U8Link(url)) {
                showFloatingElement(url);
            }
        });
        originalOpen.apply(this, args);
    };
})();