<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loadstring Maker</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #131313;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow-y: auto;
            position: relative;
        }

        /* Top Right Navigation */
        .top-nav {
            position: absolute;
            top: 10px;
            right: 20px;
            display: flex;
            gap: 20px;
        }

        .top-nav a {
            color: white;
            text-decoration: none;
            font-size: 16px;
            font-family: Arial, sans-serif;
        }

        .top-nav a:hover {
            color: #5a5a5a;
        }

        .container {
            background-color: #1f1f1f;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            max-width: 700px;
            width: 100%;
            text-align: center;
            border: 1px solid #aab8c2;
            box-sizing: border-box;
        }

        h1 {
            margin-bottom: 20px;
            font-size: 24px;
            color: #fff;
        }

        input, textarea {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border-radius: 3px;
            border: 1px solid #aab8c2;
            background-color: #2c2c2c;
            color: white;
            resize: none;
            box-sizing: border-box;
        }

        textarea {
            height: 150px;
            margin-top: 10px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            font-size: 18px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        .history-box {
            margin-top: 30px;
            padding: 15px;
            background-color: #2c2c2c;
            border: 1px solid #aab8c2;
            max-height: 300px;
            overflow-y: auto;
            border-radius: 5px;
            box-sizing: border-box;
        }

        .history-box div {
            margin-bottom: 15px;
            padding: 10px;
            border-bottom: 1px solid #aab8c2;
        }

        .history-box div:last-child {
            border-bottom: none;
        }

        .history-box p {
            font-size: 14px;
            margin: 5px 0;
            word-wrap: break-word;
        }

        .timestamp {
            font-size: 12px;
            color: #ccc;
        }

        #gistLink {
            margin-top: 20px;
            font-size: 16px;
            color: #aab8c2;
        }

        a {
            color: #aab8c2;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        #clearHistoryBtn {
            background-color: #f44336;
            margin-top: 20px;
        }

        #clearHistoryBtn:hover {
            background-color: #d32f2f;
        }
    </style>
</head>
<body>


<div class="top-nav">
    <a href="#">Home</a>
    <a href="#">Loadstring Maker</a>
    <a href="#">Account</a>
</div>

<div class="container">
    <h1>Loadstring Maker</h1>

    <input id="gistName" type="text" placeholder="Enter name Of Loadstring." />
    <textarea id="userInput" placeholder="Enter Your Script"></textarea>
    <br>
    <button onclick="createGitHubGist()">Create Loadstring</button>
    <p id="gistLink"></p>

    <div class="history-box">
        <h2>History</h2>
        <div id="historyLinks"></div>
    </div>


    <button id="clearHistoryBtn" onclick="clearHistory()">Clear History</button>
</div>

<script>

    function timeAgo(timestamp) {
        const now = Date.now();
        const diff = now - timestamp;


        const seconds = Math.floor(diff / 1000);
        const minutes = Math.floor(diff / 60000);
        const hours = Math.floor(diff / 3600000);
        const days = Math.floor(diff / 86400000);

        if (seconds < 60) {
            return `${seconds} second(s) ago`;
        } else if (minutes < 60) {
            return `${minutes} minute(s) ago`;
        } else if (hours < 24) {
            return `${hours} hour(s) ago`;
        } else {
            return `${days} day(s) ago`;
        }
    }


    function loadHistory() {
        const history = JSON.parse(localStorage.getItem("gistHistory")) || [];
        const historyContainer = document.getElementById("historyLinks");
        historyContainer.innerHTML = '';

        history.forEach(entry => {

            if (entry.name && entry.link && entry.timestamp) {
                const historyItem = document.createElement('div');
                const timeAgoString = timeAgo(entry.timestamp);
                historyItem.innerHTML = `
                    <strong>${entry.name}</strong> <span class="timestamp" id="timestamp-${entry.timestamp}">(${timeAgoString})</span>
                    <p><a href="${entry.link}" target="_blank">${entry.loadstring}</a></p>
                `;
                historyContainer.appendChild(historyItem);
            }
        });
    }


    function updateTimestamps() {
        const history = JSON.parse(localStorage.getItem("gistHistory")) || [];

        history.forEach(entry => {
            const timestampElement = document.getElementById(`timestamp-${entry.timestamp}`);
            if (timestampElement) {
                timestampElement.textContent = `(${timeAgo(entry.timestamp)})`;
            }
        });
    }


    async function createGitHubGist() {
        const userInput = document.getElementById("userInput").value;
        const gistName = document.getElementById("gistName").value || "Untitled Gist";
        const token = "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";  
        const repo = "loadstrings";  
        const fileName = "loadstring.txt";  

        if (userInput.trim() === "") {
            alert("Please enter some text.");
            return;
        }

        const data = {
            "files": {
                [fileName]: {
                    "content": userInput
                }
            }
        };

        try {
            const response = await fetch(`https://api.github.com/gists`, {
                method: "POST",
                headers: {
                    "Authorization": `token ${token}`,
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(data)
            });

            const result = await response.json();

            if (result.html_url) {
                const rawLink = result.files[fileName].raw_url;
                const loadstring = `loadstring(game:HttpGet('${rawLink}'))()`;


                let history = JSON.parse(localStorage.getItem("gistHistory")) || [];
                history.push({
                    name: gistName,
                    link: rawLink,
                    loadstring: loadstring,
                    timestamp: Date.now()
                });


                history = history.filter(entry => entry.name && entry.link && entry.timestamp);
                localStorage.setItem("gistHistory", JSON.stringify(history));

                loadHistory(); 


                document.getElementById("gistLink").innerHTML = "";
            } else {
                alert("Error creating Gist.");
            }
        } catch (error) {
            console.error(error);
            alert("An error occurred while creating the GitHub Gist.");
        }
    }


    function clearHistory() {
        if (confirm("Are you sure you want to clear the history?")) {
            localStorage.removeItem("gistHistory");
            loadHistory();  
        }
    }


    loadHistory();


    setInterval(updateTimestamps, 1000);
</script>

</body>
</html>
