JSFiddle - React, Tailwind, and code Playground

by Aakash Chakravarthy

HTML

<h1>Google Drive Link Converter</h1>
  <p>Enter a Google Drive sharing link here:</p>
  <input type="text" id="shareLink" placeholder="Paste sharing link">
  <button onclick="convertLink()">Convert Link</button>
  <br>
  <br>
  <div id="downloadLink"></div>

JavaScript

function convertLink() {
  const shareLink = document.getElementById("shareLink").value;

    //https://drive.google.com/file/d/1BjSvPzNZOyJ0HfG4y7J3IcUti_LCUKHe/view?usp=sharing

  // Extract the file ID from the sharing link
  const url_obj = new URL(shareLink);
  const fileId = url_obj.pathname.split('/')[3];

  if (!fileId) {
    alert("Invalid Google Drive sharing link. Please check the URL.");
    return;
  }

  // Construct the downloadable link
  const downloadLink = `https://drive.google.com/uc?export=download&id=${fileId}`;

  // Display the downloadable link
  document.getElementById("downloadLink").innerHTML = `Download Link: <a href="${downloadLink}" target="_blank">${downloadLink}</a>`;
}