Author Topic: YouTube URL formatter code  (Read 213 times)

0 Members and 1 Guest are viewing this topic.

Offline flipperTopic starter

  • Regular Contributor
  • *
  • !
  • Posts: 72
  • Country: gb
  • Definitely eti, unlokia, glossywhite, iamwhoiam
YouTube URL formatter code
« on: November 10, 2024, 12:26:43 am »
Since this forum is exceptionally pedantic as to which YouTube URLs it will accept to make the video embed properly, I asked ChatGPT to make a simple YouTube URL formatter. 

You paste in any old YouTube URL (m.youtube.com/BLAHBLAH or youtu.be/BLAHBLAH) and it spits out a properly formatted URL in the format this forum likes, which you can then just paste into your posts.

FYI: The forum only appears to like “https://www.youtube.com/watch?v=<VIDEO_ID>” formatted types.

If there’s any errors, please take it up with the ChatGPT bots 🤖 🤣

# Edit: the “Copy” button doesn’t seem to work, so please select and copy the formatted URL text. I am not an HTML expert, but please do fix the code if you’d like.

Here’s the static HTML, I shoved it on my Pi and serve it with Apache2 web server:

Code: [Select]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YouTube URL Formatter</title>
</head>
<body>
    <h2>YouTube URL Formatter</h2>
    <input type="text" id="youtubeUrl" placeholder="Paste YouTube URL here" style="width: 400px;">
    <button onclick="formatUrl()">Format</button>
   
    <p>Formatted URL:</p>
    <pre><code id="formattedUrl"></code></pre>
    <button onclick="copyToClipboard()">Copy</button>

    <script>
        function formatUrl() {
            const urlInput = document.getElementById("youtubeUrl").value;
            const formattedUrlOutput = document.getElementById("formattedUrl");

            try {
                const url = new URL(urlInput);
                let videoId = '';

                if (url.hostname === 'youtu.be') {
                    videoId = url.pathname.slice(1);
                } else if (url.hostname.includes('youtube.com')) {
                    videoId = url.searchParams.get('v');
                }

                if (videoId) {
                    formattedUrlOutput.textContent = `https://www.youtube.com/watch?v=${videoId}`;
                } else {
                    formattedUrlOutput.textContent = 'Invalid YouTube URL';
                }
            } catch (error) {
                formattedUrlOutput.textContent = 'Invalid URL format';
            }
        }

        function copyToClipboard() {
            const formattedUrl = document.getElementById("formattedUrl").textContent;
            navigator.clipboard.writeText(formattedUrl).then(() => {
                alert("URL copied to clipboard!");
            }).catch((error) => {
                alert("Failed to copy: "   error);
            });
        }
    </script>
</body>
</html>
« Last Edit: November 10, 2024, 12:37:44 am by flipper »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf