chrome extension Shortened URL: undefined

21 views Asked by At

URL shortener is not working. i don't what exactly is not working because there is no errors in manage extenssions. Please, if anyone know what exactly wrong with it? manifest.js


{
  "manifest_version": 3,
  "name": "Your Extension Name",
  "version": "1.0",
  "permissions": [
    "storage",
    "activeTab"
  ],
  "action": {
    "default_popup": "popup.html"
  },
  "icons":{
    "16": "./icons/icon16.png",
    "48": "./icons/icon48.png",
    "128": "./icons/icon128.png"
  }
}

 

popup.js

// popup.js
const apiKey = ''
console.log('Content script is running');
document.addEventListener("DOMContentLoaded", function () {
    console.log('DOM content loaded');

    const shortenButton = document.getElementById("shortenButton");
    const urlInput = document.getElementById("urlInput");
    const shortenedUrlContainer = document.getElementById('shortenedUrlContainer');
    const otherContainer = document.getElementById('otherContainer'); // New container

    if (shortenButton && urlInput && shortenedUrlContainer && otherContainer) {
        shortenButton.addEventListener("click", async function () {
            console.log('Button clicked');

            const urlToShorten = urlInput.value;

            try {
                const response = await fetch('https://api-ssl.bitly.com/v4/shorten', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${apiKey}`,  // Replace with your actual Bitly API key
                    },
                    body: JSON.stringify({ url: urlToShorten }),
                });

                const data = await response.json();
                shortenedUrlContainer.innerText = `Shortened URL: ${data.shortenedUrl}`;
                otherContainer.textContent = `Shortened URL: ${data.shortenedUrl}`;
                otherContainer.style.display = 'block'; // Show the container
            } catch (error) {
                console.error('Error:', error);
                shortenedUrlContainer.innerText = 'Error shortening URL';
                otherContainer.style.display = 'none'; // Hide the container on error
            }
        });
    } else {
        console.log('Button, input, or container not found');
    }
});

popup.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URL Shortener</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <input type="text" id="urlInput" placeholder="Enter URL">
    <button id="shortenButton">Shorten URL</button>
    <div id="shortenedUrlContainer" class="messageContainer"></div>
    <div id="otherContainer" class="messageContainer"></div> <!-- New container -->
    <script src="popup.js"></script>
</body>
</html>

style.css

/* style.css */

body {
    font-family: 'Arial', sans-serif;
    background-color: #f5f5f5;
    margin: 0;
    padding: 10px;
    min-width: 200px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

#urlInput {
    margin-bottom: 10px;
    padding: 8px;
    font-size: 16px;
}

button {
    background-color: #4CAF50;
    color: white;
    border: none;
    padding: 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
    border-radius: 5px;
}

.messageContainer {
    margin-top: 10px;
    padding: 10px;
    border-radius: 5px;
    display: none; /* Hide initially */
}

.messageContainer.success {
    background-color: #4CAF50;
    color: white;
}

.messageContainer.error {
    background-color: #f44336;
    color: white;
}

i don't what exactly is not working because there is no errors in manage extenssions.

Chrome extension is not working and i have this result. enter image description here

0

There are 0 answers