slimshadow commited on
Commit
4b1f3a4
·
verified ·
1 Parent(s): ad90a0a

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +22 -30
script.js CHANGED
@@ -1,39 +1,31 @@
1
- async function downloadReel() {
2
- const reelUrl = document.getElementById('reelUrl').value.trim();
3
- const messageDiv = document.getElementById('message');
4
- const downloadLinkDiv = document.getElementById('downloadLink');
5
-
6
- // Clear previous messages
7
- messageDiv.textContent = '';
8
- downloadLinkDiv.innerHTML = '';
9
 
10
- // Check if the URL is empty or invalid
11
- if (!reelUrl || !isValidUrl(reelUrl)) {
12
- messageDiv.textContent = 'Please enter a valid Instagram Reel URL.';
13
- return;
14
- }
15
-
16
- // Display loading message
17
- messageDiv.textContent = 'Processing... Please wait.';
18
 
19
  try {
20
- const response = await fetch(`https://slimshadow-instagram-r-api.hf.space/download/?reel_url=${encodeURIComponent(reelUrl)}`);
 
 
 
 
 
 
21
  const data = await response.json();
22
 
23
- if (data && data.url) {
24
- // Display download link
25
- messageDiv.textContent = 'Reel downloaded successfully!';
26
- downloadLinkDiv.innerHTML = `<a href="${data.url}" download>Click here to download the Reel</a>`;
 
 
 
27
  } else {
28
- messageDiv.textContent = 'Failed to fetch Reel. Please try again later.';
29
  }
30
  } catch (error) {
31
- messageDiv.textContent = 'An error occurred. Please try again later.';
 
32
  }
33
- }
34
-
35
- // Function to validate the URL format
36
- function isValidUrl(url) {
37
- const regex = /^(https?:\/\/)?(www\.)?instagram\.com\/reel\/[A-Za-z0-9_-]+$/;
38
- return regex.test(url);
39
- }
 
1
+ document.getElementById("reelForm").addEventListener("submit", async function (e) {
2
+ e.preventDefault();
 
 
 
 
 
 
3
 
4
+ const reelUrl = document.getElementById("reelUrl").value;
5
+ const apiUrl = `https://slimshadow-instagram-r-api.hf.space/download/?reel_url=${encodeURIComponent(reelUrl)}`;
 
 
 
 
 
 
6
 
7
  try {
8
+ // Make the API request
9
+ const response = await fetch(apiUrl);
10
+ if (!response.ok) {
11
+ throw new Error("Failed to fetch the reel. Please check the URL and try again.");
12
+ }
13
+
14
+ // Parse the JSON response
15
  const data = await response.json();
16
 
17
+ // Check if the response contains a valid download link
18
+ if (data.download_link) {
19
+ // Show the download link to the user
20
+ document.getElementById("result").classList.remove("hidden");
21
+ const downloadLinkElement = document.getElementById("downloadLink");
22
+ downloadLinkElement.href = data.download_link;
23
+ downloadLinkElement.textContent = "Click here to download the reel";
24
  } else {
25
+ throw new Error("Download link not available. Please try again.");
26
  }
27
  } catch (error) {
28
+ // Handle any errors that occur during the fetch or parsing
29
+ alert("Error: " + error.message);
30
  }
31
+ });