Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] URL Shortener #295

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Url_shortener/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!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
href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css"
rel="stylesheet"
/>
</head>
<body class="flex items-center justify-center h-screen bg-gray-100">
<div class="bg-white p-8 rounded shadow-md text-center w-80">
<h1 class="text-2xl font-bold mb-4">URL Shortener</h1>
<input
type="text"
id="url-input"
class="w-full p-2 mb-4 border border-gray-300 rounded"
placeholder="Enter URL"
/>
<button
id="shorten-btn"
class="w-full bg-blue-500 text-white py-2 rounded mb-4"
>
Shorten URL
</button>
<div id="result" class="hidden">
<p id="short-url" class="mb-4"></p>
<button
id="copy-btn"
class="w-full bg-green-500 text-white py-2 rounded mb-2"
>
Copy
</button>
<button
id="share-btn"
class="w-full bg-teal-500 text-white py-2 rounded"
>
Share
</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
64 changes: 64 additions & 0 deletions Url_shortener/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
document.getElementById("shorten-btn").addEventListener("click", async () => {
const urlInput = document.getElementById("url-input").value.trim();
if (urlInput) {
const apiUrl = "https://spoo-me-url-shortener.p.rapidapi.com/";
const options = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
"X-RapidAPI-Key": "a9691f48eemsh89135388428c926p181a59jsn952d884f92e7",
"X-RapidAPI-Host": "spoo-me-url-shortener.p.rapidapi.com",
},
body: new URLSearchParams({
url: urlInput,
}),
};

try {
const response = await fetch(apiUrl, options);
const result = await response.json();
console.log("API response:", result); // Log the response for debugging

if (result && result.short_url) {
const shortUrl = result.short_url;
document.getElementById("short-url").textContent = shortUrl;
document.getElementById("result").classList.remove("hidden");
} else {
alert("Error shortening URL: " + (result.message || "Unknown error"));
}
} catch (error) {
console.error("Error:", error);
alert("Error shortening URL: " + error.message);
}
} else {
alert("Please enter a valid URL.");
}
});

document.getElementById("copy-btn").addEventListener("click", () => {
const shortUrl = document.getElementById("short-url").textContent;
navigator.clipboard
.writeText(shortUrl)
.then(() => {
alert("URL copied to clipboard!");
})
.catch((err) => {
console.error("Could not copy text: ", err);
alert("Failed to copy URL.");
});
});

document.getElementById("share-btn").addEventListener("click", () => {
const shortUrl = document.getElementById("short-url").textContent;
if (navigator.share) {
navigator
.share({
title: "Check out this URL",
url: shortUrl,
})
.catch((error) => console.error("Error sharing:", error));
} else {
alert("Sharing not supported on this browser");
}
});